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
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
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
))
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())
69 die("No " + name
+ " function found");
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();
103 isl_class
*c
= method2class(classes
, *in
);
106 if (is_constructor(*in
)) {
107 c
->constructors
.insert(*in
);
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
);
125 /* Print error message "msg" and abort.
127 void generator::die(string msg
)
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())
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
);
153 string s
= ann
->getAnnotation().str();
154 if (s
.substr(0, len
) == sub
) {
155 s
= s
.substr(len
+ 1, s
.length() - len
- 2);
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(map
<string
, isl_class
> &classes
,
205 map
<string
, isl_class
>::iterator ci
;
206 string name
= fd
->getNameAsString();
208 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
209 if (name
.substr(0, ci
->first
.length()) == ci
->first
)
213 if (classes
.find(best
) == classes
.end()) {
214 cerr
<< "Unable to find class of " << name
<< endl
;
218 return &classes
[best
];
221 /* Is "type" the type "isl_ctx *"?
223 bool generator::is_isl_ctx(QualType type
)
225 if (!type
->isPointerType())
227 type
= type
->getPointeeType();
228 if (type
.getAsString() != "isl_ctx")
234 /* Is the first argument of "fd" of type "isl_ctx *"?
236 bool generator::first_arg_is_isl_ctx(FunctionDecl
*fd
)
240 if (fd
->getNumParams() < 1)
243 param
= fd
->getParamDecl(0);
244 return is_isl_ctx(param
->getOriginalType());
247 /* Is "type" that of a pointer to an isl_* structure?
249 bool generator::is_isl_type(QualType type
)
251 if (type
->isPointerType()) {
254 type
= type
->getPointeeType();
255 if (type
->isFunctionType())
257 s
= type
.getAsString();
258 return s
.substr(0, 4) == "isl_";
264 /* Is "type" the type isl_bool?
266 bool generator::is_isl_bool(QualType type
)
270 if (type
->isPointerType())
273 s
= type
.getAsString();
274 return s
== "isl_bool";
277 /* Is "type" the type isl_stat?
279 bool generator::is_isl_stat(QualType type
)
283 if (type
->isPointerType())
286 s
= type
.getAsString();
287 return s
== "isl_stat";
291 /* Is "type" that of a pointer to a function?
293 bool generator::is_callback(QualType type
)
295 if (!type
->isPointerType())
297 type
= type
->getPointeeType();
298 return type
->isFunctionType();
301 /* Is "type" that of "char *" of "const char *"?
303 bool generator::is_string(QualType type
)
305 if (type
->isPointerType()) {
306 string s
= type
->getPointeeType().getAsString();
307 return s
== "const char" || s
== "char";
313 /* Return the name of the type that "type" points to.
314 * The input "type" is assumed to be a pointer type.
316 string
generator::extract_type(QualType type
)
318 if (type
->isPointerType())
319 return type
->getPointeeType().getAsString();
320 die("Cannot extract type from non-pointer type");
323 /* If "method" is overloaded, then drop the suffix of "name"
324 * corresponding to the type of the final argument and
325 * return the modified name (or the original name if
326 * no modifications were made).
328 string
generator::drop_type_suffix(string name
, FunctionDecl
*method
)
333 size_t name_len
, type_len
;
335 if (!is_overload(method
))
338 num_params
= method
->getNumParams();
339 param
= method
->getParamDecl(num_params
- 1);
340 type
= extract_type(param
->getOriginalType());
341 type
= type
.substr(4);
342 name_len
= name
.length();
343 type_len
= type
.length();
345 if (name_len
> type_len
&& name
.substr(name_len
- type_len
) == type
)
346 name
= name
.substr(0, name_len
- type_len
- 1);