isl 0.08
[isl.git] / interface / extract_interface.cc
blob7b25a77190f754b1a43a99957dc9034a08a8cdba
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/Frontend/CompilerInstance.h>
48 #include <clang/Frontend/CompilerInvocation.h>
49 #include <clang/Frontend/DiagnosticOptions.h>
50 #include <clang/Frontend/TextDiagnosticPrinter.h>
51 #include <clang/Frontend/Utils.h>
52 #include <clang/Lex/HeaderSearch.h>
53 #include <clang/Lex/Preprocessor.h>
54 #include <clang/Parse/ParseAST.h>
55 #include <clang/Sema/Sema.h>
57 #include "isl_config.h"
58 #include "extract_interface.h"
59 #include "python.h"
61 using namespace std;
62 using namespace clang;
64 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
65 llvm::cl::Required, llvm::cl::desc("<input file>"));
66 static llvm::cl::list<string> Includes("I",
67 llvm::cl::desc("Header search path"),
68 llvm::cl::value_desc("path"), llvm::cl::Prefix);
70 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
72 /* Does decl have an attribute of the following form?
74 * __attribute__((annotate("name")))
76 bool has_annotation(Decl *decl, const char *name)
78 if (!decl->hasAttrs())
79 return false;
81 AttrVec attrs = decl->getAttrs();
82 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
83 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
84 if (!ann)
85 continue;
86 if (ann->getAnnotation().str() == name)
87 return true;
90 return false;
93 /* Is decl marked as exported?
95 static bool is_exported(Decl *decl)
97 return has_annotation(decl, "isl_export");
100 /* Collect all types and functions that are annotated "isl_export"
101 * in "types" and "function".
103 * We currently only consider single declarations.
105 struct MyASTConsumer : public ASTConsumer {
106 set<RecordDecl *> types;
107 set<FunctionDecl *> functions;
109 virtual void HandleTopLevelDecl(DeclGroupRef D) {
110 Decl *decl;
112 if (!D.isSingleDecl())
113 return;
114 decl = D.getSingleDecl();
115 if (!is_exported(decl))
116 return;
117 switch (decl->getKind()) {
118 case Decl::Record:
119 types.insert(cast<RecordDecl>(decl));
120 break;
121 case Decl::Function:
122 functions.insert(cast<FunctionDecl>(decl));
123 break;
124 default:
125 break;
130 int main(int argc, char *argv[])
132 llvm::cl::ParseCommandLineOptions(argc, argv);
134 CompilerInstance *Clang = new CompilerInstance();
135 DiagnosticOptions DO;
136 Clang->createDiagnostics(0, NULL,
137 new TextDiagnosticPrinter(llvm::errs(), DO));
138 DiagnosticsEngine &Diags = Clang->getDiagnostics();
139 Diags.setSuppressSystemWarnings(true);
140 Clang->createFileManager();
141 Clang->createSourceManager(Clang->getFileManager());
142 TargetOptions TO;
143 TO.Triple = llvm::sys::getHostTriple();
144 TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO);
145 Clang->setTarget(target);
146 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
147 LangStandard::lang_unspecified);
148 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
149 LangOptions &LO = Clang->getLangOpts();
150 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
151 HSO.ResourceDir = ResourceDir;
153 for (int i = 0; i < Includes.size(); ++i)
154 HSO.AddPath(Includes[i], frontend::Angled, true, false, false);
156 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
157 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
158 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
159 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
160 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
161 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
163 Clang->createPreprocessor();
164 Preprocessor &PP = Clang->getPreprocessor();
166 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), LO);
168 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
169 assert(file);
170 Clang->getSourceManager().createMainFileID(file);
172 Clang->createASTContext();
173 MyASTConsumer consumer;
174 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
176 Diags.getClient()->BeginSourceFile(LO, &PP);
177 ParseAST(*sema);
178 Diags.getClient()->EndSourceFile();
180 generate_python(consumer.types, consumer.functions);
182 delete sema;
183 delete Clang;
184 llvm::llvm_shutdown();
186 return 0;