Update to isl-0.20-35-ge0a98b62
[polly-mirror.git] / lib / External / isl / interface / generator.cc
blob01704f881b117a60deea40a35165752a7d1002f8
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 <iostream>
37 #include <clang/AST/Attr.h>
39 #include "isl_config.h"
40 #include "extract_interface.h"
41 #include "generator.h"
43 /* Should "method" be considered to be a static method?
44 * That is, is the first argument something other than
45 * an instance of the class?
47 bool generator::is_static(const isl_class &clazz, FunctionDecl *method)
49 ParmVarDecl *param = method->getParamDecl(0);
50 QualType type = param->getOriginalType();
52 if (!is_isl_type(type))
53 return true;
54 return extract_type(type) != clazz.name;
57 /* Find the FunctionDecl with name "name",
58 * returning NULL if there is no such FunctionDecl.
59 * If "required" is set, then error out if no FunctionDecl can be found.
61 FunctionDecl *generator::find_by_name(const string &name, bool required)
63 map<string, FunctionDecl *>::iterator i;
65 i = functions_by_name.find(name);
66 if (i != functions_by_name.end())
67 return i->second;
68 if (required)
69 die("No " + name + " function found");
70 return NULL;
73 /* Collect all functions that belong to a certain type, separating
74 * constructors from regular methods and keeping track of the _to_str,
75 * _copy and _free functions, if any, separately. If there are any overloaded
76 * functions, then they are grouped based on their name after removing the
77 * argument type suffix.
79 generator::generator(set<RecordDecl *> &exported_types,
80 set<FunctionDecl *> exported_functions, set<FunctionDecl *> functions)
82 map<string, isl_class>::iterator ci;
84 set<FunctionDecl *>::iterator in;
85 for (in = functions.begin(); in != functions.end(); ++in) {
86 FunctionDecl *decl = *in;
87 functions_by_name[decl->getName()] = decl;
90 set<RecordDecl *>::iterator it;
91 for (it = exported_types.begin(); it != exported_types.end(); ++it) {
92 RecordDecl *decl = *it;
93 string name = decl->getName();
94 classes[name].name = name;
95 classes[name].type = decl;
96 classes[name].fn_to_str = find_by_name(name + "_to_str", false);
97 classes[name].fn_copy = find_by_name(name + "_copy", true);
98 classes[name].fn_free = find_by_name(name + "_free", true);
101 for (in = exported_functions.begin(); in != exported_functions.end();
102 ++in) {
103 isl_class *c = method2class(*in);
104 if (!c)
105 continue;
106 if (is_constructor(*in)) {
107 c->constructors.insert(*in);
108 } else {
109 FunctionDecl *method = *in;
110 string fullname = method->getName();
111 fullname = drop_type_suffix(fullname, method);
112 c->methods[fullname].insert(method);
117 /* Print error message "msg" and abort.
119 void generator::die(const char *msg)
121 fprintf(stderr, "%s\n", msg);
122 abort();
125 /* Print error message "msg" and abort.
127 void generator::die(string msg)
129 die(msg.c_str());
132 /* Return a sequence of the types of which the given type declaration is
133 * marked as being a subtype.
134 * The order of the types is the opposite of the order in which they
135 * appear in the source. In particular, the first annotation
136 * is the one that is closest to the annotated type and the corresponding
137 * type is then also the first that will appear in the sequence of types.
139 std::vector<string> generator::find_superclasses(RecordDecl *decl)
141 vector<string> super;
143 if (!decl->hasAttrs())
144 return super;
146 string sub = "isl_subclass";
147 size_t len = sub.length();
148 AttrVec attrs = decl->getAttrs();
149 for (AttrVec::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
150 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
151 if (!ann)
152 continue;
153 string s = ann->getAnnotation().str();
154 if (s.substr(0, len) == sub) {
155 s = s.substr(len + 1, s.length() - len - 2);
156 super.push_back(s);
160 return super;
163 /* Is decl marked as being part of an overloaded method?
165 bool generator::is_overload(Decl *decl)
167 return has_annotation(decl, "isl_overload");
170 /* Is decl marked as a constructor?
172 bool generator::is_constructor(Decl *decl)
174 return has_annotation(decl, "isl_constructor");
177 /* Is decl marked as consuming a reference?
179 bool generator::takes(Decl *decl)
181 return has_annotation(decl, "isl_take");
184 /* Is decl marked as preserving a reference?
186 bool generator::keeps(Decl *decl)
188 return has_annotation(decl, "isl_keep");
191 /* Is decl marked as returning a reference that is required to be freed.
193 bool generator::gives(Decl *decl)
195 return has_annotation(decl, "isl_give");
198 /* Return the class that has a name that matches the initial part
199 * of the name of function "fd" or NULL if no such class could be found.
201 isl_class *generator::method2class(FunctionDecl *fd)
203 string best;
204 map<string, isl_class>::iterator ci;
205 string name = fd->getNameAsString();
207 for (ci = classes.begin(); ci != classes.end(); ++ci) {
208 if (name.substr(0, ci->first.length()) == ci->first)
209 best = ci->first;
212 if (classes.find(best) == classes.end()) {
213 cerr << "Unable to find class of " << name << endl;
214 return NULL;
217 return &classes[best];
220 /* Is "type" the type "isl_ctx *"?
222 bool generator::is_isl_ctx(QualType type)
224 if (!type->isPointerType())
225 return 0;
226 type = type->getPointeeType();
227 if (type.getAsString() != "isl_ctx")
228 return false;
230 return true;
233 /* Is the first argument of "fd" of type "isl_ctx *"?
235 bool generator::first_arg_is_isl_ctx(FunctionDecl *fd)
237 ParmVarDecl *param;
239 if (fd->getNumParams() < 1)
240 return false;
242 param = fd->getParamDecl(0);
243 return is_isl_ctx(param->getOriginalType());
246 /* Is "type" that of a pointer to an isl_* structure?
248 bool generator::is_isl_type(QualType type)
250 if (type->isPointerType()) {
251 string s;
253 type = type->getPointeeType();
254 if (type->isFunctionType())
255 return false;
256 s = type.getAsString();
257 return s.substr(0, 4) == "isl_";
260 return false;
263 /* Is "type" the type isl_bool?
265 bool generator::is_isl_bool(QualType type)
267 string s;
269 if (type->isPointerType())
270 return false;
272 s = type.getAsString();
273 return s == "isl_bool";
276 /* Is "type" the type isl_stat?
278 bool generator::is_isl_stat(QualType type)
280 string s;
282 if (type->isPointerType())
283 return false;
285 s = type.getAsString();
286 return s == "isl_stat";
290 /* Is "type" that of a pointer to a function?
292 bool generator::is_callback(QualType type)
294 if (!type->isPointerType())
295 return false;
296 type = type->getPointeeType();
297 return type->isFunctionType();
300 /* Is "type" that of "char *" of "const char *"?
302 bool generator::is_string(QualType type)
304 if (type->isPointerType()) {
305 string s = type->getPointeeType().getAsString();
306 return s == "const char" || s == "char";
309 return false;
312 /* Is "type" that of "long"?
314 bool generator::is_long(QualType type)
316 const BuiltinType *builtin = type->getAs<BuiltinType>();
317 return builtin && builtin->getKind() == BuiltinType::Long;
320 /* Return the name of the type that "type" points to.
321 * The input "type" is assumed to be a pointer type.
323 string generator::extract_type(QualType type)
325 if (type->isPointerType())
326 return type->getPointeeType().getAsString();
327 die("Cannot extract type from non-pointer type");
330 /* If "method" is overloaded, then drop the suffix of "name"
331 * corresponding to the type of the final argument and
332 * return the modified name (or the original name if
333 * no modifications were made).
335 string generator::drop_type_suffix(string name, FunctionDecl *method)
337 int num_params;
338 ParmVarDecl *param;
339 string type;
340 size_t name_len, type_len;
342 if (!is_overload(method))
343 return name;
345 num_params = method->getNumParams();
346 param = method->getParamDecl(num_params - 1);
347 type = extract_type(param->getOriginalType());
348 type = type.substr(4);
349 name_len = name.length();
350 type_len = type.length();
352 if (name_len > type_len && name.substr(name_len - type_len) == type)
353 name = name.substr(0, name_len - type_len - 1);
355 return name;