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