source: src/ResolvExpr/Resolver.cc@ a8541d9

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 a8541d9 was bfbf97f, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

recursively resolve the type of array dimensions

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