isl_basic_map_make_strides_explicit: use isl_basic_map_var_offset
[isl.git] / interface / generator.cc
blobfee2347b6c5cde508a96901a72391a913f69d8ef
1 /*
2 * Copyright 2011,2015 Sven Verdoolaege. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
34 #include <stdio.h>
35 #include <string.h>
36 #include <iostream>
38 #include <clang/AST/Attr.h>
39 #include <clang/Basic/SourceManager.h>
41 #include "isl_config.h"
42 #include "extract_interface.h"
43 #include "generator.h"
45 /* Compare the prefix of "s" to "prefix" up to the length of "prefix".
47 static int prefixcmp(const char *s, const char *prefix)
49 return strncmp(s, prefix, strlen(prefix));
52 /* Should "method" be considered to be a static method?
53 * That is, is the first argument something other than
54 * an instance of the class?
56 bool generator::is_static(const isl_class &clazz, FunctionDecl *method)
58 ParmVarDecl *param = method->getParamDecl(0);
59 QualType type = param->getOriginalType();
61 if (!is_isl_type(type))
62 return true;
63 return extract_type(type) != clazz.name;
66 /* Find the FunctionDecl with name "name",
67 * returning NULL if there is no such FunctionDecl.
68 * If "required" is set, then error out if no FunctionDecl can be found.
70 FunctionDecl *generator::find_by_name(const string &name, bool required)
72 map<string, FunctionDecl *>::iterator i;
74 i = functions_by_name.find(name);
75 if (i != functions_by_name.end())
76 return i->second;
77 if (required)
78 die("No " + name + " function found");
79 return NULL;
82 /* Collect all functions that belong to a certain type, separating
83 * constructors from regular methods and keeping track of the _to_str,
84 * _copy and _free functions, if any, separately. If there are any overloaded
85 * functions, then they are grouped based on their name after removing the
86 * argument type suffix.
88 generator::generator(SourceManager &SM, set<RecordDecl *> &exported_types,
89 set<FunctionDecl *> exported_functions, set<FunctionDecl *> functions) :
90 SM(SM)
92 map<string, isl_class>::iterator ci;
94 set<FunctionDecl *>::iterator in;
95 for (in = functions.begin(); in != functions.end(); ++in) {
96 FunctionDecl *decl = *in;
97 functions_by_name[decl->getName()] = decl;
100 set<RecordDecl *>::iterator it;
101 for (it = exported_types.begin(); it != exported_types.end(); ++it) {
102 RecordDecl *decl = *it;
103 string name = decl->getName();
104 classes[name].name = name;
105 classes[name].type = decl;
106 classes[name].fn_to_str = find_by_name(name + "_to_str", false);
107 classes[name].fn_copy = find_by_name(name + "_copy", true);
108 classes[name].fn_free = find_by_name(name + "_free", true);
111 for (in = exported_functions.begin(); in != exported_functions.end();
112 ++in) {
113 FunctionDecl *method = *in;
114 isl_class *c = method2class(method);
116 if (!c)
117 continue;
118 if (is_constructor(method)) {
119 c->constructors.insert(method);
120 } else {
121 string fullname = c->name_without_type_suffix(method);
122 c->methods[fullname].insert(method);
127 /* Print error message "msg" and abort.
129 void generator::die(const char *msg)
131 fprintf(stderr, "%s\n", msg);
132 abort();
135 /* Print error message "msg" and abort.
137 void generator::die(string msg)
139 die(msg.c_str());
142 /* Return a sequence of the types of which the given type declaration is
143 * marked as being a subtype.
144 * The order of the types is the opposite of the order in which they
145 * appear in the source. In particular, the first annotation
146 * is the one that is closest to the annotated type and the corresponding
147 * type is then also the first that will appear in the sequence of types.
149 std::vector<string> generator::find_superclasses(RecordDecl *decl)
151 vector<string> super;
153 if (!decl->hasAttrs())
154 return super;
156 string sub = "isl_subclass";
157 size_t len = sub.length();
158 AttrVec attrs = decl->getAttrs();
159 for (AttrVec::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
160 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
161 if (!ann)
162 continue;
163 string s = ann->getAnnotation().str();
164 if (s.substr(0, len) == sub) {
165 s = s.substr(len + 1, s.length() - len - 2);
166 super.push_back(s);
170 return super;
173 /* Is decl marked as being part of an overloaded method?
175 bool generator::is_overload(Decl *decl)
177 return has_annotation(decl, "isl_overload");
180 /* Is decl marked as a constructor?
182 bool generator::is_constructor(Decl *decl)
184 return has_annotation(decl, "isl_constructor");
187 /* Is decl marked as consuming a reference?
189 bool generator::takes(Decl *decl)
191 return has_annotation(decl, "isl_take");
194 /* Is decl marked as preserving a reference?
196 bool generator::keeps(Decl *decl)
198 return has_annotation(decl, "isl_keep");
201 /* Is decl marked as returning a reference that is required to be freed.
203 bool generator::gives(Decl *decl)
205 return has_annotation(decl, "isl_give");
208 /* Return the class that has a name that matches the initial part
209 * of the name of function "fd" or NULL if no such class could be found.
211 isl_class *generator::method2class(FunctionDecl *fd)
213 string best;
214 map<string, isl_class>::iterator ci;
215 string name = fd->getNameAsString();
217 for (ci = classes.begin(); ci != classes.end(); ++ci) {
218 if (name.substr(0, ci->first.length()) == ci->first)
219 best = ci->first;
222 if (classes.find(best) == classes.end()) {
223 cerr << "Unable to find class of " << name << endl;
224 return NULL;
227 return &classes[best];
230 /* Is "type" the type "isl_ctx *"?
232 bool generator::is_isl_ctx(QualType type)
234 if (!type->isPointerType())
235 return 0;
236 type = type->getPointeeType();
237 if (type.getAsString() != "isl_ctx")
238 return false;
240 return true;
243 /* Is the first argument of "fd" of type "isl_ctx *"?
245 bool generator::first_arg_is_isl_ctx(FunctionDecl *fd)
247 ParmVarDecl *param;
249 if (fd->getNumParams() < 1)
250 return false;
252 param = fd->getParamDecl(0);
253 return is_isl_ctx(param->getOriginalType());
256 namespace {
258 struct ClangAPI {
259 /* Return the first location in the range returned by
260 * clang::SourceManager::getImmediateExpansionRange.
261 * Older versions of clang return a pair of SourceLocation objects.
262 * More recent versions return a CharSourceRange.
264 static SourceLocation range_begin(
265 const std::pair<SourceLocation,SourceLocation> &p) {
266 return p.first;
268 static SourceLocation range_begin(const CharSourceRange &range) {
269 return range.getBegin();
275 /* Does the callback argument "param" take its argument at position "pos"?
277 * The memory management annotations of arguments to function pointers
278 * are not recorded by clang, so the information cannot be extracted
279 * from the type of "param".
280 * Instead, go to the location in the source where the callback argument
281 * is declared, look for the right argument of the callback itself and
282 * then check if it has an "__isl_take" memory management annotation.
284 * If the return value of the function has a memory management annotation,
285 * then the spelling of "param" will point to the spelling
286 * of this memory management annotation. Since the macro is defined
287 * on the command line (in main), this location does not have a file entry.
288 * In this case, move up one level in the macro expansion to the location
289 * where the memory management annotation is used.
291 bool generator::callback_takes_argument(ParmVarDecl *param,
292 int pos)
294 SourceLocation loc;
295 const char *s, *end, *next;
296 bool takes, keeps;
298 loc = param->getSourceRange().getBegin();
299 if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(loc))))
300 loc = ClangAPI::range_begin(SM.getImmediateExpansionRange(loc));
301 s = SM.getCharacterData(loc);
302 if (!s)
303 die("No character data");
304 s = strchr(s, '(');
305 if (!s)
306 die("Cannot find function pointer");
307 s = strchr(s + 1, '(');
308 if (!s)
309 die("Cannot find function pointer arguments");
310 end = strchr(s + 1, ')');
311 if (!end)
312 die("Cannot find end of function pointer arguments");
313 while (pos-- > 0) {
314 s = strchr(s + 1, ',');
315 if (!s || s > end)
316 die("Cannot find function pointer argument");
318 next = strchr(s + 1, ',');
319 if (next && next < end)
320 end = next;
321 s = strchr(s + 1, '_');
322 if (!s || s > end)
323 die("Cannot find function pointer argument annotation");
324 takes = prefixcmp(s, "__isl_take") == 0;
325 keeps = prefixcmp(s, "__isl_keep") == 0;
326 if (!takes && !keeps)
327 die("Cannot find function pointer argument annotation");
329 return takes;
332 /* Is "type" that of a pointer to an isl_* structure?
334 bool generator::is_isl_type(QualType type)
336 if (type->isPointerType()) {
337 string s;
339 type = type->getPointeeType();
340 if (type->isFunctionType())
341 return false;
342 s = type.getAsString();
343 return s.substr(0, 4) == "isl_";
346 return false;
349 /* Is "type" the type isl_bool?
351 bool generator::is_isl_bool(QualType type)
353 string s;
355 if (type->isPointerType())
356 return false;
358 s = type.getAsString();
359 return s == "isl_bool";
362 /* Is "type" the type isl_stat?
364 bool generator::is_isl_stat(QualType type)
366 string s;
368 if (type->isPointerType())
369 return false;
371 s = type.getAsString();
372 return s == "isl_stat";
376 /* Is "type" that of a pointer to a function?
378 bool generator::is_callback(QualType type)
380 if (!type->isPointerType())
381 return false;
382 type = type->getPointeeType();
383 return type->isFunctionType();
386 /* Is "type" that of "char *" of "const char *"?
388 bool generator::is_string(QualType type)
390 if (type->isPointerType()) {
391 string s = type->getPointeeType().getAsString();
392 return s == "const char" || s == "char";
395 return false;
398 /* Is "type" that of "long"?
400 bool generator::is_long(QualType type)
402 const BuiltinType *builtin = type->getAs<BuiltinType>();
403 return builtin && builtin->getKind() == BuiltinType::Long;
406 /* Return the name of the type that "type" points to.
407 * The input "type" is assumed to be a pointer type.
409 string generator::extract_type(QualType type)
411 if (type->isPointerType())
412 return type->getPointeeType().getAsString();
413 die("Cannot extract type from non-pointer type");
416 /* Given the type of a function pointer, return the corresponding
417 * function prototype.
419 const FunctionProtoType *generator::extract_prototype(QualType type)
421 return type->getPointeeType()->getAs<FunctionProtoType>();
424 /* If "method" is overloaded, then return its name with the suffix
425 * corresponding to the type of the final argument removed.
426 * Otherwise, simply return the name of the function.
428 string isl_class::name_without_type_suffix(FunctionDecl *method)
430 int num_params;
431 ParmVarDecl *param;
432 string name, type;
433 size_t name_len, type_len;
435 name = method->getName();
436 if (!generator::is_overload(method))
437 return name;
439 num_params = method->getNumParams();
440 param = method->getParamDecl(num_params - 1);
441 type = generator::extract_type(param->getOriginalType());
442 type = type.substr(4);
443 name_len = name.length();
444 type_len = type.length();
446 if (name_len > type_len && name.substr(name_len - type_len) == type)
447 name = name.substr(0, name_len - type_len - 1);
449 return name;