Changes in / [2cdf6dc:ad6343e]
- Location:
- src
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/utility.h
r2cdf6dc rad6343e 252 252 253 253 template< typename T > 254 struct reverse Iterate_t {254 struct reverse_iterate_t { 255 255 T& ref; 256 256 257 reverse Iterate_t( T & ref ) : ref(ref) {}257 reverse_iterate_t( T & ref ) : ref(ref) {} 258 258 259 259 typedef typename T::reverse_iterator iterator; … … 263 263 264 264 template< typename T > 265 reverseIterate_t< T > reverseIterate( T & ref ) { 266 return reverseIterate_t< T >( ref ); 267 } 268 265 reverse_iterate_t< T > reverseIterate( T & ref ) { 266 return reverse_iterate_t< T >( ref ); 267 } 268 269 // ----------------------------------------------------------------------------- 270 // Helper struct and function to support 271 // for ( val : group_iterate( container1, container2, ... ) ) {} 272 // syntax to have a for each that iterates multiple containers of the same length 273 // TODO: update to use variadic arguments 274 275 template< typename T1, typename T2 > 276 struct group_iterate_t { 277 group_iterate_t( const T1 & v1, const T2 & v2 ) : args(v1, v2) { 278 assertf(v1.size() == v2.size(), "group iteration requires containers of the same size."); 279 }; 280 281 struct iterator { 282 typedef std::tuple<typename T1::value_type, typename T2::value_type> value_type; 283 typedef typename T1::iterator T1Iter; 284 typedef typename T2::iterator T2Iter; 285 typedef std::tuple<T1Iter, T2Iter> IterTuple; 286 IterTuple it; 287 iterator( T1Iter i1, T2Iter i2 ) : it( i1, i2 ) {} 288 iterator operator++() { 289 return iterator( ++std::get<0>(it), ++std::get<1>(it) ); 290 } 291 bool operator!=( const iterator &other ) const { return it != other.it; } 292 value_type operator*() const { return std::make_tuple( *std::get<0>(it), *std::get<1>(it) ); } 293 }; 294 iterator begin() { return iterator( std::get<0>(args).begin(), std::get<1>(args).begin() ); } 295 iterator end() { return iterator( std::get<0>(args).end(), std::get<1>(args).end() ); } 296 297 private: 298 std::tuple<T1, T2> args; 299 }; 300 301 template< typename... Args > 302 group_iterate_t<Args...> group_iterate( const Args &... args ) { 303 return group_iterate_t<Args...>(args...); 304 } 269 305 #endif // _UTILITY_H 270 306 -
src/Parser/TypeData.cc
r2cdf6dc rad6343e 57 57 aggregate.actuals = nullptr; 58 58 aggregate.fields = nullptr; 59 aggregate.body = false; 59 60 break; 60 61 case AggregateInst: -
src/ResolvExpr/Unify.cc
r2cdf6dc rad6343e 123 123 } 124 124 125 struct CompleteTypeChecker : public Visitor {126 virtual void visit( VoidType *basicType ) { status = false; }127 virtual void visit( BasicType *basicType ) {}128 virtual void visit( PointerType *pointerType ) {}129 virtual void visit( ArrayType *arrayType ) { status = ! arrayType->get_isVarLen(); }130 virtual void visit( FunctionType *functionType ) {}131 virtual void visit( StructInstType *aggregateUseType ) { status = aggregateUseType->get_baseStruct()->has_body(); }132 virtual void visit( UnionInstType *aggregateUseType ) { status = aggregateUseType->get_baseUnion()->has_body(); }133 // xxx - enum inst does not currently contain a pointer to base, this should be fixed.134 virtual void visit( EnumInstType *aggregateUseType ) { /* status = aggregateUseType->get_baseEnum()->hasBody(); */ }135 virtual void visit( TraitInstType *aggregateUseType ) { assert( false ); }136 virtual void visit( TypeInstType *aggregateUseType ) { status = aggregateUseType->get_baseType()->isComplete(); }137 virtual void visit( TupleType *tupleType ) {} // xxx - not sure if this is right, might need to recursively check complete-ness138 virtual void visit( TypeofType *typeofType ) { assert( false ); }139 virtual void visit( AttrType *attrType ) { assert( false ); } // xxx - not sure what to do here140 virtual void visit( VarArgsType *varArgsType ){} // xxx - is this right?141 virtual void visit( ZeroType *zeroType ) {}142 virtual void visit( OneType *oneType ) {}143 bool status = true;144 };145 bool isComplete( Type * type ) {146 CompleteTypeChecker checker;147 assert( type );148 type->accept( checker );149 return checker.status;150 }151 152 125 bool tyVarCompatible( const TypeDecl::Data & data, Type *type, const SymTab::Indexer &indexer ) { 153 126 switch ( data.kind ) { … … 158 131 // type must also be complete 159 132 // xxx - should this also check that type is not a tuple type and that it's not a ttype? 160 return ! isFtype( type, indexer ) && (! data.isComplete || isComplete( type ));133 return ! isFtype( type, indexer ) && (! data.isComplete || type->isComplete() ); 161 134 case TypeDecl::Ftype: 162 135 return isFtype( type, indexer ); -
src/SymTab/Validate.cc
r2cdf6dc rad6343e 114 114 LinkReferenceToTypes( bool doDebug, const Indexer *indexer ); 115 115 private: 116 using Indexer::visit;116 using Parent::visit; 117 117 void visit( StructInstType *structInst ) final; 118 118 void visit( UnionInstType *unionInst ) final; … … 131 131 132 132 /// Replaces array and function types in forall lists by appropriate pointer type 133 class Pass3 : public Indexer {133 class Pass3 final : public Indexer { 134 134 typedef Indexer Parent; 135 135 public: 136 using Parent::visit; 136 137 Pass3( const Indexer *indexer ); 137 138 private: 138 virtual void visit( ObjectDecl *object ) ;139 virtual void visit( FunctionDecl *func ) ;139 virtual void visit( ObjectDecl *object ) override; 140 virtual void visit( FunctionDecl *func ) override; 140 141 141 142 const Indexer *indexer; … … 375 376 } 376 377 377 void LinkReferenceToTypes::visit( TraitInstType * contextInst ) {378 Parent::visit( contextInst );379 if ( contextInst->get_name() == "sized" ) {378 void LinkReferenceToTypes::visit( TraitInstType *traitInst ) { 379 Parent::visit( traitInst ); 380 if ( traitInst->get_name() == "sized" ) { 380 381 // "sized" is a special trait with no members - just flick the sized status on for the type variable 381 if ( contextInst->get_parameters().size() != 1 ) {382 throw SemanticError( "incorrect number of context parameters: ", contextInst );382 if ( traitInst->get_parameters().size() != 1 ) { 383 throw SemanticError( "incorrect number of trait parameters: ", traitInst ); 383 384 } 384 TypeExpr * param = safe_dynamic_cast< TypeExpr * > ( contextInst->get_parameters().front() );385 TypeExpr * param = safe_dynamic_cast< TypeExpr * > ( traitInst->get_parameters().front() ); 385 386 TypeInstType * inst = safe_dynamic_cast< TypeInstType * > ( param->get_type() ); 386 387 TypeDecl * decl = inst->get_baseType(); … … 389 390 return; 390 391 } 391 TraitDecl * ctx = indexer->lookupTrait( contextInst->get_name() );392 if ( ! ctx) {393 throw SemanticError( "use of undeclared context " + contextInst->get_name() );394 } // if 395 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i) {396 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {397 if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) {398 cloneAll( otherCtx->get_members(), contextInst->get_members() ); 399 } else{400 contextInst->get_members().push_back( (*assert )->clone() );401 } // if392 TraitDecl *traitDecl = indexer->lookupTrait( traitInst->get_name() ); 393 if ( ! traitDecl ) { 394 throw SemanticError( "use of undeclared trait " + traitInst->get_name() ); 395 } // if 396 if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) { 397 throw SemanticError( "incorrect number of trait parameters: ", traitInst ); 398 } // if 399 400 for ( TypeDecl * td : traitDecl->get_parameters() ) { 401 for ( DeclarationWithType * assert : td->get_assertions() ) { 402 traitInst->get_members().push_back( assert->clone() ); 402 403 } // for 403 404 } // for 404 405 405 if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) { 406 throw SemanticError( "incorrect number of context parameters: ", contextInst ); 407 } // if 408 409 // need to clone members of the context for ownership purposes 406 // need to clone members of the trait for ownership purposes 410 407 std::list< Declaration * > members; 411 std::transform( ctx->get_members().begin(), ctx->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); 412 413 applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( contextInst->get_members() ) ); 408 std::transform( traitDecl->get_members().begin(), traitDecl->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); 409 410 applySubstitution( traitDecl->get_parameters().begin(), traitDecl->get_parameters().end(), traitInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( traitInst->get_members() ) ); 411 412 // need to carry over the 'sized' status of each decl in the instance 413 for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) { 414 TypeExpr * expr = safe_dynamic_cast< TypeExpr * >( std::get<1>(p) ); 415 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { 416 TypeDecl * formalDecl = std::get<0>(p); 417 TypeDecl * instDecl = inst->get_baseType(); 418 if ( formalDecl->get_sized() ) instDecl->set_sized( true ); 419 } 420 } 414 421 } 415 422 … … 457 464 } 458 465 459 /// Fix up assertions 460 void forallFixer( Type * func ) {461 for ( Type ::ForallList::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type) {466 /// Fix up assertions - flattens assertion lists, removing all trait instances 467 void forallFixer( Type * func ) { 468 for ( TypeDecl * type : func->get_forall() ) { 462 469 std::list< DeclarationWithType * > toBeDone, nextRound; 463 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );470 toBeDone.splice( toBeDone.end(), type->get_assertions() ); 464 471 while ( ! toBeDone.empty() ) { 465 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion) {466 if ( TraitInstType * ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) {467 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {468 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );469 assert( dwt);472 for ( DeclarationWithType * assertion : toBeDone ) { 473 if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { 474 // expand trait instance into all of its members 475 for ( Declaration * member : traitInst->get_members() ) { 476 DeclarationWithType *dwt = safe_dynamic_cast< DeclarationWithType * >( member ); 470 477 nextRound.push_back( dwt->clone() ); 471 478 } 472 delete ctx;479 delete traitInst; 473 480 } else { 481 // pass assertion through 474 482 FixFunction fixer; 475 *assertion = (*assertion )->acceptMutator( fixer );483 assertion = assertion->acceptMutator( fixer ); 476 484 if ( fixer.get_isVoid() ) { 477 485 throw SemanticError( "invalid type void in assertion of function ", func ); 478 486 } 479 (*type )->get_assertions().push_back( *assertion );487 type->get_assertions().push_back( assertion ); 480 488 } // if 481 489 } // for -
src/SynTree/AggregateDecl.cc
r2cdf6dc rad6343e 69 69 std::string EnumDecl::typeString() const { return "enum"; } 70 70 71 std::string TraitDecl::typeString() const { return " context"; }71 std::string TraitDecl::typeString() const { return "trait"; } 72 72 73 73 // Local Variables: // -
src/SynTree/ReferenceToType.cc
r2cdf6dc rad6343e 64 64 } 65 65 66 bool StructInstType::isComplete() const { return baseStruct->has_body(); } 67 66 68 void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { 67 69 assert( baseStruct ); … … 90 92 } 91 93 94 bool UnionInstType::isComplete() const { return baseUnion->has_body(); } 95 92 96 void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { 93 97 assert( baseUnion ); … … 111 115 std::string EnumInstType::typeString() const { return "enum"; } 112 116 113 std::string TraitInstType::typeString() const { return " context"; }117 std::string TraitInstType::typeString() const { return "trait"; } 114 118 115 119 TraitInstType::TraitInstType( const TraitInstType &other ) : Parent( other ) { … … 120 124 deleteAll( members ); 121 125 } 126 127 bool TraitInstType::isComplete() const { assert( false ); } 122 128 123 129 TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType ) : Parent( tq, name ) { … … 143 149 std::string TypeInstType::typeString() const { return "type"; } 144 150 151 bool TypeInstType::isComplete() const { return baseType->isComplete(); } 152 145 153 void TypeInstType::print( std::ostream &os, int indent ) const { 146 154 using std::endl; -
src/SynTree/Type.h
r2cdf6dc rad6343e 74 74 virtual Type * getComponent( unsigned i ) { assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i ); return this; } 75 75 76 virtual bool isComplete() const { return true; } 77 76 78 virtual Type *clone() const = 0; 77 79 virtual void accept( Visitor &v ) = 0; … … 90 92 91 93 virtual unsigned size() const { return 0; }; 94 virtual bool isComplete() const { return false; } 92 95 93 96 virtual VoidType *clone() const { return new VoidType( *this ); } … … 185 188 void set_isStatic( bool newValue ) { isStatic = newValue; } 186 189 190 virtual bool isComplete() const { return ! isVarLen; } 191 187 192 virtual ArrayType *clone() const { return new ArrayType( *this ); } 188 193 virtual void accept( Visitor &v ) { v.visit( this ); } … … 258 263 std::list<TypeDecl*> * get_baseParameters(); 259 264 265 virtual bool isComplete() const; 266 260 267 /// Looks up the members of this struct named "name" and places them into "foundDecls". 261 268 /// Clones declarations into "foundDecls", caller responsible for freeing … … 287 294 std::list<TypeDecl*> * get_baseParameters(); 288 295 296 virtual bool isComplete() const; 297 289 298 /// looks up the members of this union named "name" and places them into "foundDecls" 290 299 /// Clones declarations into "foundDecls", caller responsible for freeing … … 310 319 EnumInstType( const EnumInstType &other ) : Parent( other ) {} 311 320 321 // xxx - enum inst does not currently contain a pointer to base, this should be fixed. 322 // virtual bool isComplete() const { return baseEnum()->hasBody(); } 323 312 324 virtual EnumInstType *clone() const { return new EnumInstType( *this ); } 313 325 virtual void accept( Visitor &v ) { v.visit( this ); } … … 325 337 326 338 std::list< Declaration* >& get_members() { return members; } 339 340 virtual bool isComplete() const; 327 341 328 342 virtual TraitInstType *clone() const { return new TraitInstType( *this ); } … … 349 363 bool get_isFtype() const { return isFtype; } 350 364 void set_isFtype( bool newValue ) { isFtype = newValue; } 365 366 virtual bool isComplete() const; 351 367 352 368 virtual TypeInstType *clone() const { return new TypeInstType( *this ); } … … 382 398 } 383 399 400 // virtual bool isComplete() const { return true; } // xxx - not sure if this is right, might need to recursively check complete-ness 401 384 402 virtual TupleType *clone() const { return new TupleType( *this ); } 385 403 virtual void accept( Visitor &v ) { v.visit( this ); } … … 398 416 Expression *get_expr() const { return expr; } 399 417 void set_expr( Expression *newValue ) { expr = newValue; } 418 419 virtual bool isComplete() const { assert( false ); return false; } 400 420 401 421 virtual TypeofType *clone() const { return new TypeofType( *this ); } … … 423 443 void set_isType( bool newValue ) { isType = newValue; } 424 444 445 virtual bool isComplete() const { assert( false ); } // xxx - not sure what to do here 446 425 447 virtual AttrType *clone() const { return new AttrType( *this ); } 426 448 virtual void accept( Visitor &v ) { v.visit( this ); } … … 439 461 VarArgsType(); 440 462 VarArgsType( Type::Qualifiers tq ); 463 464 virtual bool isComplete() const{ return true; } // xxx - is this right? 441 465 442 466 virtual VarArgsType *clone() const { return new VarArgsType( *this ); } -
src/tests/vector/array.c
r2cdf6dc rad6343e 16 16 #include "array.h" 17 17 18 /// forall( otype array_type, elt_type | bounded_array( array_type, elt_type ) ) 19 /// [ array_iterator begin, array_iterator end ] 20 /// get_iterators( array_type array ) 21 /// { 22 /// begin = 0; 23 /// end = last( array ); 24 /// } 18 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 19 [ elt_type * begin, elt_type * end ] get_iterators( array_type * array ) { 20 return [ begin( array ), end( array ) ]; 21 } 25 22 26 23 // The first element is always at index 0. -
src/tests/vector/array.h
r2cdf6dc rad6343e 32 32 // implement iterator_for 33 33 34 typedef int array_iterator; 35 36 /// forall( otype array_type, elt_type | bounded_array( array_type, elt_type ) ) 37 /// [ array_iterator begin, array_iterator end ] get_iterators( array_type ); 34 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 35 [ elt_type * begin, elt_type * end ] get_iterators( array_type * ); 38 36 39 37
Note: See TracChangeset
for help on using the changeset viewer.