source: src/AST/Util.cpp@ 92aeae1

Last change on this file since 92aeae1 was 90e683b, checked in by Andrew Beach <ajbeach@…>, 8 months ago

I set out to do a enum rework. It ended up being much the same and I unwound the core rework. But I hope the new names are a bit clearer and other minor fixes are helpful, so I am keeping those.

  • Property mode set to 100644
File size: 10.5 KB
Line 
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
27using GenPoly::ScopedSet;
28
29namespace ast {
30
31namespace {
32
33/// Check that ast::ptr/strong references do not form a cycle.
34struct 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.
52void isCodeLocationSet( const ParseNode * node ) {
53 assert( node->location.isSet() );
54}
55
56void 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.
63void isStable( const Node * node ) {
64 assert( node->isStable() );
65}
66
67/// Check that a FunctionDecl is synchronized with it's FunctionType.
68void 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 an enumeration has not been made with an inconsistent spec.
90void isEnumerationConsistent( const EnumDecl * node ) {
91 if ( node->is_c_enum() ) {
92 assert( nullptr == node->base );
93 }
94}
95
96/// Check that the MemberExpr has an aggregate type and matching member.
97void memberMatchesAggregate( const MemberExpr * expr ) {
98 const Type * aggrType = expr->aggregate->result->stripReferences();
99 const AggregateDecl * decl = nullptr;
100 if ( auto inst = dynamic_cast<const StructInstType *>( aggrType ) ) {
101 decl = inst->base;
102 } else if ( auto inst = dynamic_cast<const UnionInstType *>( aggrType ) ) {
103 decl = inst->base;
104 }
105 assertf( decl, "Aggregate of member not correct type." );
106
107 for ( auto aggrMember : decl->members ) {
108 if ( expr->member == aggrMember ) {
109 return;
110 }
111 }
112 assertf( false, "Member not found." );
113}
114
115/// Check for Floating Nodes:
116/// Every node should be reachable from a root (the TranslationUnit) via a
117/// chain of structural references (tracked with ptr). This cannot check all
118/// of that, it just checks if a given node's field has a strong reference.
119template<typename node_t, typename field_t>
120void noFloatingNode( const node_t * node, field_t node_t::*field_ptr ) {
121 const field_t & field = node->*field_ptr;
122 if ( nullptr == field ) return;
123 assertf( field->isManaged(), "Floating node found." );
124}
125
126struct InvariantCore {
127 // To save on the number of visits: this is a kind of composed core.
128 // None of the passes should make changes so ordering doesn't matter.
129 NoStrongCyclesCore no_strong_cycles;
130
131 void previsit( const Node * node ) {
132 no_strong_cycles.previsit( node );
133 isStable( node );
134 }
135
136 void previsit( const ParseNode * node ) {
137 previsit( (const Node *)node );
138 isCodeLocationSet( node );
139 }
140
141 void previsit( const FunctionDecl * node ) {
142 previsit( (const ParseNode *)node );
143 functionDeclMatchesType( node );
144 }
145
146 void previsit( const EnumDecl * node ) {
147 previsit( (const ParseNode *)node );
148 isEnumerationConsistent( node );
149 }
150
151 void previsit( const Stmt * node ) {
152 previsit( (const ParseNode *)node );
153 areLabelLocationsSet( node );
154 }
155
156 void previsit( const VariableExpr * node ) {
157 previsit( (const ParseNode *)node );
158 noFloatingNode( node, &VariableExpr::var );
159 }
160
161 void previsit( const MemberExpr * node ) {
162 previsit( (const ParseNode *)node );
163 memberMatchesAggregate( node );
164 }
165
166 void previsit( const StructInstType * node ) {
167 previsit( (const Node *)node );
168 noFloatingNode( node, &StructInstType::base );
169 }
170
171 void previsit( const UnionInstType * node ) {
172 previsit( (const Node *)node );
173 noFloatingNode( node, &UnionInstType::base );
174 }
175
176 void previsit( const EnumInstType * node ) {
177 previsit( (const Node *)node );
178 noFloatingNode( node, &EnumInstType::base );
179 }
180
181 void previsit( const TypeInstType * node ) {
182 previsit( (const Node *)node );
183 noFloatingNode( node, &TypeInstType::base );
184 }
185
186 void postvisit( const Node * node ) {
187 no_strong_cycles.postvisit( node );
188 }
189};
190
191/// Checks that referred to nodes are in scope.
192/// This checks many readonly pointers to see if the declaration they are
193/// referring to is in scope by the structural rules of code.
194// Any escapes marked with a bug should be removed once the bug is fixed.
195// This is a separate pass because of it changes the visit pattern and
196// must always be run on the entire translation unit.
197struct InScopeCore : public ast::WithShortCircuiting {
198 ScopedSet<DeclWithType const *> typedDecls;
199 ScopedSet<TypeDecl const *> typeDecls;
200 // These 3 are really hard to check, because uses that originally ref. at
201 // a forward declaration can be rewired to point a later full definition.
202 ScopedSet<StructDecl const *> structDecls;
203 ScopedSet<UnionDecl const *> unionDecls;
204 ScopedSet<EnumDecl const *> enumDecls;
205 ScopedSet<TraitDecl const *> traitDecls;
206
207 bool isInGlobal = false;
208
209 void beginScope() {
210 typedDecls.beginScope();
211 typeDecls.beginScope();
212 structDecls.beginScope();
213 unionDecls.beginScope();
214 enumDecls.beginScope();
215 traitDecls.beginScope();
216 }
217
218 void endScope() {
219 typedDecls.endScope();
220 typeDecls.endScope();
221 structDecls.endScope();
222 unionDecls.endScope();
223 enumDecls.endScope();
224 traitDecls.endScope();
225 }
226
227 void previsit( ApplicationExpr const * expr ) {
228 // All isInGlobal manipulation is just to isolate this check.
229 // The invalid compound literals lead to bad ctor/dtors. [#280]
230 VariableExpr const * func = nullptr;
231 CastExpr const * cast = nullptr;
232 VariableExpr const * arg = nullptr;
233 if ( isInGlobal
234 && 1 == expr->args.size()
235 && ( func = expr->func.as<VariableExpr>() )
236 && ( "?{}" == func->var->name || "^?{}" == func->var->name )
237 && ( cast = expr->args[0].as<CastExpr>() )
238 && ( arg = cast->arg.as<VariableExpr>() )
239 && isPrefix( arg->var->name, "_compLit" ) ) {
240 visit_children = false;
241 }
242 }
243
244 void previsit( VariableExpr const * expr ) {
245 if ( !expr->var ) return;
246 // bitwise assignment escape [#281]
247 if ( expr->var->location.isUnset() ) return;
248 assert( typedDecls.contains( expr->var ) );
249 }
250
251 void previsit( FunctionType const * type ) {
252 // This is to avoid checking the assertions, which can point at the
253 // function's declaration and not the enclosing function.
254 for ( auto type_param : type->forall ) {
255 if ( type_param->formal_usage ) {
256 visit_children = false;
257 // We could check non-assertion fields here.
258 }
259 }
260 }
261
262 void previsit( TypeInstType const * type ) {
263 if ( !type->base ) return;
264 assertf( type->base->isManaged(), "Floating Node" );
265
266 // bitwise assignment escape [#281]
267 if ( type->base->location.isUnset() ) return;
268 // Formal types can actually look at out of scope variables.
269 if ( type->formal_usage ) return;
270 assert( typeDecls.contains( type->base ) );
271 }
272
273 void previsit( TraitInstType const * type ) {
274 if ( !type->base ) return;
275 assert( traitDecls.contains( type->base ) );
276 }
277
278 void previsit( ObjectDecl const * decl ) {
279 typedDecls.insert( decl );
280 // There are some ill-formed compound literals. [#280]
281 // The only known problem cases are at the top level.
282 if ( isPrefix( decl->name, "_compLit" ) ) {
283 visit_children = false;
284 }
285 }
286
287 void previsit( FunctionDecl const * decl ) {
288 typedDecls.insert( decl );
289 beginScope();
290 for ( auto & type_param : decl->type_params ) {
291 typeDecls.insert( type_param );
292 }
293 for ( auto & assertion : decl->assertions ) {
294 typedDecls.insert( assertion );
295 }
296 for ( auto & param : decl->params ) {
297 typedDecls.insert( param );
298 }
299 for ( auto & ret : decl->returns ) {
300 typedDecls.insert( ret );
301 }
302 // No special handling of withExprs.
303
304 // Part of the compound literal escape. [#280]
305 if ( "__global_init__" == decl->name
306 || "__global_destroy__" == decl->name ) {
307 assert( !isInGlobal );
308 isInGlobal = true;
309 }
310 }
311
312 void postvisit( FunctionDecl const * decl ) {
313 endScope();
314 // Part of the compound literal escape. [#280]
315 if ( isInGlobal && ( "__global_init__" == decl->name
316 || "__global_destroy__" == decl->name ) ) {
317 isInGlobal = false;
318 }
319 }
320
321 void previsit( StructDecl const * decl ) {
322 structDecls.insert( decl );
323 beginScope();
324 for ( auto & type_param : decl->params ) {
325 typeDecls.insert( type_param );
326 }
327 }
328
329 void postvisit( StructDecl const * ) {
330 endScope();
331 }
332
333 void previsit( UnionDecl const * decl ) {
334 unionDecls.insert( decl );
335 beginScope();
336 for ( auto & type_param : decl->params ) {
337 typeDecls.insert( type_param );
338 }
339 }
340
341 void postvisit( UnionDecl const * ) {
342 endScope();
343 }
344
345 void previsit( EnumDecl const * decl ) {
346 enumDecls.insert( decl );
347 for ( auto & member : decl->members ) {
348 typedDecls.insert( member.strict_as<ast::DeclWithType>() );
349 }
350 beginScope();
351 for ( auto & type_param : decl->params ) {
352 typeDecls.insert( type_param );
353 }
354 }
355
356 void postvisit( EnumDecl const * ) {
357 endScope();
358 }
359
360 void previsit( TraitDecl const * decl ) {
361 traitDecls.insert( decl );
362 beginScope();
363 for ( auto & type_param : decl->params ) {
364 typeDecls.insert( type_param );
365 }
366 }
367
368 void postvisit( TraitDecl const * ) {
369 endScope();
370 }
371
372 void previsit( Designation const * ) {
373 visit_children = false;
374 }
375};
376
377} // namespace
378
379void checkInvariants( TranslationUnit & transUnit ) {
380 Pass<InvariantCore>::run( transUnit );
381 Pass<InScopeCore>::run( transUnit );
382}
383
384} // namespace ast
Note: See TracBrowser for help on using the repository browser.