1 /*
2  * Copyright 2011,2015 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 <stdio.h>
35 #include <string.h>
36 #include <algorithm>
37 #include <iostream>
38 
39 #include <clang/AST/Attr.h>
40 #include <clang/Basic/SourceManager.h>
41 
42 #include "isl_config.h"
43 #include "extract_interface.h"
44 #include "generator.h"
45 
46 const char *isl_class::get_prefix = "get_";
47 const char *isl_class::set_callback_prefix = "set_";
48 
49 /* Should "method" be considered to be a static method?
50  * That is, is the first argument something other than
51  * an instance of the class?
52  */
is_static(FunctionDecl * method) const53 bool isl_class::is_static(FunctionDecl *method) const
54 {
55 	ParmVarDecl *param;
56 	QualType type;
57 
58 	if (method->getNumParams() < 1)
59 		return true;
60 
61 	param = method->getParamDecl(0);
62 	type = param->getOriginalType();
63 	if (!generator::is_isl_type(type))
64 		return true;
65 	return generator::extract_type(type) != name;
66 }
67 
68 /* Should "method" be considered to be a static method?
69  * That is, is the first argument something other than
70  * an instance of the class?
71  */
is_static(const isl_class & clazz,FunctionDecl * method)72 bool generator::is_static(const isl_class &clazz, FunctionDecl *method)
73 {
74 	return clazz.is_static(method);
75 }
76 
77 /* Does "fd" modify an object of "clazz"?
78  * That is, is it an object method that takes the object and
79  * returns (gives) an object of the same type?
80  */
is_mutator(const isl_class & clazz,FunctionDecl * fd)81 bool generator::is_mutator(const isl_class &clazz, FunctionDecl *fd)
82 {
83 	ParmVarDecl *param;
84 	QualType type, return_type;
85 
86 	if (fd->getNumParams() < 1)
87 		return false;
88 	if (is_static(clazz, fd))
89 		return false;
90 
91 	if (!gives(fd))
92 		return false;
93 	param = fd->getParamDecl(0);
94 	if (!takes(param))
95 		return false;
96 	type = param->getOriginalType();
97 	return_type = fd->getReturnType();
98 	return return_type == type;
99 }
100 
101 /* Find the FunctionDecl with name "name",
102  * returning NULL if there is no such FunctionDecl.
103  * If "required" is set, then error out if no FunctionDecl can be found.
104  */
find_by_name(const string & name,bool required)105 FunctionDecl *generator::find_by_name(const string &name, bool required)
106 {
107 	map<string, FunctionDecl *>::iterator i;
108 
109 	i = functions_by_name.find(name);
110 	if (i != functions_by_name.end())
111 		return i->second;
112 	if (required)
113 		die("No " + name + " function found");
114 	return NULL;
115 }
116 
117 /* List of conversion functions that are used to automatically convert
118  * the second argument of the conversion function to its function result.
119  */
120 const std::set<std::string> generator::automatic_conversion_functions = {
121 	"isl_id_read_from_str",
122 	"isl_val_int_from_si",
123 };
124 
125 /* Extract information about the automatic conversion function "fd",
126  * storing the results in this->conversions.
127  *
128  * A function used for automatic conversion has exactly two arguments,
129  * an isl_ctx and a non-isl object, and it returns an isl object.
130  * Store a mapping from the isl object return type
131  * to the non-isl object source type.
132  */
extract_automatic_conversion(FunctionDecl * fd)133 void generator::extract_automatic_conversion(FunctionDecl *fd)
134 {
135 	QualType return_type = fd->getReturnType();
136 	const Type *type = return_type.getTypePtr();
137 
138 	if (fd->getNumParams() != 2)
139 		die("Expecting two arguments");
140 	if (!is_isl_ctx(fd->getParamDecl(0)->getOriginalType()))
141 		die("Expecting isl_ctx first argument");
142 	if (!is_isl_type(return_type))
143 		die("Expecting isl object return type");
144 	conversions[type] = fd->getParamDecl(1);
145 }
146 
147 /* Extract information about all automatic conversion functions
148  * for the given class, storing the results in this->conversions.
149  *
150  * In particular, look through all exported constructors for the class and
151  * check if any of them is explicitly marked as a conversion function.
152  */
extract_class_automatic_conversions(const isl_class & clazz)153 void generator::extract_class_automatic_conversions(const isl_class &clazz)
154 {
155 	const function_set &constructors = clazz.constructors;
156 	function_set::iterator fi;
157 
158 	for (fi = constructors.begin(); fi != constructors.end(); ++fi) {
159 		FunctionDecl *fd = *fi;
160 		string name = fd->getName();
161 		if (automatic_conversion_functions.count(name) != 0)
162 			extract_automatic_conversion(fd);
163 	}
164 }
165 
166 /* Extract information about all automatic conversion functions,
167  * storing the results in this->conversions.
168  */
extract_automatic_conversions()169 void generator::extract_automatic_conversions()
170 {
171 	map<string, isl_class>::iterator ci;
172 
173 	for (ci = classes.begin(); ci != classes.end(); ++ci)
174 		extract_class_automatic_conversions(ci->second);
175 }
176 
177 /* Add a subclass derived from "decl" called "sub_name" to the set of classes,
178  * keeping track of the _to_str, _copy and _free functions, if any, separately.
179  * "sub_name" is either the name of the class itself or
180  * the name of a type based subclass.
181  * If the class is a proper subclass, then "super_name" is the name
182  * of its immediate superclass.
183  */
add_subclass(RecordDecl * decl,const string & super_name,const string & sub_name)184 void generator::add_subclass(RecordDecl *decl, const string &super_name,
185 	const string &sub_name)
186 {
187 	string name = decl->getName();
188 
189 	classes[sub_name].name = name;
190 	classes[sub_name].superclass_name = super_name;
191 	classes[sub_name].subclass_name = sub_name;
192 	classes[sub_name].type = decl;
193 	classes[sub_name].fn_to_str = find_by_name(name + "_to_str", false);
194 	classes[sub_name].fn_copy = find_by_name(name + "_copy", true);
195 	classes[sub_name].fn_free = find_by_name(name + "_free", true);
196 }
197 
198 /* Add a class derived from "decl" to the set of classes,
199  * keeping track of the _to_str, _copy and _free functions, if any, separately.
200  */
add_class(RecordDecl * decl)201 void generator::add_class(RecordDecl *decl)
202 {
203 	return add_subclass(decl, "", decl->getName());
204 }
205 
206 /* Given a function "fn_type" that returns the subclass type
207  * of a C object, create subclasses for each of the (non-negative)
208  * return values.
209  *
210  * The function "fn_type" is also stored in the superclass,
211  * along with all pairs of type values and subclass names.
212  */
add_type_subclasses(FunctionDecl * fn_type)213 void generator::add_type_subclasses(FunctionDecl *fn_type)
214 {
215 	QualType return_type = fn_type->getReturnType();
216 	const EnumType *enum_type = return_type->getAs<EnumType>();
217 	EnumDecl *decl = enum_type->getDecl();
218 	isl_class *c = method2class(fn_type);
219 	DeclContext::decl_iterator i;
220 
221 	c->fn_type = fn_type;
222 	for (i = decl->decls_begin(); i != decl->decls_end(); ++i) {
223 		EnumConstantDecl *ecd = dyn_cast<EnumConstantDecl>(*i);
224 		int val = (int) ecd->getInitVal().getSExtValue();
225 		string name = ecd->getNameAsString();
226 
227 		if (val < 0)
228 			continue;
229 		c->type_subclasses[val] = name;
230 		add_subclass(c->type, c->subclass_name, name);
231 	}
232 }
233 
234 /* Add information about the enum values in "decl", set by "fd",
235  * to c->set_enums. "prefix" is the prefix of the generated method names.
236  * In particular, it has the name of the enum type removed.
237  *
238  * In particular, for each non-negative enum value, keep track of
239  * the value, the name and the corresponding method name.
240  */
add_set_enum(isl_class * c,const string & prefix,EnumDecl * decl,FunctionDecl * fd)241 static void add_set_enum(isl_class *c, const string &prefix, EnumDecl *decl,
242 	FunctionDecl *fd)
243 {
244 	DeclContext::decl_iterator i;
245 
246 	for (i = decl->decls_begin(); i != decl->decls_end(); ++i) {
247 		EnumConstantDecl *ecd = dyn_cast<EnumConstantDecl>(*i);
248 		int val = (int) ecd->getInitVal().getSExtValue();
249 		string name = ecd->getNameAsString();
250 		string method_name;
251 
252 		if (val < 0)
253 			continue;
254 		method_name = prefix + name.substr(4);
255 		c->set_enums[fd].push_back(set_enum(val, name, method_name));
256 	}
257 }
258 
259 /* Check if "fd" sets an enum value and, if so, add information
260  * about the enum values to c->set_enums.
261  *
262  * A function is considered to set an enum value if:
263  * - the function returns an object of the same type
264  * - the last argument is of type enum
265  * - the name of the function ends with the name of the enum
266  */
handled_sets_enum(isl_class * c,FunctionDecl * fd)267 static bool handled_sets_enum(isl_class *c, FunctionDecl *fd)
268 {
269 	unsigned n;
270 	ParmVarDecl *param;
271 	const EnumType *enum_type;
272 	EnumDecl *decl;
273 	string enum_name;
274 	string fd_name;
275 	string prefix;
276 	size_t pos;
277 
278 	if (!generator::is_mutator(*c, fd))
279 		return false;
280 	n = fd->getNumParams();
281 	if (n < 2)
282 		return false;
283 	param = fd->getParamDecl(n - 1);
284 	enum_type = param->getType()->getAs<EnumType>();
285 	if (!enum_type)
286 		return false;
287 	decl = enum_type->getDecl();
288 	enum_name = decl->getName();
289 	enum_name = enum_name.substr(4);
290 	fd_name = c->method_name(fd);
291 	pos = fd_name.find(enum_name);
292 	if (pos == std::string::npos)
293 		return false;
294 	prefix = fd_name.substr(0, pos);
295 
296 	add_set_enum(c, prefix, decl, fd);
297 
298 	return true;
299 }
300 
301 /* Return the callback argument of a function setting
302  * a persistent callback.
303  * This callback is in the second argument (position 1).
304  */
persistent_callback_arg(FunctionDecl * fd)305 ParmVarDecl *generator::persistent_callback_arg(FunctionDecl *fd)
306 {
307 	return fd->getParamDecl(1);
308 }
309 
310 /* Does the given function set a persistent callback?
311  * The following heuristics are used to determine this property:
312  * - the function returns an object of the same type
313  * - its name starts with "set_"
314  * - it has exactly three arguments
315  * - the second (position 1) of which is a callback
316  */
sets_persistent_callback(isl_class * c,FunctionDecl * fd)317 static bool sets_persistent_callback(isl_class *c, FunctionDecl *fd)
318 {
319 	ParmVarDecl *param;
320 
321 	if (!generator::is_mutator(*c, fd))
322 		return false;
323 	if (fd->getNumParams() != 3)
324 		return false;
325 	param = generator::persistent_callback_arg(fd);
326 	if (!generator::is_callback(param->getType()))
327 		return false;
328 	return prefixcmp(c->method_name(fd).c_str(),
329 			 c->set_callback_prefix) == 0;
330 }
331 
332 /* Sorting function that places declaration of functions
333  * with a shorter name first.
334  */
less_name(const FunctionDecl * a,const FunctionDecl * b)335 static bool less_name(const FunctionDecl *a, const FunctionDecl *b)
336 {
337 	return a->getName().size() < b->getName().size();
338 }
339 
340 /* Collect all functions that belong to a certain type, separating
341  * constructors from methods that set persistent callback and
342  * from regular methods, while keeping track of the _to_str,
343  * _copy and _free functions, if any, separately.  If there are any overloaded
344  * functions, then they are grouped based on their name after removing the
345  * argument type suffix.
346  * Check for functions that describe subclasses before considering
347  * any other functions in order to be able to detect those other
348  * functions as belonging to the subclasses.
349  * Sort the names of the functions based on their lengths
350  * to ensure that nested subclasses are handled later.
351  */
generator(SourceManager & SM,set<RecordDecl * > & exported_types,set<FunctionDecl * > exported_functions,set<FunctionDecl * > functions)352 generator::generator(SourceManager &SM, set<RecordDecl *> &exported_types,
353 	set<FunctionDecl *> exported_functions, set<FunctionDecl *> functions) :
354 	SM(SM)
355 {
356 	set<FunctionDecl *>::iterator in;
357 	set<RecordDecl *>::iterator it;
358 	vector<FunctionDecl *> type_subclasses;
359 	vector<FunctionDecl *>::iterator iv;
360 
361 	for (in = functions.begin(); in != functions.end(); ++in) {
362 		FunctionDecl *decl = *in;
363 		functions_by_name[decl->getName()] = decl;
364 	}
365 
366 	for (it = exported_types.begin(); it != exported_types.end(); ++it)
367 		add_class(*it);
368 
369 	for (in = exported_functions.begin(); in != exported_functions.end();
370 	     ++in) {
371 		if (is_subclass(*in))
372 			type_subclasses.push_back(*in);
373 	}
374 	std::sort(type_subclasses.begin(), type_subclasses.end(), &less_name);
375 	for (iv = type_subclasses.begin(); iv != type_subclasses.end(); ++iv) {
376 		add_type_subclasses(*iv);
377 	}
378 
379 	for (in = exported_functions.begin(); in != exported_functions.end();
380 	     ++in) {
381 		FunctionDecl *method = *in;
382 		isl_class *c;
383 
384 		if (is_subclass(method))
385 			continue;
386 
387 		c = method2class(method);
388 		if (!c)
389 			continue;
390 		if (is_constructor(method)) {
391 			c->constructors.insert(method);
392 		} else if (handled_sets_enum(c, method)) {
393 		} else if (sets_persistent_callback(c, method)) {
394 			c->persistent_callbacks.insert(method);
395 		} else {
396 			string fullname = c->name_without_type_suffixes(method);
397 			c->methods[fullname].insert(method);
398 		}
399 	}
400 
401 	extract_automatic_conversions();
402 }
403 
404 /* Print error message "msg" and abort.
405  */
die(const char * msg)406 void generator::die(const char *msg)
407 {
408 	fprintf(stderr, "%s\n", msg);
409 	abort();
410 }
411 
412 /* Print error message "msg" and abort.
413  */
die(string msg)414 void generator::die(string msg)
415 {
416 	die(msg.c_str());
417 }
418 
419 /* Return a sequence of the types of which the given type declaration is
420  * marked as being a subtype.
421  * The order of the types is the opposite of the order in which they
422  * appear in the source.  In particular, the first annotation
423  * is the one that is closest to the annotated type and the corresponding
424  * type is then also the first that will appear in the sequence of types.
425  * This is also the order in which the annotations appear
426  * in the AttrVec returned by Decl::getAttrs() in older versions of clang.
427  * In newer versions of clang, the order is that in which
428  * the attribute appears in the source.
429  * Use the position of the "isl_export" attribute to determine
430  * whether this is an old (with reversed order) or a new version.
431  * The "isl_export" attribute is automatically added
432  * after each "isl_subclass" attribute.  If it appears in the list before
433  * any "isl_subclass" is encountered, then this must be a reversed list.
434  */
find_superclasses(Decl * decl)435 std::vector<string> generator::find_superclasses(Decl *decl)
436 {
437 	vector<string> super;
438 	bool reversed = false;
439 
440 	if (!decl->hasAttrs())
441 		return super;
442 
443 	string sub = "isl_subclass";
444 	size_t len = sub.length();
445 	AttrVec attrs = decl->getAttrs();
446 	for (AttrVec::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
447 		const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
448 		if (!ann)
449 			continue;
450 		string s = ann->getAnnotation().str();
451 		if (s == "isl_export" && super.size() == 0)
452 			reversed = true;
453 		if (s.substr(0, len) == sub) {
454 			s = s.substr(len + 1, s.length() - len  - 2);
455 			if (reversed)
456 				super.push_back(s);
457 			else
458 				super.insert(super.begin(), s);
459 		}
460 	}
461 
462 	return super;
463 }
464 
465 /* Is "decl" marked as describing subclasses?
466  */
is_subclass(FunctionDecl * decl)467 bool generator::is_subclass(FunctionDecl *decl)
468 {
469 	return find_superclasses(decl).size() > 0;
470 }
471 
472 /* Is decl marked as being part of an overloaded method?
473  */
is_overload(Decl * decl)474 bool generator::is_overload(Decl *decl)
475 {
476 	return has_annotation(decl, "isl_overload");
477 }
478 
479 /* Is decl marked as a constructor?
480  */
is_constructor(Decl * decl)481 bool generator::is_constructor(Decl *decl)
482 {
483 	return has_annotation(decl, "isl_constructor");
484 }
485 
486 /* Is decl marked as consuming a reference?
487  */
takes(Decl * decl)488 bool generator::takes(Decl *decl)
489 {
490 	return has_annotation(decl, "isl_take");
491 }
492 
493 /* Is decl marked as preserving a reference?
494  */
keeps(Decl * decl)495 bool generator::keeps(Decl *decl)
496 {
497 	return has_annotation(decl, "isl_keep");
498 }
499 
500 /* Is decl marked as returning a reference that is required to be freed.
501  */
gives(Decl * decl)502 bool generator::gives(Decl *decl)
503 {
504 	return has_annotation(decl, "isl_give");
505 }
506 
507 /* Return the class that has a name that best matches the initial part
508  * of the name of function "fd" or NULL if no such class could be found.
509  */
method2class(FunctionDecl * fd)510 isl_class *generator::method2class(FunctionDecl *fd)
511 {
512 	string best;
513 	map<string, isl_class>::iterator ci;
514 	string name = fd->getNameAsString();
515 
516 	for (ci = classes.begin(); ci != classes.end(); ++ci) {
517 		size_t len = ci->first.length();
518 		if (len > best.length() && name.substr(0, len) == ci->first &&
519 		    name[len] == '_')
520 			best = ci->first;
521 	}
522 
523 	if (classes.find(best) == classes.end()) {
524 		cerr << "Unable to find class of " << name << endl;
525 		return NULL;
526 	}
527 
528 	return &classes[best];
529 }
530 
531 /* Is "type" the type "isl_ctx *"?
532  */
is_isl_ctx(QualType type)533 bool generator::is_isl_ctx(QualType type)
534 {
535 	if (!type->isPointerType())
536 		return false;
537 	type = type->getPointeeType();
538 	if (type.getAsString() != "isl_ctx")
539 		return false;
540 
541 	return true;
542 }
543 
544 /* Is the first argument of "fd" of type "isl_ctx *"?
545  */
first_arg_is_isl_ctx(FunctionDecl * fd)546 bool generator::first_arg_is_isl_ctx(FunctionDecl *fd)
547 {
548 	ParmVarDecl *param;
549 
550 	if (fd->getNumParams() < 1)
551 		return false;
552 
553 	param = fd->getParamDecl(0);
554 	return is_isl_ctx(param->getOriginalType());
555 }
556 
557 namespace {
558 
559 struct ClangAPI {
560 	/* Return the first location in the range returned by
561 	 * clang::SourceManager::getImmediateExpansionRange.
562 	 * Older versions of clang return a pair of SourceLocation objects.
563 	 * More recent versions return a CharSourceRange.
564 	 */
range_begin__anon478103060111::ClangAPI565 	static SourceLocation range_begin(
566 			const std::pair<SourceLocation,SourceLocation> &p) {
567 		return p.first;
568 	}
range_begin__anon478103060111::ClangAPI569 	static SourceLocation range_begin(const CharSourceRange &range) {
570 		return range.getBegin();
571 	}
572 };
573 
574 }
575 
576 /* Does the callback argument "param" take its argument at position "pos"?
577  *
578  * The memory management annotations of arguments to function pointers
579  * are not recorded by clang, so the information cannot be extracted
580  * from the type of "param".
581  * Instead, go to the location in the source where the callback argument
582  * is declared, look for the right argument of the callback itself and
583  * then check if it has an "__isl_take" memory management annotation.
584  *
585  * If the return value of the function has a memory management annotation,
586  * then the spelling of "param" will point to the spelling
587  * of this memory management annotation.  Since the macro is defined
588  * on the command line (in main), this location does not have a file entry.
589  * In this case, move up one level in the macro expansion to the location
590  * where the memory management annotation is used.
591  */
callback_takes_argument(ParmVarDecl * param,int pos)592 bool generator::callback_takes_argument(ParmVarDecl *param,
593 	int pos)
594 {
595 	SourceLocation loc;
596 	const char *s, *end, *next;
597 	bool takes, keeps;
598 
599 	loc = param->getSourceRange().getBegin();
600 	if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(loc))))
601 		loc = ClangAPI::range_begin(SM.getImmediateExpansionRange(loc));
602 	s = SM.getCharacterData(loc);
603 	if (!s)
604 		die("No character data");
605 	s = strchr(s, '(');
606 	if (!s)
607 		die("Cannot find function pointer");
608 	s = strchr(s + 1, '(');
609 	if (!s)
610 		die("Cannot find function pointer arguments");
611 	end = strchr(s + 1, ')');
612 	if (!end)
613 		die("Cannot find end of function pointer arguments");
614 	while (pos-- > 0) {
615 		s = strchr(s + 1, ',');
616 		if (!s || s > end)
617 			die("Cannot find function pointer argument");
618 	}
619 	next = strchr(s + 1, ',');
620 	if (next && next < end)
621 		end = next;
622 	s = strchr(s + 1, '_');
623 	if (!s || s > end)
624 		die("Cannot find function pointer argument annotation");
625 	takes = prefixcmp(s, "__isl_take") == 0;
626 	keeps = prefixcmp(s, "__isl_keep") == 0;
627 	if (!takes && !keeps)
628 		die("Cannot find function pointer argument annotation");
629 
630 	return takes;
631 }
632 
633 /* Is "type" that of a pointer to an isl_* structure?
634  */
is_isl_type(QualType type)635 bool generator::is_isl_type(QualType type)
636 {
637 	if (type->isPointerType()) {
638 		string s;
639 
640 		type = type->getPointeeType();
641 		if (type->isFunctionType())
642 			return false;
643 		s = type.getAsString();
644 		return s.substr(0, 4) == "isl_";
645 	}
646 
647 	return false;
648 }
649 
650 /* Is "type" one of the integral types with a negative value
651  * indicating an error condition?
652  */
is_isl_neg_error(QualType type)653 bool generator::is_isl_neg_error(QualType type)
654 {
655 	return is_isl_bool(type) || is_isl_stat(type) || is_isl_size(type);
656 }
657 
658 /* Is "type" the primitive type with the given name?
659  */
is_isl_primitive(QualType type,const char * name)660 static bool is_isl_primitive(QualType type, const char *name)
661 {
662 	string s;
663 
664 	if (type->isPointerType())
665 		return false;
666 
667 	s = type.getAsString();
668 	return s == name;
669 }
670 
671 /* Is "type" the type isl_bool?
672  */
is_isl_bool(QualType type)673 bool generator::is_isl_bool(QualType type)
674 {
675 	return is_isl_primitive(type, "isl_bool");
676 }
677 
678 /* Is "type" the type isl_stat?
679  */
is_isl_stat(QualType type)680 bool generator::is_isl_stat(QualType type)
681 {
682 	return is_isl_primitive(type, "isl_stat");
683 }
684 
685 /* Is "type" the type isl_size?
686  */
is_isl_size(QualType type)687 bool generator::is_isl_size(QualType type)
688 {
689 	return is_isl_primitive(type, "isl_size");
690 }
691 
692 /* Is "type" that of a pointer to a function?
693  */
is_callback(QualType type)694 bool generator::is_callback(QualType type)
695 {
696 	if (!type->isPointerType())
697 		return false;
698 	type = type->getPointeeType();
699 	return type->isFunctionType();
700 }
701 
702 /* Is "type" that of "char *" of "const char *"?
703  */
is_string(QualType type)704 bool generator::is_string(QualType type)
705 {
706 	if (type->isPointerType()) {
707 		string s = type->getPointeeType().getAsString();
708 		return s == "const char" || s == "char";
709 	}
710 
711 	return false;
712 }
713 
714 /* Is "type" that of "long"?
715  */
is_long(QualType type)716 bool generator::is_long(QualType type)
717 {
718 	const BuiltinType *builtin = type->getAs<BuiltinType>();
719 	return builtin && builtin->getKind() == BuiltinType::Long;
720 }
721 
722 /* Is "type" that of "unsigned int"?
723  */
is_unsigned_int(QualType type)724 static bool is_unsigned_int(QualType type)
725 {
726 	const BuiltinType *builtin = type->getAs<BuiltinType>();
727 	return builtin && builtin->getKind() == BuiltinType::UInt;
728 }
729 
730 /* Return the name of the type that "type" points to.
731  * The input "type" is assumed to be a pointer type.
732  */
extract_type(QualType type)733 string generator::extract_type(QualType type)
734 {
735 	if (type->isPointerType())
736 		return type->getPointeeType().getAsString();
737 	die("Cannot extract type from non-pointer type");
738 }
739 
740 /* Given the type of a function pointer, return the corresponding
741  * function prototype.
742  */
extract_prototype(QualType type)743 const FunctionProtoType *generator::extract_prototype(QualType type)
744 {
745 	return type->getPointeeType()->getAs<FunctionProtoType>();
746 }
747 
748 /* Return the function name suffix for the type of "param".
749  *
750  * If the type of "param" is an isl object type,
751  * then the suffix is the name of the type with the "isl" prefix removed,
752  * but keeping the "_".
753  * If the type is an unsigned integer, then the type suffix is "_ui".
754  */
type_suffix(ParmVarDecl * param)755 static std::string type_suffix(ParmVarDecl *param)
756 {
757 	QualType type;
758 
759 	type = param->getOriginalType();
760 	if (generator::is_isl_type(type))
761 		return generator::extract_type(type).substr(3);
762 	else if (is_unsigned_int(type))
763 		return "_ui";
764 	generator::die("Unsupported type suffix");
765 }
766 
767 /* If "suffix" is a suffix of "s", then return "s" with the suffix removed.
768  * Otherwise, simply return "s".
769  */
drop_suffix(const std::string & s,const std::string & suffix)770 static std::string drop_suffix(const std::string &s, const std::string &suffix)
771 {
772 	size_t len, suffix_len;
773 
774 	len = s.length();
775 	suffix_len = suffix.length();
776 
777 	if (len >= suffix_len && s.substr(len - suffix_len) == suffix)
778 		return s.substr(0, len - suffix_len);
779 	else
780 		return s;
781 }
782 
783 /* If "method" is overloaded, then return its name with the suffixes
784  * corresponding to the types of the final arguments removed.
785  * Otherwise, simply return the name of the function.
786  * Start from the final argument and keep removing suffixes
787  * matching arguments, independently of whether previously considered
788  * arguments matched.
789  */
name_without_type_suffixes(FunctionDecl * method)790 string isl_class::name_without_type_suffixes(FunctionDecl *method)
791 {
792 	int num_params;
793 	string name;
794 
795 	name = method->getName();
796 	if (!generator::is_overload(method))
797 		return name;
798 
799 	num_params = method->getNumParams();
800 	for (int i = num_params - 1; i >= 0; --i) {
801 		ParmVarDecl *param;
802 		string type;
803 
804 		param = method->getParamDecl(i);
805 		type = type_suffix(param);
806 
807 		name = drop_suffix(name, type);
808 	}
809 
810 	return name;
811 }
812 
813 /* Is function "fd" with the given name a "get" method?
814  *
815  * A "get" method is an instance method
816  * with a name that starts with the get method prefix.
817  */
is_get_method_name(FunctionDecl * fd,const string & name) const818 bool isl_class::is_get_method_name(FunctionDecl *fd, const string &name) const
819 {
820 	return !is_static(fd) && prefixcmp(name.c_str(), get_prefix) == 0;
821 }
822 
823 /* Extract the method name corresponding to "fd".
824  *
825  * If "fd" is a "get" method, then drop the "get" method prefix.
826  */
method_name(FunctionDecl * fd) const827 string isl_class::method_name(FunctionDecl *fd) const
828 {
829       string base = base_method_name(fd);
830 
831       if (is_get_method_name(fd, base))
832 	      return base.substr(strlen(get_prefix));
833       return base;
834 }
835