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().str();
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().str();
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().str());
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().str();
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 /* Does this function take any enum arguments?
333  */
takes_enums(FunctionDecl * fd)334 static bool takes_enums(FunctionDecl *fd)
335 {
336 	unsigned n;
337 
338 	n = fd->getNumParams();
339 	for (unsigned i = 0; i < n; ++i) {
340 		ParmVarDecl *param = fd->getParamDecl(i);
341 		if (param->getType()->getAs<EnumType>())
342 			return true;
343 	}
344 	return false;
345 }
346 
347 /* Sorting function that places declaration of functions
348  * with a shorter name first.
349  */
less_name(const FunctionDecl * a,const FunctionDecl * b)350 static bool less_name(const FunctionDecl *a, const FunctionDecl *b)
351 {
352 	return a->getName().size() < b->getName().size();
353 }
354 
355 /* Collect all functions that belong to a certain type, separating
356  * constructors from methods that set an enum value,
357  * methods that set a persistent callback and
358  * from regular methods, while keeping track of the _to_str,
359  * _copy and _free functions, if any, separately.
360  * Methods that accept any enum arguments that are not specifically handled
361  * are not supported.
362  * If there are any overloaded
363  * functions, then they are grouped based on their name after removing the
364  * argument type suffix.
365  * Check for functions that describe subclasses before considering
366  * any other functions in order to be able to detect those other
367  * functions as belonging to the subclasses.
368  * Sort the names of the functions based on their lengths
369  * to ensure that nested subclasses are handled later.
370  */
generator(SourceManager & SM,set<RecordDecl * > & exported_types,set<FunctionDecl * > exported_functions,set<FunctionDecl * > functions)371 generator::generator(SourceManager &SM, set<RecordDecl *> &exported_types,
372 	set<FunctionDecl *> exported_functions, set<FunctionDecl *> functions) :
373 	SM(SM)
374 {
375 	set<FunctionDecl *>::iterator in;
376 	set<RecordDecl *>::iterator it;
377 	vector<FunctionDecl *> type_subclasses;
378 	vector<FunctionDecl *>::iterator iv;
379 
380 	for (in = functions.begin(); in != functions.end(); ++in) {
381 		FunctionDecl *decl = *in;
382 		functions_by_name[decl->getName().str()] = decl;
383 	}
384 
385 	for (it = exported_types.begin(); it != exported_types.end(); ++it)
386 		add_class(*it);
387 
388 	for (in = exported_functions.begin(); in != exported_functions.end();
389 	     ++in) {
390 		if (is_subclass(*in))
391 			type_subclasses.push_back(*in);
392 	}
393 	std::sort(type_subclasses.begin(), type_subclasses.end(), &less_name);
394 	for (iv = type_subclasses.begin(); iv != type_subclasses.end(); ++iv) {
395 		add_type_subclasses(*iv);
396 	}
397 
398 	for (in = exported_functions.begin(); in != exported_functions.end();
399 	     ++in) {
400 		FunctionDecl *method = *in;
401 		isl_class *c;
402 
403 		if (is_subclass(method))
404 			continue;
405 
406 		c = method2class(method);
407 		if (!c)
408 			continue;
409 		if (is_constructor(method)) {
410 			c->constructors.insert(method);
411 		} else if (handled_sets_enum(c, method)) {
412 		} else if (sets_persistent_callback(c, method)) {
413 			c->persistent_callbacks.insert(method);
414 		} else if (takes_enums(method)) {
415 			std::string name = method->getName().str();
416 			die(name + " has unhandled enum argument");
417 		} else {
418 			string fullname = c->name_without_type_suffixes(method);
419 			c->methods[fullname].insert(method);
420 		}
421 	}
422 
423 	extract_automatic_conversions();
424 }
425 
426 /* Print error message "msg" and abort.
427  */
die(const char * msg)428 void generator::die(const char *msg)
429 {
430 	fprintf(stderr, "%s\n", msg);
431 	abort();
432 }
433 
434 /* Print error message "msg" and abort.
435  */
die(string msg)436 void generator::die(string msg)
437 {
438 	die(msg.c_str());
439 }
440 
441 /* Return a sequence of the types of which the given type declaration is
442  * marked as being a subtype.
443  * The order of the types is the opposite of the order in which they
444  * appear in the source.  In particular, the first annotation
445  * is the one that is closest to the annotated type and the corresponding
446  * type is then also the first that will appear in the sequence of types.
447  * This is also the order in which the annotations appear
448  * in the AttrVec returned by Decl::getAttrs() in older versions of clang.
449  * In newer versions of clang, the order is that in which
450  * the attribute appears in the source.
451  * Use the position of the "isl_export" attribute to determine
452  * whether this is an old (with reversed order) or a new version.
453  * The "isl_export" attribute is automatically added
454  * after each "isl_subclass" attribute.  If it appears in the list before
455  * any "isl_subclass" is encountered, then this must be a reversed list.
456  */
find_superclasses(Decl * decl)457 std::vector<string> generator::find_superclasses(Decl *decl)
458 {
459 	vector<string> super;
460 	bool reversed = false;
461 
462 	if (!decl->hasAttrs())
463 		return super;
464 
465 	string sub = "isl_subclass";
466 	size_t len = sub.length();
467 	AttrVec attrs = decl->getAttrs();
468 	for (AttrVec::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
469 		const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
470 		if (!ann)
471 			continue;
472 		string s = ann->getAnnotation().str();
473 		if (s == "isl_export" && super.size() == 0)
474 			reversed = true;
475 		if (s.substr(0, len) == sub) {
476 			s = s.substr(len + 1, s.length() - len  - 2);
477 			if (reversed)
478 				super.push_back(s);
479 			else
480 				super.insert(super.begin(), s);
481 		}
482 	}
483 
484 	return super;
485 }
486 
487 /* Is "decl" marked as describing subclasses?
488  */
is_subclass(FunctionDecl * decl)489 bool generator::is_subclass(FunctionDecl *decl)
490 {
491 	return find_superclasses(decl).size() > 0;
492 }
493 
494 /* Is decl marked as being part of an overloaded method?
495  */
is_overload(Decl * decl)496 bool generator::is_overload(Decl *decl)
497 {
498 	return has_annotation(decl, "isl_overload");
499 }
500 
501 /* Is decl marked as a constructor?
502  */
is_constructor(Decl * decl)503 bool generator::is_constructor(Decl *decl)
504 {
505 	return has_annotation(decl, "isl_constructor");
506 }
507 
508 /* Is decl marked as consuming a reference?
509  */
takes(Decl * decl)510 bool generator::takes(Decl *decl)
511 {
512 	return has_annotation(decl, "isl_take");
513 }
514 
515 /* Is decl marked as preserving a reference?
516  */
keeps(Decl * decl)517 bool generator::keeps(Decl *decl)
518 {
519 	return has_annotation(decl, "isl_keep");
520 }
521 
522 /* Is decl marked as returning a reference that is required to be freed.
523  */
gives(Decl * decl)524 bool generator::gives(Decl *decl)
525 {
526 	return has_annotation(decl, "isl_give");
527 }
528 
529 /* Return the class that has a name that best matches the initial part
530  * of the name of function "fd" or NULL if no such class could be found.
531  */
method2class(FunctionDecl * fd)532 isl_class *generator::method2class(FunctionDecl *fd)
533 {
534 	string best;
535 	map<string, isl_class>::iterator ci;
536 	string name = fd->getNameAsString();
537 
538 	for (ci = classes.begin(); ci != classes.end(); ++ci) {
539 		size_t len = ci->first.length();
540 		if (len > best.length() && name.substr(0, len) == ci->first &&
541 		    name[len] == '_')
542 			best = ci->first;
543 	}
544 
545 	if (classes.find(best) == classes.end()) {
546 		cerr << "Unable to find class of " << name << endl;
547 		return NULL;
548 	}
549 
550 	return &classes[best];
551 }
552 
553 /* Is "type" the type "isl_ctx *"?
554  */
is_isl_ctx(QualType type)555 bool generator::is_isl_ctx(QualType type)
556 {
557 	if (!type->isPointerType())
558 		return false;
559 	type = type->getPointeeType();
560 	if (type.getAsString() != "isl_ctx")
561 		return false;
562 
563 	return true;
564 }
565 
566 /* Is the first argument of "fd" of type "isl_ctx *"?
567  */
first_arg_is_isl_ctx(FunctionDecl * fd)568 bool generator::first_arg_is_isl_ctx(FunctionDecl *fd)
569 {
570 	ParmVarDecl *param;
571 
572 	if (fd->getNumParams() < 1)
573 		return false;
574 
575 	param = fd->getParamDecl(0);
576 	return is_isl_ctx(param->getOriginalType());
577 }
578 
579 namespace {
580 
581 struct ClangAPI {
582 	/* Return the first location in the range returned by
583 	 * clang::SourceManager::getImmediateExpansionRange.
584 	 * Older versions of clang return a pair of SourceLocation objects.
585 	 * More recent versions return a CharSourceRange.
586 	 */
range_begin__anon70ed26e80111::ClangAPI587 	static SourceLocation range_begin(
588 			const std::pair<SourceLocation,SourceLocation> &p) {
589 		return p.first;
590 	}
range_begin__anon70ed26e80111::ClangAPI591 	static SourceLocation range_begin(const CharSourceRange &range) {
592 		return range.getBegin();
593 	}
594 };
595 
596 }
597 
598 /* Does the callback argument "param" take its argument at position "pos"?
599  *
600  * The memory management annotations of arguments to function pointers
601  * are not recorded by clang, so the information cannot be extracted
602  * from the type of "param".
603  * Instead, go to the location in the source where the callback argument
604  * is declared, look for the right argument of the callback itself and
605  * then check if it has an "__isl_take" memory management annotation.
606  *
607  * If the return value of the function has a memory management annotation,
608  * then the spelling of "param" will point to the spelling
609  * of this memory management annotation.  Since the macro is defined
610  * on the command line (in main), this location does not have a file entry.
611  * In this case, move up one level in the macro expansion to the location
612  * where the memory management annotation is used.
613  */
callback_takes_argument(ParmVarDecl * param,int pos)614 bool generator::callback_takes_argument(ParmVarDecl *param,
615 	int pos)
616 {
617 	SourceLocation loc;
618 	const char *s, *end, *next;
619 	bool takes, keeps;
620 
621 	loc = param->getSourceRange().getBegin();
622 	if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(loc))))
623 		loc = ClangAPI::range_begin(SM.getImmediateExpansionRange(loc));
624 	s = SM.getCharacterData(loc);
625 	if (!s)
626 		die("No character data");
627 	s = strchr(s, '(');
628 	if (!s)
629 		die("Cannot find function pointer");
630 	s = strchr(s + 1, '(');
631 	if (!s)
632 		die("Cannot find function pointer arguments");
633 	end = strchr(s + 1, ')');
634 	if (!end)
635 		die("Cannot find end of function pointer arguments");
636 	while (pos-- > 0) {
637 		s = strchr(s + 1, ',');
638 		if (!s || s > end)
639 			die("Cannot find function pointer argument");
640 	}
641 	next = strchr(s + 1, ',');
642 	if (next && next < end)
643 		end = next;
644 	s = strchr(s + 1, '_');
645 	if (!s || s > end)
646 		die("Cannot find function pointer argument annotation");
647 	takes = prefixcmp(s, "__isl_take") == 0;
648 	keeps = prefixcmp(s, "__isl_keep") == 0;
649 	if (!takes && !keeps)
650 		die("Cannot find function pointer argument annotation");
651 
652 	return takes;
653 }
654 
655 /* Is "type" that of a pointer to an isl_* structure?
656  */
is_isl_type(QualType type)657 bool generator::is_isl_type(QualType type)
658 {
659 	if (type->isPointerType()) {
660 		string s;
661 
662 		type = type->getPointeeType();
663 		if (type->isFunctionType())
664 			return false;
665 		s = type.getAsString();
666 		return s.substr(0, 4) == "isl_";
667 	}
668 
669 	return false;
670 }
671 
672 /* Is "type" one of the integral types with a negative value
673  * indicating an error condition?
674  */
is_isl_neg_error(QualType type)675 bool generator::is_isl_neg_error(QualType type)
676 {
677 	return is_isl_bool(type) || is_isl_stat(type) || is_isl_size(type);
678 }
679 
680 /* Is "type" the primitive type with the given name?
681  */
is_isl_primitive(QualType type,const char * name)682 static bool is_isl_primitive(QualType type, const char *name)
683 {
684 	string s;
685 
686 	if (type->isPointerType())
687 		return false;
688 
689 	s = type.getAsString();
690 	return s == name;
691 }
692 
693 /* Is "type" the type isl_bool?
694  */
is_isl_bool(QualType type)695 bool generator::is_isl_bool(QualType type)
696 {
697 	return is_isl_primitive(type, "isl_bool");
698 }
699 
700 /* Is "type" the type isl_stat?
701  */
is_isl_stat(QualType type)702 bool generator::is_isl_stat(QualType type)
703 {
704 	return is_isl_primitive(type, "isl_stat");
705 }
706 
707 /* Is "type" the type isl_size?
708  */
is_isl_size(QualType type)709 bool generator::is_isl_size(QualType type)
710 {
711 	return is_isl_primitive(type, "isl_size");
712 }
713 
714 /* Is "type" that of a pointer to a function?
715  */
is_callback(QualType type)716 bool generator::is_callback(QualType type)
717 {
718 	if (!type->isPointerType())
719 		return false;
720 	type = type->getPointeeType();
721 	return type->isFunctionType();
722 }
723 
724 /* Is "type" that of "char *" of "const char *"?
725  */
is_string(QualType type)726 bool generator::is_string(QualType type)
727 {
728 	if (type->isPointerType()) {
729 		string s = type->getPointeeType().getAsString();
730 		return s == "const char" || s == "char";
731 	}
732 
733 	return false;
734 }
735 
736 /* Is "type" that of "long"?
737  */
is_long(QualType type)738 bool generator::is_long(QualType type)
739 {
740 	const BuiltinType *builtin = type->getAs<BuiltinType>();
741 	return builtin && builtin->getKind() == BuiltinType::Long;
742 }
743 
744 /* Is "type" that of "unsigned int"?
745  */
is_unsigned_int(QualType type)746 static bool is_unsigned_int(QualType type)
747 {
748 	const BuiltinType *builtin = type->getAs<BuiltinType>();
749 	return builtin && builtin->getKind() == BuiltinType::UInt;
750 }
751 
752 /* Return the name of the type that "type" points to.
753  * The input "type" is assumed to be a pointer type.
754  */
extract_type(QualType type)755 string generator::extract_type(QualType type)
756 {
757 	if (type->isPointerType())
758 		return type->getPointeeType().getAsString();
759 	die("Cannot extract type from non-pointer type");
760 }
761 
762 /* Given the type of a function pointer, return the corresponding
763  * function prototype.
764  */
extract_prototype(QualType type)765 const FunctionProtoType *generator::extract_prototype(QualType type)
766 {
767 	return type->getPointeeType()->getAs<FunctionProtoType>();
768 }
769 
770 /* Return the function name suffix for the type of "param".
771  *
772  * If the type of "param" is an isl object type,
773  * then the suffix is the name of the type with the "isl" prefix removed,
774  * but keeping the "_".
775  * If the type is an unsigned integer, then the type suffix is "_ui".
776  */
type_suffix(ParmVarDecl * param)777 static std::string type_suffix(ParmVarDecl *param)
778 {
779 	QualType type;
780 
781 	type = param->getOriginalType();
782 	if (generator::is_isl_type(type))
783 		return generator::extract_type(type).substr(3);
784 	else if (is_unsigned_int(type))
785 		return "_ui";
786 	generator::die("Unsupported type suffix");
787 }
788 
789 /* If "suffix" is a suffix of "s", then return "s" with the suffix removed.
790  * Otherwise, simply return "s".
791  */
drop_suffix(const std::string & s,const std::string & suffix)792 static std::string drop_suffix(const std::string &s, const std::string &suffix)
793 {
794 	size_t len, suffix_len;
795 
796 	len = s.length();
797 	suffix_len = suffix.length();
798 
799 	if (len >= suffix_len && s.substr(len - suffix_len) == suffix)
800 		return s.substr(0, len - suffix_len);
801 	else
802 		return s;
803 }
804 
805 /* If "method" is overloaded, then return its name with the suffixes
806  * corresponding to the types of the final arguments removed.
807  * Otherwise, simply return the name of the function.
808  * Start from the final argument and keep removing suffixes
809  * matching arguments, independently of whether previously considered
810  * arguments matched.
811  */
name_without_type_suffixes(FunctionDecl * method)812 string isl_class::name_without_type_suffixes(FunctionDecl *method)
813 {
814 	int num_params;
815 	string name;
816 
817 	name = method->getName().str();
818 	if (!generator::is_overload(method))
819 		return name;
820 
821 	num_params = method->getNumParams();
822 	for (int i = num_params - 1; i >= 0; --i) {
823 		ParmVarDecl *param;
824 		string type;
825 
826 		param = method->getParamDecl(i);
827 		type = type_suffix(param);
828 
829 		name = drop_suffix(name, type);
830 	}
831 
832 	return name;
833 }
834 
835 /* Is function "fd" with the given name a "get" method?
836  *
837  * A "get" method is an instance method
838  * with a name that starts with the get method prefix.
839  */
is_get_method_name(FunctionDecl * fd,const string & name) const840 bool isl_class::is_get_method_name(FunctionDecl *fd, const string &name) const
841 {
842 	return !is_static(fd) && prefixcmp(name.c_str(), get_prefix) == 0;
843 }
844 
845 /* Extract the method name corresponding to "fd".
846  *
847  * If "fd" is a "get" method, then drop the "get" method prefix.
848  */
method_name(FunctionDecl * fd) const849 string isl_class::method_name(FunctionDecl *fd) const
850 {
851       string base = base_method_name(fd);
852 
853       if (is_get_method_name(fd, base))
854 	      return base.substr(strlen(get_prefix));
855       return base;
856 }
857