cpp: generate C++ wrapper classes
[isl.git] / interface / extract_interface.cc
blob64859053cdf0ddef2e1bd17e4ad320e1a9db29f1
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 "isl_config.h"
36 #include <assert.h>
37 #include <iostream>
38 #ifdef HAVE_ADT_OWNINGPTR_H
39 #include <llvm/ADT/OwningPtr.h>
40 #else
41 #include <memory>
42 #endif
43 #include <llvm/Support/raw_ostream.h>
44 #include <llvm/Support/CommandLine.h>
45 #include <llvm/Support/Host.h>
46 #include <llvm/Support/ManagedStatic.h>
47 #include <clang/AST/ASTContext.h>
48 #include <clang/AST/ASTConsumer.h>
49 #include <clang/Basic/FileSystemOptions.h>
50 #include <clang/Basic/FileManager.h>
51 #include <clang/Basic/TargetOptions.h>
52 #include <clang/Basic/TargetInfo.h>
53 #include <clang/Basic/Version.h>
54 #include <clang/Driver/Compilation.h>
55 #include <clang/Driver/Driver.h>
56 #include <clang/Driver/Tool.h>
57 #include <clang/Frontend/CompilerInstance.h>
58 #include <clang/Frontend/CompilerInvocation.h>
59 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
60 #include <clang/Basic/DiagnosticOptions.h>
61 #else
62 #include <clang/Frontend/DiagnosticOptions.h>
63 #endif
64 #include <clang/Frontend/TextDiagnosticPrinter.h>
65 #include <clang/Frontend/Utils.h>
66 #include <clang/Lex/HeaderSearch.h>
67 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
68 #include <clang/Lex/PreprocessorOptions.h>
69 #else
70 #include <clang/Frontend/PreprocessorOptions.h>
71 #endif
72 #include <clang/Lex/Preprocessor.h>
73 #include <clang/Parse/ParseAST.h>
74 #include <clang/Sema/Sema.h>
76 #include "extract_interface.h"
77 #include "generator.h"
78 #include "python.h"
79 #include "cpp.h"
81 using namespace std;
82 using namespace clang;
83 using namespace clang::driver;
85 #ifdef HAVE_ADT_OWNINGPTR_H
86 #define unique_ptr llvm::OwningPtr
87 #endif
89 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
90 llvm::cl::Required, llvm::cl::desc("<input file>"));
91 static llvm::cl::list<string> Includes("I",
92 llvm::cl::desc("Header search path"),
93 llvm::cl::value_desc("path"), llvm::cl::Prefix);
95 static llvm::cl::opt<string> Language(llvm::cl::Required,
96 llvm::cl::ValueRequired, "language",
97 llvm::cl::desc("Bindings to generate"),
98 llvm::cl::value_desc("name"));
100 static const char *ResourceDir =
101 CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
103 /* Does decl have an attribute of the following form?
105 * __attribute__((annotate("name")))
107 bool has_annotation(Decl *decl, const char *name)
109 if (!decl->hasAttrs())
110 return false;
112 AttrVec attrs = decl->getAttrs();
113 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
114 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
115 if (!ann)
116 continue;
117 if (ann->getAnnotation().str() == name)
118 return true;
121 return false;
124 /* Is decl marked as exported?
126 static bool is_exported(Decl *decl)
128 return has_annotation(decl, "isl_export");
131 /* Collect all types and functions that are annotated "isl_export"
132 * in "exported_types" and "exported_function". Collect all function
133 * declarations in "functions".
135 * We currently only consider single declarations.
137 struct MyASTConsumer : public ASTConsumer {
138 set<RecordDecl *> exported_types;
139 set<FunctionDecl *> exported_functions;
140 set<FunctionDecl *> functions;
142 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) {
143 Decl *decl;
145 if (!D.isSingleDecl())
146 return HandleTopLevelDeclContinue;
147 decl = D.getSingleDecl();
148 if (isa<FunctionDecl>(decl))
149 functions.insert(cast<FunctionDecl>(decl));
150 if (!is_exported(decl))
151 return HandleTopLevelDeclContinue;
152 switch (decl->getKind()) {
153 case Decl::Record:
154 exported_types.insert(cast<RecordDecl>(decl));
155 break;
156 case Decl::Function:
157 exported_functions.insert(cast<FunctionDecl>(decl));
158 break;
159 default:
160 break;
162 return HandleTopLevelDeclContinue;
166 #ifdef USE_ARRAYREF
168 #ifdef HAVE_CXXISPRODUCTION
169 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
171 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
172 "", false, false, Diags);
174 #elif defined(HAVE_ISPRODUCTION)
175 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
177 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
178 "", false, Diags);
180 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
181 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
183 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
184 "", Diags);
186 #else
187 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
189 return new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags);
191 #endif
193 namespace clang { namespace driver { class Job; } }
195 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
196 * We fix this with a simple overloaded function here.
198 struct ClangAPI {
199 static Job *command(Job *J) { return J; }
200 static Job *command(Job &J) { return &J; }
201 static Command *command(Command &C) { return &C; }
204 /* Create a CompilerInvocation object that stores the command line
205 * arguments constructed by the driver.
206 * The arguments are mainly useful for setting up the system include
207 * paths on newer clangs and on some platforms.
209 static CompilerInvocation *construct_invocation(const char *filename,
210 DiagnosticsEngine &Diags)
212 const char *binary = CLANG_PREFIX"/bin/clang";
213 const unique_ptr<Driver> driver(construct_driver(binary, Diags));
214 std::vector<const char *> Argv;
215 Argv.push_back(binary);
216 Argv.push_back(filename);
217 const unique_ptr<Compilation> compilation(
218 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
219 JobList &Jobs = compilation->getJobs();
221 Command *cmd = cast<Command>(ClangAPI::command(*Jobs.begin()));
222 if (strcmp(cmd->getCreator().getName(), "clang"))
223 return NULL;
225 const ArgStringList *args = &cmd->getArguments();
227 CompilerInvocation *invocation = new CompilerInvocation;
228 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
229 args->data() + args->size(),
230 Diags);
231 return invocation;
234 #else
236 static CompilerInvocation *construct_invocation(const char *filename,
237 DiagnosticsEngine &Diags)
239 return NULL;
242 #endif
244 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
246 static TextDiagnosticPrinter *construct_printer(void)
248 return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
251 #else
253 static TextDiagnosticPrinter *construct_printer(void)
255 DiagnosticOptions DO;
256 return new TextDiagnosticPrinter(llvm::errs(), DO);
259 #endif
261 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
263 static TargetInfo *create_target_info(CompilerInstance *Clang,
264 DiagnosticsEngine &Diags)
266 shared_ptr<TargetOptions> TO = Clang->getInvocation().TargetOpts;
267 TO->Triple = llvm::sys::getDefaultTargetTriple();
268 return TargetInfo::CreateTargetInfo(Diags, TO);
271 #elif defined(CREATETARGETINFO_TAKES_POINTER)
273 static TargetInfo *create_target_info(CompilerInstance *Clang,
274 DiagnosticsEngine &Diags)
276 TargetOptions &TO = Clang->getTargetOpts();
277 TO.Triple = llvm::sys::getDefaultTargetTriple();
278 return TargetInfo::CreateTargetInfo(Diags, &TO);
281 #else
283 static TargetInfo *create_target_info(CompilerInstance *Clang,
284 DiagnosticsEngine &Diags)
286 TargetOptions &TO = Clang->getTargetOpts();
287 TO.Triple = llvm::sys::getDefaultTargetTriple();
288 return TargetInfo::CreateTargetInfo(Diags, TO);
291 #endif
293 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
295 static void create_diagnostics(CompilerInstance *Clang)
297 Clang->createDiagnostics(0, NULL, construct_printer());
300 #else
302 static void create_diagnostics(CompilerInstance *Clang)
304 Clang->createDiagnostics(construct_printer());
307 #endif
309 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
311 static void create_preprocessor(CompilerInstance *Clang)
313 Clang->createPreprocessor(TU_Complete);
316 #else
318 static void create_preprocessor(CompilerInstance *Clang)
320 Clang->createPreprocessor();
323 #endif
325 #ifdef ADDPATH_TAKES_4_ARGUMENTS
327 void add_path(HeaderSearchOptions &HSO, string Path)
329 HSO.AddPath(Path, frontend::Angled, false, false);
332 #else
334 void add_path(HeaderSearchOptions &HSO, string Path)
336 HSO.AddPath(Path, frontend::Angled, true, false, false);
339 #endif
341 #ifdef HAVE_SETMAINFILEID
343 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
345 SM.setMainFileID(SM.createFileID(file, SourceLocation(),
346 SrcMgr::C_User));
349 #else
351 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
353 SM.createMainFileID(file);
356 #endif
358 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
360 static void set_lang_defaults(CompilerInstance *Clang)
362 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
363 TargetOptions &TO = Clang->getTargetOpts();
364 llvm::Triple T(TO.Triple);
365 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C, T, PO,
366 LangStandard::lang_unspecified);
369 #else
371 static void set_lang_defaults(CompilerInstance *Clang)
373 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
374 LangStandard::lang_unspecified);
377 #endif
379 #ifdef SETINVOCATION_TAKES_SHARED_PTR
381 static void set_invocation(CompilerInstance *Clang,
382 CompilerInvocation *invocation)
384 Clang->setInvocation(std::make_shared<CompilerInvocation>(*invocation));
387 #else
389 static void set_invocation(CompilerInstance *Clang,
390 CompilerInvocation *invocation)
392 Clang->setInvocation(invocation);
395 #endif
397 int main(int argc, char *argv[])
399 llvm::cl::ParseCommandLineOptions(argc, argv);
401 CompilerInstance *Clang = new CompilerInstance();
402 create_diagnostics(Clang);
403 DiagnosticsEngine &Diags = Clang->getDiagnostics();
404 Diags.setSuppressSystemWarnings(true);
405 CompilerInvocation *invocation =
406 construct_invocation(InputFilename.c_str(), Diags);
407 if (invocation)
408 set_invocation(Clang, invocation);
409 Clang->createFileManager();
410 Clang->createSourceManager(Clang->getFileManager());
411 TargetInfo *target = create_target_info(Clang, Diags);
412 Clang->setTarget(target);
413 set_lang_defaults(Clang);
414 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
415 LangOptions &LO = Clang->getLangOpts();
416 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
417 HSO.ResourceDir = ResourceDir;
419 for (llvm::cl::list<string>::size_type i = 0; i < Includes.size(); ++i)
420 add_path(HSO, Includes[i]);
422 PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
423 PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
424 PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
425 PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
426 PO.addMacroDef("__isl_overload="
427 "__attribute__((annotate(\"isl_overload\"))) "
428 "__attribute__((annotate(\"isl_export\")))");
429 PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
430 PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
432 create_preprocessor(Clang);
433 Preprocessor &PP = Clang->getPreprocessor();
435 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), LO);
437 const FileEntry *file = Clang->getFileManager().getFile(InputFilename);
438 assert(file);
439 create_main_file_id(Clang->getSourceManager(), file);
441 Clang->createASTContext();
442 MyASTConsumer consumer;
443 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
445 Diags.getClient()->BeginSourceFile(LO, &PP);
446 ParseAST(*sema);
447 Diags.getClient()->EndSourceFile();
449 generator *gen = NULL;
450 if (Language.compare("python") == 0)
451 gen = new python_generator(consumer.exported_types,
452 consumer.exported_functions, consumer.functions);
453 else if (Language.compare("cpp") == 0)
454 gen = new cpp_generator(consumer.exported_types,
455 consumer.functions);
456 else
457 cerr << "Language '" << Language << "' not recognized." << endl
458 << "Not generating bindings." << endl;
460 if (gen)
461 gen->generate();
463 delete sema;
464 delete Clang;
465 llvm::llvm_shutdown();
467 return 0;