source: src/ResolvExpr/Resolver.cc@ 27de955

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 27de955 was d1d17f5, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

prefer unsigned long int expressions for variable length array declarations

  • Property mode set to 100644
File size: 11.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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// Resolver.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:17:01 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Mon Jun 01 13:47:16 2015
13// Update Count : 21
14//
15
16#include "Resolver.h"
17#include "AlternativeFinder.h"
18#include "Alternative.h"
19#include "RenameVars.h"
20#include "ResolveTypeof.h"
21#include "SynTree/Statement.h"
22#include "SynTree/Type.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Initializer.h"
25#include "SymTab/Indexer.h"
26#include "utility.h"
27
28#include <iostream>
29using namespace std;
30
31namespace ResolvExpr {
32 class Resolver : public SymTab::Indexer {
33 public:
34 Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
35
36 virtual void visit( FunctionDecl *functionDecl );
37 virtual void visit( ObjectDecl *functionDecl );
38 virtual void visit( TypeDecl *typeDecl );
39
40 virtual void visit( ExprStmt *exprStmt );
41 virtual void visit( IfStmt *ifStmt );
42 virtual void visit( WhileStmt *whileStmt );
43 virtual void visit( ForStmt *forStmt );
44 virtual void visit( SwitchStmt *switchStmt );
45 virtual void visit( ChooseStmt *switchStmt );
46 virtual void visit( CaseStmt *caseStmt );
47 virtual void visit( ReturnStmt *returnStmt );
48
49 virtual void visit( SingleInit *singleInit );
50 virtual void visit( ListInit *listInit );
51 private:
52 std::list< Type * > functionReturn;
53 Type *initContext;
54 Type *switchType;
55 };
56
57 void resolve( std::list< Declaration * > translationUnit ) {
58 Resolver resolver;
59 acceptAll( translationUnit, resolver );
60#if 0
61 resolver.print( cerr );
62 for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
63 (*i)->print( std::cerr );
64 (*i)->accept( resolver );
65 } // for
66#endif
67 }
68
69 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
70 TypeEnvironment env;
71 return resolveInVoidContext( expr, indexer, env );
72 }
73
74 namespace {
75 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
76 expr->set_env( new TypeSubstitution );
77 env.makeSubstitution( *expr->get_env() );
78 }
79
80 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
81 global_renamer.reset();
82 TypeEnvironment env;
83 Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
84 finishExpr( newExpr, env );
85 return newExpr;
86 }
87
88 Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
89 TypeEnvironment env;
90 AlternativeFinder finder( indexer, env );
91 finder.find( untyped );
92#if 0
93 if ( finder.get_alternatives().size() != 1 ) {
94 std::cout << "untyped expr is ";
95 untyped->print( std::cout );
96 std::cout << std::endl << "alternatives are:";
97 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
98 i->print( std::cout );
99 } // for
100 } // if
101#endif
102 assert( finder.get_alternatives().size() == 1 );
103 Alternative &choice = finder.get_alternatives().front();
104 Expression *newExpr = choice.expr->clone();
105 finishExpr( newExpr, choice.env );
106 return newExpr;
107 }
108
109 bool isIntegralType( Type *type ) {
110 if ( dynamic_cast< EnumInstType * >( type ) ) {
111 return true;
112 } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
113 return bt->isInteger();
114 } else {
115 return false;
116 } // if
117 }
118
119 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
120 TypeEnvironment env;
121 AlternativeFinder finder( indexer, env );
122 finder.find( untyped );
123#if 0
124 if ( finder.get_alternatives().size() != 1 ) {
125 std::cout << "untyped expr is ";
126 untyped->print( std::cout );
127 std::cout << std::endl << "alternatives are:";
128 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
129 i->print( std::cout );
130 } // for
131 } // if
132#endif
133 Expression *newExpr = 0;
134 const TypeEnvironment *newEnv = 0;
135 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
136 if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
137 if ( newExpr ) {
138 throw SemanticError( "Too many interpretations for case control expression", untyped );
139 } else {
140 newExpr = i->expr->clone();
141 newEnv = &i->env;
142 } // if
143 } // if
144 } // for
145 if ( ! newExpr ) {
146 throw SemanticError( "No interpretations for case control expression", untyped );
147 } // if
148 finishExpr( newExpr, *newEnv );
149 return newExpr;
150 }
151
152 }
153
154 void Resolver::visit( ObjectDecl *objectDecl ) {
155 Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
156 objectDecl->set_type( new_type );
157 initContext = new_type;
158 SymTab::Indexer::visit( objectDecl );
159
160 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){
161 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
162 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
163 Expression *newExpr = findSingleExpression( castExpr, *this );
164 delete at->get_dimension();
165 at->set_dimension( newExpr );
166 }
167 }
168
169 void Resolver::visit( TypeDecl *typeDecl ) {
170 if ( typeDecl->get_base() ) {
171 Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
172 typeDecl->set_base( new_type );
173 } // if
174 SymTab::Indexer::visit( typeDecl );
175 }
176
177 void Resolver::visit( FunctionDecl *functionDecl ) {
178#if 0
179 std::cout << "resolver visiting functiondecl ";
180 functionDecl->print( std::cout );
181 std::cout << std::endl;
182#endif
183 Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
184 functionDecl->set_type( new_type );
185 std::list< Type * > oldFunctionReturn = functionReturn;
186 functionReturn.clear();
187 for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
188 functionReturn.push_back( (*i)->get_type() );
189 } // for
190 SymTab::Indexer::visit( functionDecl );
191 functionReturn = oldFunctionReturn;
192 }
193
194 void Resolver::visit( ExprStmt *exprStmt ) {
195 if ( exprStmt->get_expr() ) {
196 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
197 delete exprStmt->get_expr();
198 exprStmt->set_expr( newExpr );
199 } // if
200 }
201
202 void Resolver::visit( IfStmt *ifStmt ) {
203 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
204 delete ifStmt->get_condition();
205 ifStmt->set_condition( newExpr );
206 Visitor::visit( ifStmt );
207 }
208
209 void Resolver::visit( WhileStmt *whileStmt ) {
210 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
211 delete whileStmt->get_condition();
212 whileStmt->set_condition( newExpr );
213 Visitor::visit( whileStmt );
214 }
215
216 void Resolver::visit( ForStmt *forStmt ) {
217 // SymTab::Indexer::visit( forStmt );
218 Expression *newExpr;
219 // for statements introduce a level of scope
220 enterScope();
221 maybeAccept( forStmt->get_initialization(), *this );
222 if ( forStmt->get_condition() ) {
223 newExpr = findSingleExpression( forStmt->get_condition(), *this );
224 delete forStmt->get_condition();
225 forStmt->set_condition( newExpr );
226 } // if
227
228 if ( forStmt->get_increment() ) {
229 newExpr = findVoidExpression( forStmt->get_increment(), *this );
230 delete forStmt->get_increment();
231 forStmt->set_increment( newExpr );
232 } // if
233
234 maybeAccept( forStmt->get_condition(), *this );
235 maybeAccept( forStmt->get_increment(), *this );
236 maybeAccept( forStmt->get_body(), *this );
237 leaveScope();
238 }
239
240 template< typename SwitchClass >
241 void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
242 Expression *newExpr;
243 newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
244 delete switchStmt->get_condition();
245 switchStmt->set_condition( newExpr );
246
247 visitor.Visitor::visit( switchStmt );
248 }
249
250 void Resolver::visit( SwitchStmt *switchStmt ) {
251 handleSwitchStmt( switchStmt, *this );
252 }
253
254 void Resolver::visit( ChooseStmt *switchStmt ) {
255 handleSwitchStmt( switchStmt, *this );
256 }
257
258 void Resolver::visit( CaseStmt *caseStmt ) {
259 Visitor::visit( caseStmt );
260 }
261
262 void Resolver::visit( ReturnStmt *returnStmt ) {
263 if ( returnStmt->get_expr() ) {
264 CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
265 cloneAll( functionReturn, castExpr->get_results() );
266 Expression *newExpr = findSingleExpression( castExpr, *this );
267 delete castExpr;
268 returnStmt->set_expr( newExpr );
269 } // if
270 }
271
272 void Resolver::visit( SingleInit *singleInit ) {
273 if ( singleInit->get_value() ) {
274#if 0
275 if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
276 string n = ne->get_name();
277 if (n == "0") {
278 initContext = new BasicType(Type::Qualifiers(),
279 BasicType::SignedInt);
280 } else {
281 DeclarationWithType * decl = lookupId(n);
282 initContext = decl->get_type();
283 }
284 } else if (ConstantExpr * e =
285 dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
286 Constant *c = e->get_constant();
287 initContext = c->get_type();
288 } else {
289 assert(0);
290 }
291#endif
292 CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
293 Expression *newExpr = findSingleExpression( castExpr, *this );
294 delete castExpr;
295 singleInit->set_value( newExpr );
296 } // if
297// singleInit->get_value()->accept( *this );
298 }
299
300 void Resolver::visit( ListInit *listInit ) {
301 Visitor::visit(listInit);
302#if 0
303 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
304 std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
305 for ( ; iter != listInit->end_initializers(); ++iter ) {
306 initContext = at->get_base();
307 (*iter)->accept( *this );
308 } // for
309 } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
310 StructDecl *baseStruct = st->get_baseStruct();
311 std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
312 std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
313 for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
314 if ( (*iter2)->get_designators().empty() ) {
315 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
316 initContext = dt->get_type();
317 (*iter2)->accept( *this );
318 ++iter1;
319 } else {
320 StructDecl *st = baseStruct;
321 iter1 = st->get_members().begin();
322 std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
323 for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
324 NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
325 assert( key );
326 for ( ; iter1 != st->get_members().end(); ++iter1 ) {
327 if ( key->get_name() == (*iter1)->get_name() ) {
328 (*iter1)->print( cout );
329 cout << key->get_name() << endl;
330 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
331 assert( fred );
332 StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
333 assert( mary );
334 st = mary->get_baseStruct();
335 iter1 = st->get_members().begin();
336 break;
337 } // if
338 } // for
339 } // for
340 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
341 assert( fred );
342 initContext = fred->get_type();
343 (*listInit->begin_initializers())->accept( *this );
344 } // if
345 } // for
346 } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
347 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
348 initContext = dt->get_type();
349 (*listInit->begin_initializers())->accept( *this );
350 } // if
351#endif
352 }
353} // namespace ResolvExpr
354
355// Local Variables: //
356// tab-width: 4 //
357// mode: c++ //
358// compile-command: "make install" //
359// End: //
Note: See TracBrowser for help on using the repository browser.