[a32b204] | 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 | //
|
---|
[71f4e4f] | 7 | // Resolver.cc --
|
---|
[a32b204] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 12:17:01 2015
|
---|
[71f4e4f] | 11 | // Last Modified By : Rob Schluntz
|
---|
[db4ecc5] | 12 | // Last Modified On : Thu Apr 14 11:18:12 2016
|
---|
[f1e012b] | 13 | // Update Count : 203
|
---|
[a32b204] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 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"
|
---|
[d3b7937] | 26 | #include "Common/utility.h"
|
---|
[51b73452] | 27 |
|
---|
[d9a0e76] | 28 | #include <iostream>
|
---|
| 29 | using namespace std;
|
---|
[51b73452] | 30 |
|
---|
[d9a0e76] | 31 | namespace ResolvExpr {
|
---|
[a32b204] | 32 | class Resolver : public SymTab::Indexer {
|
---|
| 33 | public:
|
---|
| 34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
|
---|
[71f4e4f] | 35 |
|
---|
[a32b204] | 36 | virtual void visit( FunctionDecl *functionDecl );
|
---|
| 37 | virtual void visit( ObjectDecl *functionDecl );
|
---|
| 38 | virtual void visit( TypeDecl *typeDecl );
|
---|
[94b4364] | 39 |
|
---|
[bfbf97f] | 40 | virtual void visit( ArrayType * at );
|
---|
[d9a0e76] | 41 |
|
---|
[a32b204] | 42 | virtual void visit( ExprStmt *exprStmt );
|
---|
[7f5566b] | 43 | virtual void visit( AsmExpr *asmExpr );
|
---|
| 44 | virtual void visit( AsmStmt *asmStmt );
|
---|
[a32b204] | 45 | virtual void visit( IfStmt *ifStmt );
|
---|
| 46 | virtual void visit( WhileStmt *whileStmt );
|
---|
| 47 | virtual void visit( ForStmt *forStmt );
|
---|
| 48 | virtual void visit( SwitchStmt *switchStmt );
|
---|
| 49 | virtual void visit( ChooseStmt *switchStmt );
|
---|
| 50 | virtual void visit( CaseStmt *caseStmt );
|
---|
[de62360d] | 51 | virtual void visit( BranchStmt *branchStmt );
|
---|
[a32b204] | 52 | virtual void visit( ReturnStmt *returnStmt );
|
---|
[d9a0e76] | 53 |
|
---|
[a32b204] | 54 | virtual void visit( SingleInit *singleInit );
|
---|
| 55 | virtual void visit( ListInit *listInit );
|
---|
[71f4e4f] | 56 | virtual void visit( ConstructorInit *ctorInit );
|
---|
[a32b204] | 57 | private:
|
---|
[94b4364] | 58 | typedef std::list< Initializer * >::iterator InitIterator;
|
---|
| 59 |
|
---|
| 60 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
|
---|
| 61 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
|
---|
[f1e012b] | 62 | void fallbackInit( ConstructorInit * ctorInit );
|
---|
[a32b204] | 63 | std::list< Type * > functionReturn;
|
---|
| 64 | Type *initContext;
|
---|
| 65 | Type *switchType;
|
---|
| 66 | };
|
---|
[d9a0e76] | 67 |
|
---|
[a32b204] | 68 | void resolve( std::list< Declaration * > translationUnit ) {
|
---|
| 69 | Resolver resolver;
|
---|
| 70 | acceptAll( translationUnit, resolver );
|
---|
[d9a0e76] | 71 | #if 0
|
---|
[a32b204] | 72 | resolver.print( cerr );
|
---|
| 73 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
|
---|
| 74 | (*i)->print( std::cerr );
|
---|
| 75 | (*i)->accept( resolver );
|
---|
| 76 | } // for
|
---|
[d9a0e76] | 77 | #endif
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[a32b204] | 80 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
|
---|
| 81 | TypeEnvironment env;
|
---|
| 82 | return resolveInVoidContext( expr, indexer, env );
|
---|
[d9a0e76] | 83 | }
|
---|
[a32b204] | 84 |
|
---|
[db4ecc5] | 85 |
|
---|
[a32b204] | 86 | namespace {
|
---|
| 87 | void finishExpr( Expression *expr, const TypeEnvironment &env ) {
|
---|
| 88 | expr->set_env( new TypeSubstitution );
|
---|
| 89 | env.makeSubstitution( *expr->get_env() );
|
---|
| 90 | }
|
---|
[db4ecc5] | 91 | } // namespace
|
---|
[a32b204] | 92 |
|
---|
[db4ecc5] | 93 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
---|
| 94 | global_renamer.reset();
|
---|
| 95 | TypeEnvironment env;
|
---|
| 96 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
|
---|
| 97 | finishExpr( newExpr, env );
|
---|
| 98 | return newExpr;
|
---|
| 99 | }
|
---|
[71f4e4f] | 100 |
|
---|
[db4ecc5] | 101 | namespace {
|
---|
[a32b204] | 102 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
---|
| 103 | TypeEnvironment env;
|
---|
| 104 | AlternativeFinder finder( indexer, env );
|
---|
| 105 | finder.find( untyped );
|
---|
[d9a0e76] | 106 | #if 0
|
---|
[a32b204] | 107 | if ( finder.get_alternatives().size() != 1 ) {
|
---|
| 108 | std::cout << "untyped expr is ";
|
---|
| 109 | untyped->print( std::cout );
|
---|
| 110 | std::cout << std::endl << "alternatives are:";
|
---|
| 111 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
---|
| 112 | i->print( std::cout );
|
---|
| 113 | } // for
|
---|
| 114 | } // if
|
---|
[d9a0e76] | 115 | #endif
|
---|
[a32b204] | 116 | assert( finder.get_alternatives().size() == 1 );
|
---|
| 117 | Alternative &choice = finder.get_alternatives().front();
|
---|
| 118 | Expression *newExpr = choice.expr->clone();
|
---|
| 119 | finishExpr( newExpr, choice.env );
|
---|
| 120 | return newExpr;
|
---|
| 121 | }
|
---|
[d9a0e76] | 122 |
|
---|
[a32b204] | 123 | bool isIntegralType( Type *type ) {
|
---|
| 124 | if ( dynamic_cast< EnumInstType * >( type ) ) {
|
---|
| 125 | return true;
|
---|
| 126 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
|
---|
| 127 | return bt->isInteger();
|
---|
| 128 | } else {
|
---|
| 129 | return false;
|
---|
| 130 | } // if
|
---|
| 131 | }
|
---|
[71f4e4f] | 132 |
|
---|
[a32b204] | 133 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
---|
| 134 | TypeEnvironment env;
|
---|
| 135 | AlternativeFinder finder( indexer, env );
|
---|
| 136 | finder.find( untyped );
|
---|
[d9a0e76] | 137 | #if 0
|
---|
[a32b204] | 138 | if ( finder.get_alternatives().size() != 1 ) {
|
---|
| 139 | std::cout << "untyped expr is ";
|
---|
| 140 | untyped->print( std::cout );
|
---|
| 141 | std::cout << std::endl << "alternatives are:";
|
---|
| 142 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
---|
| 143 | i->print( std::cout );
|
---|
| 144 | } // for
|
---|
| 145 | } // if
|
---|
[d9a0e76] | 146 | #endif
|
---|
[a32b204] | 147 | Expression *newExpr = 0;
|
---|
| 148 | const TypeEnvironment *newEnv = 0;
|
---|
| 149 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
---|
| 150 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
|
---|
| 151 | if ( newExpr ) {
|
---|
| 152 | throw SemanticError( "Too many interpretations for case control expression", untyped );
|
---|
| 153 | } else {
|
---|
| 154 | newExpr = i->expr->clone();
|
---|
| 155 | newEnv = &i->env;
|
---|
| 156 | } // if
|
---|
| 157 | } // if
|
---|
| 158 | } // for
|
---|
| 159 | if ( ! newExpr ) {
|
---|
| 160 | throw SemanticError( "No interpretations for case control expression", untyped );
|
---|
| 161 | } // if
|
---|
| 162 | finishExpr( newExpr, *newEnv );
|
---|
| 163 | return newExpr;
|
---|
| 164 | }
|
---|
[71f4e4f] | 165 |
|
---|
[a32b204] | 166 | }
|
---|
[71f4e4f] | 167 |
|
---|
[a32b204] | 168 | void Resolver::visit( ObjectDecl *objectDecl ) {
|
---|
| 169 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
|
---|
| 170 | objectDecl->set_type( new_type );
|
---|
[3cfe27f] | 171 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
|
---|
| 172 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
|
---|
| 173 | // initContext because of a function type can contain object declarations in the return and parameter types. So
|
---|
| 174 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
|
---|
| 175 | // the RHS.
|
---|
| 176 | Type *temp = initContext;
|
---|
[a32b204] | 177 | initContext = new_type;
|
---|
| 178 | SymTab::Indexer::visit( objectDecl );
|
---|
[3cfe27f] | 179 | initContext = temp;
|
---|
[bfbf97f] | 180 | }
|
---|
| 181 |
|
---|
| 182 | void Resolver::visit( ArrayType * at ) {
|
---|
| 183 | if ( at->get_dimension() ) {
|
---|
| 184 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
| 185 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
|
---|
| 186 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
---|
| 187 | delete at->get_dimension();
|
---|
| 188 | at->set_dimension( newExpr );
|
---|
[d1d17f5] | 189 | }
|
---|
[bfbf97f] | 190 | Visitor::visit( at );
|
---|
[a32b204] | 191 | }
|
---|
[94b4364] | 192 |
|
---|
[a32b204] | 193 | void Resolver::visit( TypeDecl *typeDecl ) {
|
---|
| 194 | if ( typeDecl->get_base() ) {
|
---|
| 195 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
|
---|
| 196 | typeDecl->set_base( new_type );
|
---|
| 197 | } // if
|
---|
| 198 | SymTab::Indexer::visit( typeDecl );
|
---|
| 199 | }
|
---|
[94b4364] | 200 |
|
---|
[a32b204] | 201 | void Resolver::visit( FunctionDecl *functionDecl ) {
|
---|
[d9a0e76] | 202 | #if 0
|
---|
[a32b204] | 203 | std::cout << "resolver visiting functiondecl ";
|
---|
| 204 | functionDecl->print( std::cout );
|
---|
| 205 | std::cout << std::endl;
|
---|
[d9a0e76] | 206 | #endif
|
---|
[a32b204] | 207 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
|
---|
| 208 | functionDecl->set_type( new_type );
|
---|
| 209 | std::list< Type * > oldFunctionReturn = functionReturn;
|
---|
| 210 | functionReturn.clear();
|
---|
| 211 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
|
---|
| 212 | functionReturn.push_back( (*i)->get_type() );
|
---|
| 213 | } // for
|
---|
| 214 | SymTab::Indexer::visit( functionDecl );
|
---|
| 215 | functionReturn = oldFunctionReturn;
|
---|
| 216 | }
|
---|
[51b73452] | 217 |
|
---|
[a32b204] | 218 | void Resolver::visit( ExprStmt *exprStmt ) {
|
---|
| 219 | if ( exprStmt->get_expr() ) {
|
---|
| 220 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
|
---|
| 221 | delete exprStmt->get_expr();
|
---|
| 222 | exprStmt->set_expr( newExpr );
|
---|
| 223 | } // if
|
---|
| 224 | }
|
---|
[51b73452] | 225 |
|
---|
[7f5566b] | 226 | void Resolver::visit( AsmExpr *asmExpr ) {
|
---|
| 227 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
|
---|
| 228 | delete asmExpr->get_operand();
|
---|
| 229 | asmExpr->set_operand( newExpr );
|
---|
| 230 | if ( asmExpr->get_inout() ) {
|
---|
| 231 | newExpr = findVoidExpression( asmExpr->get_inout(), *this );
|
---|
| 232 | delete asmExpr->get_inout();
|
---|
| 233 | asmExpr->set_inout( newExpr );
|
---|
| 234 | } // if
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | void Resolver::visit( AsmStmt *asmStmt ) {
|
---|
| 238 | acceptAll( asmStmt->get_input(), *this);
|
---|
| 239 | acceptAll( asmStmt->get_output(), *this);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[a32b204] | 242 | void Resolver::visit( IfStmt *ifStmt ) {
|
---|
| 243 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
|
---|
| 244 | delete ifStmt->get_condition();
|
---|
| 245 | ifStmt->set_condition( newExpr );
|
---|
| 246 | Visitor::visit( ifStmt );
|
---|
| 247 | }
|
---|
[51b73452] | 248 |
|
---|
[a32b204] | 249 | void Resolver::visit( WhileStmt *whileStmt ) {
|
---|
| 250 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
|
---|
| 251 | delete whileStmt->get_condition();
|
---|
| 252 | whileStmt->set_condition( newExpr );
|
---|
| 253 | Visitor::visit( whileStmt );
|
---|
| 254 | }
|
---|
[51b73452] | 255 |
|
---|
[a32b204] | 256 | void Resolver::visit( ForStmt *forStmt ) {
|
---|
[145f1fc] | 257 | SymTab::Indexer::visit( forStmt );
|
---|
| 258 |
|
---|
[a32b204] | 259 | if ( forStmt->get_condition() ) {
|
---|
[145f1fc] | 260 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
|
---|
[a32b204] | 261 | delete forStmt->get_condition();
|
---|
| 262 | forStmt->set_condition( newExpr );
|
---|
| 263 | } // if
|
---|
[71f4e4f] | 264 |
|
---|
[a32b204] | 265 | if ( forStmt->get_increment() ) {
|
---|
[145f1fc] | 266 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
|
---|
[a32b204] | 267 | delete forStmt->get_increment();
|
---|
| 268 | forStmt->set_increment( newExpr );
|
---|
| 269 | } // if
|
---|
| 270 | }
|
---|
[51b73452] | 271 |
|
---|
[a32b204] | 272 | template< typename SwitchClass >
|
---|
| 273 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
|
---|
| 274 | Expression *newExpr;
|
---|
| 275 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
|
---|
| 276 | delete switchStmt->get_condition();
|
---|
| 277 | switchStmt->set_condition( newExpr );
|
---|
[71f4e4f] | 278 |
|
---|
[a32b204] | 279 | visitor.Visitor::visit( switchStmt );
|
---|
| 280 | }
|
---|
[51b73452] | 281 |
|
---|
[a32b204] | 282 | void Resolver::visit( SwitchStmt *switchStmt ) {
|
---|
| 283 | handleSwitchStmt( switchStmt, *this );
|
---|
| 284 | }
|
---|
[51b73452] | 285 |
|
---|
[a32b204] | 286 | void Resolver::visit( ChooseStmt *switchStmt ) {
|
---|
| 287 | handleSwitchStmt( switchStmt, *this );
|
---|
| 288 | }
|
---|
[51b73452] | 289 |
|
---|
[a32b204] | 290 | void Resolver::visit( CaseStmt *caseStmt ) {
|
---|
| 291 | Visitor::visit( caseStmt );
|
---|
| 292 | }
|
---|
[51b73452] | 293 |
|
---|
[de62360d] | 294 | void Resolver::visit( BranchStmt *branchStmt ) {
|
---|
| 295 | // must resolve the argument for a computed goto
|
---|
| 296 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
|
---|
[2871210] | 297 | if ( Expression * arg = branchStmt->get_computedTarget() ) {
|
---|
[de62360d] | 298 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
|
---|
| 299 | PointerType pt( Type::Qualifiers(), v.clone() );
|
---|
| 300 | CastExpr * castExpr = new CastExpr( arg, pt.clone() );
|
---|
| 301 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
|
---|
| 302 | branchStmt->set_target( newExpr );
|
---|
| 303 | } // if
|
---|
| 304 | } // if
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[a32b204] | 307 | void Resolver::visit( ReturnStmt *returnStmt ) {
|
---|
| 308 | if ( returnStmt->get_expr() ) {
|
---|
| 309 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
|
---|
| 310 | cloneAll( functionReturn, castExpr->get_results() );
|
---|
| 311 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
---|
| 312 | delete castExpr;
|
---|
| 313 | returnStmt->set_expr( newExpr );
|
---|
| 314 | } // if
|
---|
| 315 | }
|
---|
[51b73452] | 316 |
|
---|
[b5c5684] | 317 | template< typename T >
|
---|
| 318 | bool isCharType( T t ) {
|
---|
| 319 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
|
---|
[71f4e4f] | 320 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
|
---|
[b5c5684] | 321 | bt->get_kind() == BasicType::UnsignedChar;
|
---|
| 322 | }
|
---|
| 323 | return false;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[a32b204] | 326 | void Resolver::visit( SingleInit *singleInit ) {
|
---|
| 327 | if ( singleInit->get_value() ) {
|
---|
[bdd516a] | 328 | #if 0
|
---|
[a32b204] | 329 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
|
---|
| 330 | string n = ne->get_name();
|
---|
| 331 | if (n == "0") {
|
---|
[71f4e4f] | 332 | initContext = new BasicType(Type::Qualifiers(),
|
---|
[a32b204] | 333 | BasicType::SignedInt);
|
---|
| 334 | } else {
|
---|
[52f85e0] | 335 | DeclarationWithType * decl = lookupId( n );
|
---|
[a32b204] | 336 | initContext = decl->get_type();
|
---|
| 337 | }
|
---|
[71f4e4f] | 338 | } else if (ConstantExpr * e =
|
---|
[a32b204] | 339 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
|
---|
| 340 | Constant *c = e->get_constant();
|
---|
| 341 | initContext = c->get_type();
|
---|
| 342 | } else {
|
---|
| 343 | assert(0);
|
---|
| 344 | }
|
---|
[bdd516a] | 345 | #endif
|
---|
[a32b204] | 346 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
|
---|
| 347 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
---|
| 348 | delete castExpr;
|
---|
| 349 | singleInit->set_value( newExpr );
|
---|
[b5c5684] | 350 |
|
---|
| 351 | // check if initializing type is char[]
|
---|
| 352 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
|
---|
| 353 | if ( isCharType( at->get_base() ) ) {
|
---|
| 354 | // check if the resolved type is char *
|
---|
| 355 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
|
---|
| 356 | if ( isCharType( pt->get_base() ) ) {
|
---|
[52f85e0] | 357 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
|
---|
[b5c5684] | 358 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
|
---|
| 359 | singleInit->set_value( ce->get_arg() );
|
---|
| 360 | ce->set_arg( NULL );
|
---|
[71f4e4f] | 361 | delete ce;
|
---|
[b5c5684] | 362 | }
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
[a32b204] | 366 | } // if
|
---|
[6c3744e] | 367 | // singleInit->get_value()->accept( *this );
|
---|
[a32b204] | 368 | }
|
---|
[51b73452] | 369 |
|
---|
[94b4364] | 370 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
|
---|
| 371 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
|
---|
| 372 | assert( dt );
|
---|
| 373 | initContext = dt->get_type();
|
---|
| 374 | try {
|
---|
| 375 | if ( init == initEnd ) return; // stop when there are no more initializers
|
---|
| 376 | (*init)->accept( *this );
|
---|
| 377 | ++init; // made it past an initializer
|
---|
| 378 | } catch( SemanticError & ) {
|
---|
| 379 | // need to delve deeper, if you can
|
---|
| 380 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
|
---|
| 381 | resolveAggrInit( sit->get_baseStruct(), init, initEnd );
|
---|
| 382 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
|
---|
| 383 | resolveAggrInit( uit->get_baseUnion(), init, initEnd );
|
---|
| 384 | } else {
|
---|
[1869adf] | 385 | // member is not an aggregate type, so can't go any deeper
|
---|
| 386 |
|
---|
[94b4364] | 387 | // might need to rethink what is being thrown
|
---|
| 388 | throw;
|
---|
| 389 | } // if
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
|
---|
| 394 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
|
---|
| 395 | // want to resolve each initializer to the members of the struct,
|
---|
| 396 | // but if there are more initializers than members we should stop
|
---|
| 397 | list< Declaration * >::iterator it = st->get_members().begin();
|
---|
| 398 | for ( ; it != st->get_members().end(); ++it) {
|
---|
| 399 | resolveSingleAggrInit( *it, init, initEnd );
|
---|
| 400 | }
|
---|
| 401 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
|
---|
| 402 | // only resolve to the first member of a union
|
---|
| 403 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
|
---|
| 404 | } // if
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | void Resolver::visit( ListInit * listInit ) {
|
---|
| 408 | InitIterator iter = listInit->begin_initializers();
|
---|
| 409 | InitIterator end = listInit->end_initializers();
|
---|
| 410 |
|
---|
[b5c5684] | 411 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
|
---|
[94b4364] | 412 | // resolve each member to the base type of the array
|
---|
| 413 | for ( ; iter != end; ++iter ) {
|
---|
[b5c5684] | 414 | initContext = at->get_base();
|
---|
| 415 | (*iter)->accept( *this );
|
---|
| 416 | } // for
|
---|
[94b4364] | 417 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
|
---|
| 418 | resolveAggrInit( st->get_baseStruct(), iter, end );
|
---|
[b5c5684] | 419 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
|
---|
[94b4364] | 420 | resolveAggrInit( st->get_baseUnion(), iter, end );
|
---|
[b5c5684] | 421 | } else {
|
---|
| 422 | // basic types are handled here
|
---|
| 423 | Visitor::visit( listInit );
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[bdd516a] | 426 | #if 0
|
---|
[a32b204] | 427 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
|
---|
| 428 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
|
---|
| 429 | for ( ; iter != listInit->end_initializers(); ++iter ) {
|
---|
| 430 | initContext = at->get_base();
|
---|
| 431 | (*iter)->accept( *this );
|
---|
| 432 | } // for
|
---|
| 433 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
|
---|
| 434 | StructDecl *baseStruct = st->get_baseStruct();
|
---|
| 435 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
|
---|
| 436 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
|
---|
| 437 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
|
---|
| 438 | if ( (*iter2)->get_designators().empty() ) {
|
---|
| 439 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
|
---|
| 440 | initContext = dt->get_type();
|
---|
| 441 | (*iter2)->accept( *this );
|
---|
| 442 | ++iter1;
|
---|
| 443 | } else {
|
---|
| 444 | StructDecl *st = baseStruct;
|
---|
| 445 | iter1 = st->get_members().begin();
|
---|
| 446 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
|
---|
| 447 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
|
---|
| 448 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
|
---|
| 449 | assert( key );
|
---|
| 450 | for ( ; iter1 != st->get_members().end(); ++iter1 ) {
|
---|
| 451 | if ( key->get_name() == (*iter1)->get_name() ) {
|
---|
| 452 | (*iter1)->print( cout );
|
---|
| 453 | cout << key->get_name() << endl;
|
---|
| 454 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
|
---|
| 455 | assert( fred );
|
---|
| 456 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
|
---|
| 457 | assert( mary );
|
---|
| 458 | st = mary->get_baseStruct();
|
---|
| 459 | iter1 = st->get_members().begin();
|
---|
| 460 | break;
|
---|
| 461 | } // if
|
---|
| 462 | } // for
|
---|
| 463 | } // for
|
---|
| 464 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
|
---|
| 465 | assert( fred );
|
---|
| 466 | initContext = fred->get_type();
|
---|
| 467 | (*listInit->begin_initializers())->accept( *this );
|
---|
| 468 | } // if
|
---|
| 469 | } // for
|
---|
| 470 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
|
---|
| 471 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
|
---|
| 472 | initContext = dt->get_type();
|
---|
| 473 | (*listInit->begin_initializers())->accept( *this );
|
---|
[2c2242c] | 474 | } // if
|
---|
[bdd516a] | 475 | #endif
|
---|
[a32b204] | 476 | }
|
---|
[71f4e4f] | 477 |
|
---|
[f1e012b] | 478 | // ConstructorInit - fall back on C-style initializer
|
---|
| 479 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
|
---|
| 480 | // could not find valid constructor, or found an intrinsic constructor
|
---|
| 481 | // fall back on C-style initializer
|
---|
| 482 | delete ctorInit->get_ctor();
|
---|
| 483 | ctorInit->set_ctor( NULL );
|
---|
| 484 | maybeAccept( ctorInit->get_init(), *this );
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[71f4e4f] | 487 | void Resolver::visit( ConstructorInit *ctorInit ) {
|
---|
[071a31a] | 488 | try {
|
---|
[5b2f5bb] | 489 | maybeAccept( ctorInit->get_ctor(), *this );
|
---|
| 490 | maybeAccept( ctorInit->get_dtor(), *this );
|
---|
[071a31a] | 491 | } catch ( SemanticError ) {
|
---|
| 492 | // no alternatives for the constructor initializer - fallback on C-style initializer
|
---|
| 493 | // xxx- not sure if this makes a ton of sense - should maybe never be able to have this situation?
|
---|
[f1e012b] | 494 | fallbackInit( ctorInit );
|
---|
[071a31a] | 495 | return;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[5b2f5bb] | 498 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * > ( ctorInit->get_ctor() ) ) {
|
---|
| 499 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() );
|
---|
| 500 | assert( appExpr );
|
---|
| 501 | VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() );
|
---|
| 502 | assert( function );
|
---|
| 503 | if ( LinkageSpec::isOverridable( function->get_var()->get_linkage() ) ) {
|
---|
| 504 | // if the constructor that was found is intrinsic or autogenerated, reset to C-style
|
---|
| 505 | // initializer so that code generation is easy to handle
|
---|
| 506 | fallbackInit( ctorInit );
|
---|
| 507 | return;
|
---|
[f1e012b] | 508 | }
|
---|
[71f4e4f] | 509 | }
|
---|
[5b2f5bb] | 510 | // found a constructor - can get rid of C-style initializer
|
---|
| 511 | delete ctorInit->get_init();
|
---|
| 512 | ctorInit->set_init( NULL );
|
---|
[71f4e4f] | 513 | }
|
---|
[51b73452] | 514 | } // namespace ResolvExpr
|
---|
[a32b204] | 515 |
|
---|
| 516 | // Local Variables: //
|
---|
| 517 | // tab-width: 4 //
|
---|
| 518 | // mode: c++ //
|
---|
| 519 | // compile-command: "make install" //
|
---|
| 520 | // End: //
|
---|