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.
10  *
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.
15  *
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.
27  *
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  */
33 
34 #include "isl_config.h"
35 #undef PACKAGE
36 
37 #include <assert.h>
38 #include <iostream>
39 #include <stdlib.h>
40 #ifdef HAVE_ADT_OWNINGPTR_H
41 #include <llvm/ADT/OwningPtr.h>
42 #else
43 #include <memory>
44 #endif
45 #ifdef HAVE_LLVM_OPTION_ARG_H
46 #include <llvm/Option/Arg.h>
47 #endif
48 #include <llvm/Support/raw_ostream.h>
49 #include <llvm/Support/CommandLine.h>
50 #include <llvm/Support/Host.h>
51 #include <llvm/Support/ManagedStatic.h>
52 #include <clang/AST/ASTContext.h>
53 #include <clang/AST/ASTConsumer.h>
54 #include <clang/Basic/Builtins.h>
55 #include <clang/Basic/FileSystemOptions.h>
56 #include <clang/Basic/FileManager.h>
57 #include <clang/Basic/TargetOptions.h>
58 #include <clang/Basic/TargetInfo.h>
59 #include <clang/Basic/Version.h>
60 #include <clang/Driver/Compilation.h>
61 #include <clang/Driver/Driver.h>
62 #include <clang/Driver/Tool.h>
63 #include <clang/Frontend/CompilerInstance.h>
64 #include <clang/Frontend/CompilerInvocation.h>
65 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
66 #include <clang/Basic/DiagnosticOptions.h>
67 #else
68 #include <clang/Frontend/DiagnosticOptions.h>
69 #endif
70 #include <clang/Frontend/TextDiagnosticPrinter.h>
71 #include <clang/Frontend/Utils.h>
72 #include <clang/Lex/HeaderSearch.h>
73 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
74 #include <clang/Lex/PreprocessorOptions.h>
75 #else
76 #include <clang/Frontend/PreprocessorOptions.h>
77 #endif
78 #include <clang/Lex/Preprocessor.h>
79 #include <clang/Parse/ParseAST.h>
80 #include <clang/Sema/Sema.h>
81 
82 #include "extract_interface.h"
83 #include "generator.h"
84 #include "python.h"
85 #include "cpp.h"
86 #include "cpp_conversion.h"
87 
88 using namespace std;
89 using namespace clang;
90 using namespace clang::driver;
91 #ifdef HAVE_LLVM_OPTION_ARG_H
92 using namespace llvm::opt;
93 #endif
94 
95 #ifdef HAVE_ADT_OWNINGPTR_H
96 #define unique_ptr	llvm::OwningPtr
97 #endif
98 
99 static llvm::cl::opt<string> InputFilename(llvm::cl::Positional,
100 			llvm::cl::Required, llvm::cl::desc("<input file>"));
101 static llvm::cl::list<string> Includes("I",
102 			llvm::cl::desc("Header search path"),
103 			llvm::cl::value_desc("path"), llvm::cl::Prefix);
104 
105 static llvm::cl::opt<string> OutputLanguage(llvm::cl::Required,
106 	llvm::cl::ValueRequired, "language",
107 	llvm::cl::desc("Bindings to generate"),
108 	llvm::cl::value_desc("name"));
109 
110 static const char *ResourceDir =
111 	CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
112 
113 /* Does decl have an attribute of the following form?
114  *
115  *	__attribute__((annotate("name")))
116  */
has_annotation(Decl * decl,const char * name)117 bool has_annotation(Decl *decl, const char *name)
118 {
119 	if (!decl->hasAttrs())
120 		return false;
121 
122 	AttrVec attrs = decl->getAttrs();
123 	for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
124 		const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
125 		if (!ann)
126 			continue;
127 		if (ann->getAnnotation().str() == name)
128 			return true;
129 	}
130 
131 	return false;
132 }
133 
134 /* Is decl marked as exported?
135  */
is_exported(Decl * decl)136 static bool is_exported(Decl *decl)
137 {
138 	return has_annotation(decl, "isl_export");
139 }
140 
141 /* Collect all types and functions that are annotated "isl_export"
142  * in "exported_types" and "exported_function".  Collect all function
143  * declarations in "functions".
144  *
145  * We currently only consider single declarations.
146  */
147 struct MyASTConsumer : public ASTConsumer {
148 	set<RecordDecl *> exported_types;
149 	set<FunctionDecl *> exported_functions;
150 	set<FunctionDecl *> functions;
151 
HandleTopLevelDeclMyASTConsumer152 	virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef D) {
153 		Decl *decl;
154 
155 		if (!D.isSingleDecl())
156 			return HandleTopLevelDeclContinue;
157 		decl = D.getSingleDecl();
158 		if (isa<FunctionDecl>(decl))
159 			functions.insert(cast<FunctionDecl>(decl));
160 		if (!is_exported(decl))
161 			return HandleTopLevelDeclContinue;
162 		switch (decl->getKind()) {
163 		case Decl::Record:
164 			exported_types.insert(cast<RecordDecl>(decl));
165 			break;
166 		case Decl::Function:
167 			exported_functions.insert(cast<FunctionDecl>(decl));
168 			break;
169 		default:
170 			break;
171 		}
172 		return HandleTopLevelDeclContinue;
173 	}
174 };
175 
176 #ifdef USE_ARRAYREF
177 
178 #ifdef HAVE_CXXISPRODUCTION
construct_driver(const char * binary,DiagnosticsEngine & Diags)179 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
180 {
181 	return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
182 			    "", false, false, Diags);
183 }
184 #elif defined(HAVE_ISPRODUCTION)
construct_driver(const char * binary,DiagnosticsEngine & Diags)185 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
186 {
187 	return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
188 			    "", false, Diags);
189 }
190 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
construct_driver(const char * binary,DiagnosticsEngine & Diags)191 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
192 {
193 	return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
194 			    "", Diags);
195 }
196 #else
construct_driver(const char * binary,DiagnosticsEngine & Diags)197 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
198 {
199 	return new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags);
200 }
201 #endif
202 
203 namespace clang { namespace driver { class Job; } }
204 
205 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
206  * We fix this with a simple overloaded function here.
207  */
208 struct ClangAPI {
commandClangAPI209 	static Job *command(Job *J) { return J; }
commandClangAPI210 	static Job *command(Job &J) { return &J; }
commandClangAPI211 	static Command *command(Command &C) { return &C; }
212 };
213 
214 #ifdef CREATE_FROM_ARGS_TAKES_ARRAYREF
215 
216 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
217  * In this case, an ArrayRef<const char *>.
218  */
create_from_args(CompilerInvocation & invocation,const ArgStringList * args,DiagnosticsEngine & Diags)219 static void create_from_args(CompilerInvocation &invocation,
220 	const ArgStringList *args, DiagnosticsEngine &Diags)
221 {
222 	CompilerInvocation::CreateFromArgs(invocation, *args, Diags);
223 }
224 
225 #else
226 
227 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
228  * In this case, two "const char *" pointers.
229  */
create_from_args(CompilerInvocation & invocation,const ArgStringList * args,DiagnosticsEngine & Diags)230 static void create_from_args(CompilerInvocation &invocation,
231 	const ArgStringList *args, DiagnosticsEngine &Diags)
232 {
233 	CompilerInvocation::CreateFromArgs(invocation, args->data() + 1,
234 						args->data() + args->size(),
235 						Diags);
236 }
237 
238 #endif
239 
240 /* Create a CompilerInvocation object that stores the command line
241  * arguments constructed by the driver.
242  * The arguments are mainly useful for setting up the system include
243  * paths on newer clangs and on some platforms.
244  */
construct_invocation(const char * filename,DiagnosticsEngine & Diags)245 static CompilerInvocation *construct_invocation(const char *filename,
246 	DiagnosticsEngine &Diags)
247 {
248 	const char *binary = CLANG_PREFIX"/bin/clang";
249 	const unique_ptr<Driver> driver(construct_driver(binary, Diags));
250 	std::vector<const char *> Argv;
251 	Argv.push_back(binary);
252 	Argv.push_back(filename);
253 	const unique_ptr<Compilation> compilation(
254 		driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
255 	JobList &Jobs = compilation->getJobs();
256 
257 	Command *cmd = cast<Command>(ClangAPI::command(*Jobs.begin()));
258 	if (strcmp(cmd->getCreator().getName(), "clang"))
259 		return NULL;
260 
261 	const ArgStringList *args = &cmd->getArguments();
262 
263 	CompilerInvocation *invocation = new CompilerInvocation;
264 	create_from_args(*invocation, args, Diags);
265 	return invocation;
266 }
267 
268 #else
269 
construct_invocation(const char * filename,DiagnosticsEngine & Diags)270 static CompilerInvocation *construct_invocation(const char *filename,
271 	DiagnosticsEngine &Diags)
272 {
273 	return NULL;
274 }
275 
276 #endif
277 
278 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
279 
construct_printer(void)280 static TextDiagnosticPrinter *construct_printer(void)
281 {
282 	return new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
283 }
284 
285 #else
286 
construct_printer(void)287 static TextDiagnosticPrinter *construct_printer(void)
288 {
289 	DiagnosticOptions DO;
290 	return new TextDiagnosticPrinter(llvm::errs(), DO);
291 }
292 
293 #endif
294 
295 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
296 
create_target_info(CompilerInstance * Clang,DiagnosticsEngine & Diags)297 static TargetInfo *create_target_info(CompilerInstance *Clang,
298 	DiagnosticsEngine &Diags)
299 {
300 	shared_ptr<TargetOptions> TO = Clang->getInvocation().TargetOpts;
301 	TO->Triple = llvm::sys::getDefaultTargetTriple();
302 	return TargetInfo::CreateTargetInfo(Diags, TO);
303 }
304 
305 #elif defined(CREATETARGETINFO_TAKES_POINTER)
306 
create_target_info(CompilerInstance * Clang,DiagnosticsEngine & Diags)307 static TargetInfo *create_target_info(CompilerInstance *Clang,
308 	DiagnosticsEngine &Diags)
309 {
310 	TargetOptions &TO = Clang->getTargetOpts();
311 	TO.Triple = llvm::sys::getDefaultTargetTriple();
312 	return TargetInfo::CreateTargetInfo(Diags, &TO);
313 }
314 
315 #else
316 
create_target_info(CompilerInstance * Clang,DiagnosticsEngine & Diags)317 static TargetInfo *create_target_info(CompilerInstance *Clang,
318 	DiagnosticsEngine &Diags)
319 {
320 	TargetOptions &TO = Clang->getTargetOpts();
321 	TO.Triple = llvm::sys::getDefaultTargetTriple();
322 	return TargetInfo::CreateTargetInfo(Diags, TO);
323 }
324 
325 #endif
326 
327 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
328 
create_diagnostics(CompilerInstance * Clang)329 static void create_diagnostics(CompilerInstance *Clang)
330 {
331 	Clang->createDiagnostics(0, NULL, construct_printer());
332 }
333 
334 #else
335 
create_diagnostics(CompilerInstance * Clang)336 static void create_diagnostics(CompilerInstance *Clang)
337 {
338 	Clang->createDiagnostics(construct_printer());
339 }
340 
341 #endif
342 
343 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
344 
create_preprocessor(CompilerInstance * Clang)345 static void create_preprocessor(CompilerInstance *Clang)
346 {
347 	Clang->createPreprocessor(TU_Complete);
348 }
349 
350 #else
351 
create_preprocessor(CompilerInstance * Clang)352 static void create_preprocessor(CompilerInstance *Clang)
353 {
354 	Clang->createPreprocessor();
355 }
356 
357 #endif
358 
359 #ifdef ADDPATH_TAKES_4_ARGUMENTS
360 
add_path(HeaderSearchOptions & HSO,string Path)361 void add_path(HeaderSearchOptions &HSO, string Path)
362 {
363 	HSO.AddPath(Path, frontend::Angled, false, false);
364 }
365 
366 #else
367 
add_path(HeaderSearchOptions & HSO,string Path)368 void add_path(HeaderSearchOptions &HSO, string Path)
369 {
370 	HSO.AddPath(Path, frontend::Angled, true, false, false);
371 }
372 
373 #endif
374 
375 #ifdef HAVE_SETMAINFILEID
376 
create_main_file_id(SourceManager & SM,const FileEntry * file)377 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
378 {
379 	SM.setMainFileID(SM.createFileID(file, SourceLocation(),
380 					SrcMgr::C_User));
381 }
382 
383 #else
384 
create_main_file_id(SourceManager & SM,const FileEntry * file)385 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
386 {
387 	SM.createMainFileID(file);
388 }
389 
390 #endif
391 
392 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
393 
set_lang_defaults(CompilerInstance * Clang)394 static void set_lang_defaults(CompilerInstance *Clang)
395 {
396 	PreprocessorOptions &PO = Clang->getPreprocessorOpts();
397 	TargetOptions &TO = Clang->getTargetOpts();
398 	llvm::Triple T(TO.Triple);
399 	CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C, T, PO,
400 					    LangStandard::lang_unspecified);
401 }
402 
403 #else
404 
set_lang_defaults(CompilerInstance * Clang)405 static void set_lang_defaults(CompilerInstance *Clang)
406 {
407 	CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
408 					    LangStandard::lang_unspecified);
409 }
410 
411 #endif
412 
413 #ifdef SETINVOCATION_TAKES_SHARED_PTR
414 
set_invocation(CompilerInstance * Clang,CompilerInvocation * invocation)415 static void set_invocation(CompilerInstance *Clang,
416 	CompilerInvocation *invocation)
417 {
418 	Clang->setInvocation(std::make_shared<CompilerInvocation>(*invocation));
419 }
420 
421 #else
422 
set_invocation(CompilerInstance * Clang,CompilerInvocation * invocation)423 static void set_invocation(CompilerInstance *Clang,
424 	CompilerInvocation *invocation)
425 {
426 	Clang->setInvocation(invocation);
427 }
428 
429 #endif
430 
431 /* Helper function for ignore_error that only gets enabled if T
432  * (which is either const FileEntry * or llvm::ErrorOr<const FileEntry *>)
433  * has getError method, i.e., if it is llvm::ErrorOr<const FileEntry *>.
434  */
435 template <class T>
ignore_error_helper(const T obj,int,int[1][sizeof (obj.getError ())])436 static const FileEntry *ignore_error_helper(const T obj, int,
437 	int[1][sizeof(obj.getError())])
438 {
439 	return *obj;
440 }
441 
442 /* Helper function for ignore_error that is always enabled,
443  * but that only gets selected if the variant above is not enabled,
444  * i.e., if T is const FileEntry *.
445  */
446 template <class T>
ignore_error_helper(const T obj,long,void *)447 static const FileEntry *ignore_error_helper(const T obj, long, void *)
448 {
449 	return obj;
450 }
451 
452 /* Given either a const FileEntry * or a llvm::ErrorOr<const FileEntry *>,
453  * extract out the const FileEntry *.
454  */
455 template <class T>
ignore_error(const T obj)456 static const FileEntry *ignore_error(const T obj)
457 {
458 	return ignore_error_helper(obj, 0, NULL);
459 }
460 
461 /* Return the FileEntry corresponding to the given file name
462  * in the given compiler instances, ignoring any error.
463  */
getFile(CompilerInstance * Clang,std::string Filename)464 static const FileEntry *getFile(CompilerInstance *Clang, std::string Filename)
465 {
466 	return ignore_error(Clang->getFileManager().getFile(Filename));
467 }
468 
469 /* Create an interface generator for the selected language and
470  * then use it to generate the interface.
471  */
generate(MyASTConsumer & consumer,SourceManager & SM)472 static void generate(MyASTConsumer &consumer, SourceManager &SM)
473 {
474 	generator *gen;
475 
476 	if (OutputLanguage.compare("python") == 0) {
477 		gen = new python_generator(SM, consumer.exported_types,
478 			consumer.exported_functions, consumer.functions);
479 	} else if (OutputLanguage.compare("cpp") == 0) {
480 		gen = new cpp_generator(SM, consumer.exported_types,
481 			consumer.exported_functions, consumer.functions);
482 	} else if (OutputLanguage.compare("cpp-checked") == 0) {
483 		gen = new cpp_generator(SM, consumer.exported_types,
484 			consumer.exported_functions, consumer.functions, true);
485 	} else if (OutputLanguage.compare("cpp-checked-conversion") == 0) {
486 		gen = new cpp_conversion_generator(SM, consumer.exported_types,
487 			consumer.exported_functions, consumer.functions);
488 	} else {
489 		cerr << "Language '" << OutputLanguage
490 		     << "' not recognized." << endl
491 		     << "Not generating bindings." << endl;
492 		exit(EXIT_FAILURE);
493 	}
494 
495 	gen->generate();
496 }
497 
main(int argc,char * argv[])498 int main(int argc, char *argv[])
499 {
500 	llvm::cl::ParseCommandLineOptions(argc, argv);
501 
502 	CompilerInstance *Clang = new CompilerInstance();
503 	create_diagnostics(Clang);
504 	DiagnosticsEngine &Diags = Clang->getDiagnostics();
505 	Diags.setSuppressSystemWarnings(true);
506 	TargetInfo *target = create_target_info(Clang, Diags);
507 	Clang->setTarget(target);
508 	set_lang_defaults(Clang);
509 	CompilerInvocation *invocation =
510 		construct_invocation(InputFilename.c_str(), Diags);
511 	if (invocation)
512 		set_invocation(Clang, invocation);
513 	Clang->createFileManager();
514 	Clang->createSourceManager(Clang->getFileManager());
515 	HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
516 	LangOptions &LO = Clang->getLangOpts();
517 	PreprocessorOptions &PO = Clang->getPreprocessorOpts();
518 	HSO.ResourceDir = ResourceDir;
519 
520 	for (llvm::cl::list<string>::size_type i = 0; i < Includes.size(); ++i)
521 		add_path(HSO, Includes[i]);
522 
523 	PO.addMacroDef("__isl_give=__attribute__((annotate(\"isl_give\")))");
524 	PO.addMacroDef("__isl_keep=__attribute__((annotate(\"isl_keep\")))");
525 	PO.addMacroDef("__isl_take=__attribute__((annotate(\"isl_take\")))");
526 	PO.addMacroDef("__isl_export=__attribute__((annotate(\"isl_export\")))");
527 	PO.addMacroDef("__isl_overload="
528 	    "__attribute__((annotate(\"isl_overload\"))) "
529 	    "__attribute__((annotate(\"isl_export\")))");
530 	PO.addMacroDef("__isl_constructor=__attribute__((annotate(\"isl_constructor\"))) __attribute__((annotate(\"isl_export\")))");
531 	PO.addMacroDef("__isl_subclass(super)=__attribute__((annotate(\"isl_subclass(\" #super \")\"))) __attribute__((annotate(\"isl_export\")))");
532 
533 	create_preprocessor(Clang);
534 	Preprocessor &PP = Clang->getPreprocessor();
535 
536 	PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), LO);
537 
538 	const FileEntry *file = getFile(Clang, InputFilename);
539 	assert(file);
540 	create_main_file_id(Clang->getSourceManager(), file);
541 
542 	Clang->createASTContext();
543 	MyASTConsumer consumer;
544 	Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
545 
546 	Diags.getClient()->BeginSourceFile(LO, &PP);
547 	ParseAST(*sema);
548 	Diags.getClient()->EndSourceFile();
549 
550 	generate(consumer, Clang->getSourceManager());
551 
552 	delete sema;
553 	delete Clang;
554 	llvm::llvm_shutdown();
555 
556 	if (Diags.hasErrorOccurred())
557 		return EXIT_FAILURE;
558 	return EXIT_SUCCESS;
559 }
560