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