add Feautrier's scheduling algorithm
[isl.git] / interface / extract_interface.cc
blobb2126a3d77c24defde8accd83831b315d584eb2c
1 /*
2 * Copyright 2011 Sven Verdoolaege. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Sven Verdoolaege.
32 */
34 #include <assert.h>
35 #include <iostream>
36 #include <llvm/Support/raw_ostream.h>
37 #include <llvm/Support/CommandLine.h>
38 #include <llvm/Support/Host.h>
39 #include <llvm/Support/ManagedStatic.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTConsumer.h>
42 #include <clang/Basic/FileSystemOptions.h>
43 #include <clang/Basic/FileManager.h>
44 #include <clang/Basic/TargetOptions.h>
45 #include <clang/Basic/TargetInfo.h>
46 #include <clang/Basic/Version.h>
47 #include <clang/Driver/Compilation.h>
48 #include <clang/Driver/Driver.h>
49 #include <clang/Driver/Tool.h>
50 #include <clang/Frontend/CompilerInstance.h>
51 #include <clang/Frontend/CompilerInvocation.h>
52 #include <clang/Frontend/DiagnosticOptions.h>
53 #include <clang/Frontend/TextDiagnosticPrinter.h>
54 #include <clang/Frontend/Utils.h>
55 #include <clang/Lex/HeaderSearch.h>
56 #include <clang/Lex/Preprocessor.h>
57 #include <clang/Parse/ParseAST.h>
58 #include <clang/Sema/Sema.h>
60 #include "isl_config.h"
61 #include "extract_interface.h"
62 #include "python.h"
64 using namespace std;
65 using namespace clang;
66 using namespace clang::driver;
68 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
69 llvm::cl::Required, llvm::cl::desc("<input file>"));
70 static llvm::cl::list<string> Includes("I",
71 llvm::cl::desc("Header search path"),
72 llvm::cl::value_desc("path"), llvm::cl::Prefix);
74 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
76 /* Does decl have an attribute of the following form?
78 * __attribute__((annotate("name")))
80 bool has_annotation(Decl *decl, const char *name)
82 if (!decl->hasAttrs())
83 return false;
85 AttrVec attrs = decl->getAttrs();
86 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
87 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
88 if (!ann)
89 continue;
90 if (ann->getAnnotation().str() == name)
91 return true;
94 return false;
97 /* Is decl marked as exported?
99 static bool is_exported(Decl *decl)
101 return has_annotation(decl, "isl_export");
104 /* Collect all types and functions that are annotated "isl_export"
105 * in "types" and "function".
107 * We currently only consider single declarations.
109 struct MyASTConsumer : public ASTConsumer {
110 set<RecordDecl *> types;
111 set<FunctionDecl *> functions;
113 virtual void HandleTopLevelDecl(DeclGroupRef D) {
114 Decl *decl;
116 if (!D.isSingleDecl())
117 return;
118 decl = D.getSingleDecl();
119 if (!is_exported(decl))
120 return;
121 switch (decl->getKind()) {
122 case Decl::Record:
123 types.insert(cast<RecordDecl>(decl));
124 break;
125 case Decl::Function:
126 functions.insert(cast<FunctionDecl>(decl));
127 break;
128 default:
129 break;
134 #ifdef USE_ARRAYREF
136 #ifdef HAVE_CXXISPRODUCTION
137 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
139 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
140 "", false, false, Diags);
142 #else
143 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
145 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
146 "", false, Diags);
148 #endif
150 /* Create a CompilerInvocation object that stores the command line
151 * arguments constructed by the driver.
152 * The arguments are mainly useful for setting up the system include
153 * paths on newer clangs and on some platforms.
155 static CompilerInvocation *construct_invocation(const char *filename,
156 DiagnosticsEngine &Diags)
158 const char *binary = CLANG_PREFIX"/bin/clang";
159 const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags));
160 std::vector<const char *> Argv;
161 Argv.push_back(binary);
162 Argv.push_back(filename);
163 const llvm::OwningPtr<Compilation> compilation(
164 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
165 JobList &Jobs = compilation->getJobs();
167 Command *cmd = cast<Command>(*Jobs.begin());
168 if (strcmp(cmd->getCreator().getName(), "clang"))
169 return NULL;
171 const ArgStringList *args = &cmd->getArguments();
173 CompilerInvocation *invocation = new CompilerInvocation;
174 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
175 args->data() + args->size(),
176 Diags);
177 return invocation;
180 #else
182 static CompilerInvocation *construct_invocation(const char *filename,
183 DiagnosticsEngine &Diags)
185 return NULL;
188 #endif
190 int main(int argc, char *argv[])
192 llvm::cl::ParseCommandLineOptions(argc, argv);
194 CompilerInstance *Clang = new CompilerInstance();
195 DiagnosticOptions DO;
196 Clang->createDiagnostics(0, NULL,
197 new TextDiagnosticPrinter(llvm::errs(), DO));
198 DiagnosticsEngine &Diags = Clang->getDiagnostics();
199 Diags.setSuppressSystemWarnings(true);
200 CompilerInvocation *invocation =
201 construct_invocation(InputFilename.c_str(), Diags);
202 if (invocation)
203 Clang->setInvocation(invocation);
204 Clang->createFileManager();
205 Clang->createSourceManager(Clang->getFileManager());
206 TargetOptions TO;
207 TO.Triple = llvm::sys::getDefaultTargetTriple();
208 TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO);
209 Clang->setTarget(target);
210 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
211 LangStandard::lang_unspecified);
212 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
213 LangOptions &LO = Clang->getLangOpts();
214 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
215 HSO.ResourceDir = ResourceDir;
217 for (int i = 0; i < Includes.size(); ++i)
218 HSO.AddPath(Includes[i], frontend::Angled, true, false, false);
220 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
221 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
222 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
223 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
224 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
225 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
227 Clang->createPreprocessor();
228 Preprocessor &PP = Clang->getPreprocessor();
230 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO);
232 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
233 assert(file);
234 Clang->getSourceManager().createMainFileID(file);
236 Clang->createASTContext();
237 MyASTConsumer consumer;
238 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
240 Diags.getClient()->BeginSourceFile(LO, &PP);
241 ParseAST(*sema);
242 Diags.getClient()->EndSourceFile();
244 generate_python(consumer.types, consumer.functions);
246 delete sema;
247 delete Clang;
248 llvm::llvm_shutdown();
250 return 0;