1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Util.cpp -- General utilities for working with the AST. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Wed Jan 19 9:46:00 2022 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed May 11 16:16:00 2022 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Util.hpp" |
---|
17 | |
---|
18 | #include "Node.hpp" |
---|
19 | #include "ParseNode.hpp" |
---|
20 | #include "Pass.hpp" |
---|
21 | #include "TranslationUnit.hpp" |
---|
22 | #include "Common/Utility.hpp" |
---|
23 | #include "GenPoly/ScopedSet.hpp" |
---|
24 | |
---|
25 | #include <vector> |
---|
26 | |
---|
27 | using GenPoly::ScopedSet; |
---|
28 | |
---|
29 | namespace ast { |
---|
30 | |
---|
31 | namespace { |
---|
32 | |
---|
33 | /// Check that ast::ptr/strong references do not form a cycle. |
---|
34 | struct NoStrongCyclesCore { |
---|
35 | std::vector<const Node *> parents; |
---|
36 | |
---|
37 | void previsit( const Node * node ) { |
---|
38 | for ( auto & parent : parents ) { |
---|
39 | assert( parent != node ); |
---|
40 | } |
---|
41 | parents.push_back( node ); |
---|
42 | } |
---|
43 | |
---|
44 | void postvisit( const Node * node ) { |
---|
45 | assert( !parents.empty() ); |
---|
46 | assert( parents.back() == node ); |
---|
47 | parents.pop_back(); |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | /// Check that every note that can has a set CodeLocation. |
---|
52 | void isCodeLocationSet( const ParseNode * node ) { |
---|
53 | assert( node->location.isSet() ); |
---|
54 | } |
---|
55 | |
---|
56 | void areLabelLocationsSet( const Stmt * stmt ) { |
---|
57 | for ( const Label& label : stmt->labels ) { |
---|
58 | assert( label.location.isSet() ); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | /// Make sure the reference counts are in a valid combination. |
---|
63 | void isStable( const Node * node ) { |
---|
64 | assert( node->isStable() ); |
---|
65 | } |
---|
66 | |
---|
67 | /// Check that a FunctionDecl is synchronized with it's FunctionType. |
---|
68 | void functionDeclMatchesType( const FunctionDecl * decl ) { |
---|
69 | // The type is a cache of sorts, if it is missing that is only a |
---|
70 | // problem if isTypeFixed is set. |
---|
71 | if ( decl->isTypeFixed ) { |
---|
72 | assert( decl->type ); |
---|
73 | } else if ( !decl->type ) { |
---|
74 | return; |
---|
75 | } |
---|
76 | |
---|
77 | const FunctionType * type = decl->type; |
---|
78 | |
---|
79 | // Check that `type->forall` corresponds with `decl->type_params`. |
---|
80 | assert( type->forall.size() == decl->type_params.size() ); |
---|
81 | // Check that `type->assertions` corresponds with `decl->assertions`. |
---|
82 | assert( type->assertions.size() == decl->assertions.size() ); |
---|
83 | // Check that `type->params` corresponds with `decl->params`. |
---|
84 | assert( type->params.size() == decl->params.size() ); |
---|
85 | // Check that `type->returns` corresponds with `decl->returns`. |
---|
86 | assert( type->returns.size() == decl->returns.size() ); |
---|
87 | } |
---|
88 | |
---|
89 | /// Check that the MemberExpr has an aggregate type and matching member. |
---|
90 | void memberMatchesAggregate( const MemberExpr * expr ) { |
---|
91 | const Type * aggrType = expr->aggregate->result->stripReferences(); |
---|
92 | const AggregateDecl * decl = nullptr; |
---|
93 | if ( auto inst = dynamic_cast<const StructInstType *>( aggrType ) ) { |
---|
94 | decl = inst->base; |
---|
95 | } else if ( auto inst = dynamic_cast<const UnionInstType *>( aggrType ) ) { |
---|
96 | decl = inst->base; |
---|
97 | } |
---|
98 | assertf( decl, "Aggregate of member not correct type." ); |
---|
99 | |
---|
100 | for ( auto aggrMember : decl->members ) { |
---|
101 | if ( expr->member == aggrMember ) { |
---|
102 | return; |
---|
103 | } |
---|
104 | } |
---|
105 | assertf( false, "Member not found." ); |
---|
106 | } |
---|
107 | |
---|
108 | /// Check for Floating Nodes: |
---|
109 | /// Every node should be reachable from a root (the TranslationUnit) via a |
---|
110 | /// chain of structural references (tracked with ptr). This cannot check all |
---|
111 | /// of that, it just checks if a given node's field has a strong reference. |
---|
112 | template<typename node_t, typename field_t> |
---|
113 | void noFloatingNode( const node_t * node, field_t node_t::*field_ptr ) { |
---|
114 | const field_t & field = node->*field_ptr; |
---|
115 | if ( nullptr == field ) return; |
---|
116 | assertf( field->isManaged(), "Floating node found." ); |
---|
117 | } |
---|
118 | |
---|
119 | struct InvariantCore { |
---|
120 | // To save on the number of visits: this is a kind of composed core. |
---|
121 | // None of the passes should make changes so ordering doesn't matter. |
---|
122 | NoStrongCyclesCore no_strong_cycles; |
---|
123 | |
---|
124 | void previsit( const Node * node ) { |
---|
125 | no_strong_cycles.previsit( node ); |
---|
126 | isStable( node ); |
---|
127 | } |
---|
128 | |
---|
129 | void previsit( const ParseNode * node ) { |
---|
130 | previsit( (const Node *)node ); |
---|
131 | isCodeLocationSet( node ); |
---|
132 | } |
---|
133 | |
---|
134 | void previsit( const FunctionDecl * node ) { |
---|
135 | previsit( (const ParseNode *)node ); |
---|
136 | functionDeclMatchesType( node ); |
---|
137 | } |
---|
138 | |
---|
139 | void previsit( const Stmt * node ) { |
---|
140 | previsit( (const ParseNode *)node ); |
---|
141 | areLabelLocationsSet( node ); |
---|
142 | } |
---|
143 | |
---|
144 | void previsit( const VariableExpr * node ) { |
---|
145 | previsit( (const ParseNode *)node ); |
---|
146 | noFloatingNode( node, &VariableExpr::var ); |
---|
147 | } |
---|
148 | |
---|
149 | void previsit( const MemberExpr * node ) { |
---|
150 | previsit( (const ParseNode *)node ); |
---|
151 | memberMatchesAggregate( node ); |
---|
152 | } |
---|
153 | |
---|
154 | void previsit( const StructInstType * node ) { |
---|
155 | previsit( (const Node *)node ); |
---|
156 | noFloatingNode( node, &StructInstType::base ); |
---|
157 | } |
---|
158 | |
---|
159 | void previsit( const UnionInstType * node ) { |
---|
160 | previsit( (const Node *)node ); |
---|
161 | noFloatingNode( node, &UnionInstType::base ); |
---|
162 | } |
---|
163 | |
---|
164 | void previsit( const EnumInstType * node ) { |
---|
165 | previsit( (const Node *)node ); |
---|
166 | noFloatingNode( node, &EnumInstType::base ); |
---|
167 | } |
---|
168 | |
---|
169 | void previsit( const TypeInstType * node ) { |
---|
170 | previsit( (const Node *)node ); |
---|
171 | noFloatingNode( node, &TypeInstType::base ); |
---|
172 | } |
---|
173 | |
---|
174 | void postvisit( const Node * node ) { |
---|
175 | no_strong_cycles.postvisit( node ); |
---|
176 | } |
---|
177 | }; |
---|
178 | |
---|
179 | /// Checks that referred to nodes are in scope. |
---|
180 | /// This checks many readonly pointers to see if the declaration they are |
---|
181 | /// referring to is in scope by the structural rules of code. |
---|
182 | // Any escapes marked with a bug should be removed once the bug is fixed. |
---|
183 | // This is a separate pass because of it changes the visit pattern and |
---|
184 | // must always be run on the entire translation unit. |
---|
185 | struct InScopeCore : public ast::WithShortCircuiting { |
---|
186 | ScopedSet<DeclWithType const *> typedDecls; |
---|
187 | ScopedSet<TypeDecl const *> typeDecls; |
---|
188 | // These 3 are really hard to check, because uses that originally ref. at |
---|
189 | // a forward declaration can be rewired to point a later full definition. |
---|
190 | ScopedSet<StructDecl const *> structDecls; |
---|
191 | ScopedSet<UnionDecl const *> unionDecls; |
---|
192 | ScopedSet<EnumDecl const *> enumDecls; |
---|
193 | ScopedSet<TraitDecl const *> traitDecls; |
---|
194 | |
---|
195 | bool isInGlobal = false; |
---|
196 | |
---|
197 | void beginScope() { |
---|
198 | typedDecls.beginScope(); |
---|
199 | typeDecls.beginScope(); |
---|
200 | structDecls.beginScope(); |
---|
201 | unionDecls.beginScope(); |
---|
202 | enumDecls.beginScope(); |
---|
203 | traitDecls.beginScope(); |
---|
204 | } |
---|
205 | |
---|
206 | void endScope() { |
---|
207 | typedDecls.endScope(); |
---|
208 | typeDecls.endScope(); |
---|
209 | structDecls.endScope(); |
---|
210 | unionDecls.endScope(); |
---|
211 | enumDecls.endScope(); |
---|
212 | traitDecls.endScope(); |
---|
213 | } |
---|
214 | |
---|
215 | void previsit( ApplicationExpr const * expr ) { |
---|
216 | // All isInGlobal manipulation is just to isolate this check. |
---|
217 | // The invalid compound literals lead to bad ctor/dtors. [#280] |
---|
218 | VariableExpr const * func = nullptr; |
---|
219 | CastExpr const * cast = nullptr; |
---|
220 | VariableExpr const * arg = nullptr; |
---|
221 | if ( isInGlobal |
---|
222 | && 1 == expr->args.size() |
---|
223 | && ( func = expr->func.as<VariableExpr>() ) |
---|
224 | && ( "?{}" == func->var->name || "^?{}" == func->var->name ) |
---|
225 | && ( cast = expr->args[0].as<CastExpr>() ) |
---|
226 | && ( arg = cast->arg.as<VariableExpr>() ) |
---|
227 | && isPrefix( arg->var->name, "_compLit" ) ) { |
---|
228 | visit_children = false; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | void previsit( VariableExpr const * expr ) { |
---|
233 | if ( !expr->var ) return; |
---|
234 | // bitwise assignment escape [#281] |
---|
235 | if ( expr->var->location.isUnset() ) return; |
---|
236 | assert( typedDecls.contains( expr->var ) ); |
---|
237 | } |
---|
238 | |
---|
239 | void previsit( FunctionType const * type ) { |
---|
240 | // This is to avoid checking the assertions, which can point at the |
---|
241 | // function's declaration and not the enclosing function. |
---|
242 | for ( auto type_param : type->forall ) { |
---|
243 | if ( type_param->formal_usage ) { |
---|
244 | visit_children = false; |
---|
245 | // We could check non-assertion fields here. |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | void previsit( TypeInstType const * type ) { |
---|
251 | if ( !type->base ) return; |
---|
252 | assertf( type->base->isManaged(), "Floating Node" ); |
---|
253 | |
---|
254 | // bitwise assignment escape [#281] |
---|
255 | if ( type->base->location.isUnset() ) return; |
---|
256 | // Formal types can actually look at out of scope variables. |
---|
257 | if ( type->formal_usage ) return; |
---|
258 | assert( typeDecls.contains( type->base ) ); |
---|
259 | } |
---|
260 | |
---|
261 | void previsit( TraitInstType const * type ) { |
---|
262 | if ( !type->base ) return; |
---|
263 | assert( traitDecls.contains( type->base ) ); |
---|
264 | } |
---|
265 | |
---|
266 | void previsit( ObjectDecl const * decl ) { |
---|
267 | typedDecls.insert( decl ); |
---|
268 | // There are some ill-formed compound literals. [#280] |
---|
269 | // The only known problem cases are at the top level. |
---|
270 | if ( isPrefix( decl->name, "_compLit" ) ) { |
---|
271 | visit_children = false; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | void previsit( FunctionDecl const * decl ) { |
---|
276 | typedDecls.insert( decl ); |
---|
277 | beginScope(); |
---|
278 | for ( auto & type_param : decl->type_params ) { |
---|
279 | typeDecls.insert( type_param ); |
---|
280 | } |
---|
281 | for ( auto & assertion : decl->assertions ) { |
---|
282 | typedDecls.insert( assertion ); |
---|
283 | } |
---|
284 | for ( auto & param : decl->params ) { |
---|
285 | typedDecls.insert( param ); |
---|
286 | } |
---|
287 | for ( auto & ret : decl->returns ) { |
---|
288 | typedDecls.insert( ret ); |
---|
289 | } |
---|
290 | // No special handling of withExprs. |
---|
291 | |
---|
292 | // Part of the compound literal escape. [#280] |
---|
293 | if ( "__global_init__" == decl->name |
---|
294 | || "__global_destroy__" == decl->name ) { |
---|
295 | assert( !isInGlobal ); |
---|
296 | isInGlobal = true; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | void postvisit( FunctionDecl const * decl ) { |
---|
301 | endScope(); |
---|
302 | // Part of the compound literal escape. [#280] |
---|
303 | if ( isInGlobal && ( "__global_init__" == decl->name |
---|
304 | || "__global_destroy__" == decl->name ) ) { |
---|
305 | isInGlobal = false; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | void previsit( StructDecl const * decl ) { |
---|
310 | structDecls.insert( decl ); |
---|
311 | beginScope(); |
---|
312 | for ( auto & type_param : decl->params ) { |
---|
313 | typeDecls.insert( type_param ); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void postvisit( StructDecl const * ) { |
---|
318 | endScope(); |
---|
319 | } |
---|
320 | |
---|
321 | void previsit( UnionDecl const * decl ) { |
---|
322 | unionDecls.insert( decl ); |
---|
323 | beginScope(); |
---|
324 | for ( auto & type_param : decl->params ) { |
---|
325 | typeDecls.insert( type_param ); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | void postvisit( UnionDecl const * ) { |
---|
330 | endScope(); |
---|
331 | } |
---|
332 | |
---|
333 | void previsit( EnumDecl const * decl ) { |
---|
334 | enumDecls.insert( decl ); |
---|
335 | for ( auto & member : decl->members ) { |
---|
336 | typedDecls.insert( member.strict_as<ast::DeclWithType>() ); |
---|
337 | } |
---|
338 | beginScope(); |
---|
339 | for ( auto & type_param : decl->params ) { |
---|
340 | typeDecls.insert( type_param ); |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | void postvisit( EnumDecl const * ) { |
---|
345 | endScope(); |
---|
346 | } |
---|
347 | |
---|
348 | void previsit( TraitDecl const * decl ) { |
---|
349 | traitDecls.insert( decl ); |
---|
350 | beginScope(); |
---|
351 | for ( auto & type_param : decl->params ) { |
---|
352 | typeDecls.insert( type_param ); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | void postvisit( TraitDecl const * ) { |
---|
357 | endScope(); |
---|
358 | } |
---|
359 | |
---|
360 | void previsit( Designation const * ) { |
---|
361 | visit_children = false; |
---|
362 | } |
---|
363 | }; |
---|
364 | |
---|
365 | } // namespace |
---|
366 | |
---|
367 | void checkInvariants( TranslationUnit & transUnit ) { |
---|
368 | Pass<InvariantCore>::run( transUnit ); |
---|
369 | Pass<InScopeCore>::run( transUnit ); |
---|
370 | } |
---|
371 | |
---|
372 | } // namespace ast |
---|