1 /*
2 * Copyright 2011 Leiden University. 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 LEIDEN UNIVERSITY ''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 LEIDEN UNIVERSITY 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 * Leiden University.
32 */
33
34 #include <string.h>
35
36 #include "id.h"
37
38 using namespace clang;
39
40 /* Create an isl_id that refers to the variable declarator "decl".
41 */
pet_id_from_decl(isl_ctx * ctx,ValueDecl * decl)42 __isl_give isl_id *pet_id_from_decl(isl_ctx *ctx, ValueDecl *decl)
43 {
44 return isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
45 }
46
47 /* Create an isl_id that refers to the variable declarator "decl", but
48 * has name "name".
49 */
pet_id_from_name_and_decl(isl_ctx * ctx,const char * name,ValueDecl * decl)50 __isl_give isl_id *pet_id_from_name_and_decl(isl_ctx *ctx, const char *name,
51 ValueDecl *decl)
52 {
53 return isl_id_alloc(ctx, name, decl);
54 }
55
56 /* Create an isl_id with name specified by "name_template" and "n" and
57 * associated type "qt".
58 */
pet_id_from_type(isl_ctx * ctx,const char * name_template,int n,QualType qt)59 static __isl_give isl_id *pet_id_from_type(isl_ctx *ctx,
60 const char *name_template, int n, QualType qt)
61 {
62 char name[50];
63 const Type *type = qt.getTypePtr();
64
65 snprintf(name, sizeof(name), name_template, n);
66 return isl_id_alloc(ctx, name, const_cast<Type *>(type));
67 }
68
69 /* Create an isl_id with name "__pet_arg_<n>" and associated type "qt".
70 */
pet_id_arg_from_type(isl_ctx * ctx,int n,QualType qt)71 __isl_give isl_id *pet_id_arg_from_type(isl_ctx *ctx, int n, QualType qt)
72 {
73 return pet_id_from_type(ctx, "__pet_arg_%d", n, qt);
74 }
75
76 /* Create an isl_id with name "__pet_ret_<n>" and associated type "qt".
77 */
pet_id_ret_from_type(isl_ctx * ctx,int n,QualType qt)78 __isl_give isl_id *pet_id_ret_from_type(isl_ctx *ctx, int n, QualType qt)
79 {
80 return pet_id_from_type(ctx, "__pet_ret_%d", n, qt);
81 }
82
83 /* Compare the prefix of "s" to "prefix" up to the length of "prefix".
84 */
prefixcmp(const char * s,const char * prefix)85 static int prefixcmp(const char *s, const char *prefix)
86 {
87 return strncmp(s, prefix, strlen(prefix));
88 }
89
90 /* Is "id" an identifier created by pet_id_arg_from_type or
91 * pet_id_ret_from_type?
92 */
pet_id_is_arg_or_ret(__isl_keep isl_id * id)93 static int pet_id_is_arg_or_ret(__isl_keep isl_id *id)
94 {
95 const char *name;
96
97 if (!id)
98 return -1;
99 name = isl_id_get_name(id);
100 if (!name)
101 return 0;
102 return !prefixcmp(name, "__pet_arg") || !prefixcmp(name, "__pet_ret");
103 }
104
105 /* Extract the ValueDecl that was associated to "id"
106 * in pet_id_from_decl.
107 *
108 * If "id" was create by pet_id_arg_from_type or pet_id_ret_from_type,
109 * then there is no ValueDecl associated to it, so return NULL instead.
110 */
pet_id_get_decl(__isl_keep isl_id * id)111 ValueDecl *pet_id_get_decl(__isl_keep isl_id *id)
112 {
113 if (pet_id_is_arg_or_ret(id))
114 return NULL;
115
116 return (ValueDecl *) isl_id_get_user(id);
117 }
118
119 /* Construct a pet_expr representing an index expression for an access
120 * to the variable represented by "id".
121 */
pet_id_create_index_expr(__isl_take isl_id * id)122 __isl_give pet_expr *pet_id_create_index_expr(__isl_take isl_id *id)
123 {
124 isl_space *space;
125
126 if (!id)
127 return NULL;
128
129 space = isl_space_alloc(isl_id_get_ctx(id), 0, 0, 0);
130 space = isl_space_set_tuple_id(space, isl_dim_out, id);
131
132 return pet_expr_from_index(isl_multi_pw_aff_zero(space));
133 }
134
135 /* Is "T" the type of a variable length array with static size?
136 */
is_vla_with_static_size(QualType T)137 static bool is_vla_with_static_size(QualType T)
138 {
139 const VariableArrayType *vlatype;
140
141 if (!T->isVariableArrayType())
142 return false;
143 vlatype = cast<VariableArrayType>(T);
144 return vlatype->getSizeModifier() == VariableArrayType::Static;
145 }
146
147 /* Return the type of the variable represented by "id" as an array.
148 *
149 * In particular, if the declaration associated to "id" is a parameter
150 * declaration that is a variable length array with a static size, then
151 * return the original type (i.e., the variable length array).
152 * Otherwise, return the type of decl.
153 *
154 * If "id" was created by pet_id_arg_from_type or pet_id_ret_from_type,
155 * then it has a type associated to it, so return that type instead.
156 */
pet_id_get_array_type(__isl_keep isl_id * id)157 QualType pet_id_get_array_type(__isl_keep isl_id *id)
158 {
159 ValueDecl *decl;
160 ParmVarDecl *parm;
161 QualType T;
162
163 if (pet_id_is_arg_or_ret(id)) {
164 const Type *type;
165 type = (const Type *) isl_id_get_user(id);
166 return QualType(type, 0);
167 }
168
169 decl = pet_id_get_decl(id);
170
171 parm = dyn_cast<ParmVarDecl>(decl);
172 if (!parm)
173 return decl->getType();
174
175 T = parm->getOriginalType();
176 if (!is_vla_with_static_size(T))
177 return decl->getType();
178 return T;
179 }
180