Changeset 6b224a52 for src/SynTree
- Timestamp:
- Aug 25, 2017, 12:11:53 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- bf7b9da7
- Parents:
- 135b431 (diff), f676b84 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/SynTree
- Files:
-
- 1 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/AddressExpr.cc
r135b431 r6b224a52 21 21 #include "Type.h" // for PointerType, Type, Type::Qualifiers 22 22 23 // Address expressions are typed based on the following inference rules: 24 // E : lvalue T &..& (n references) 25 // &E : T *&..& (n references) 26 // 27 // E : T &..& (m references) 28 // &E : T *&..& (m-1 references) 29 // 30 // That is, lvalues becomes 31 32 namespace { 33 Type * addrType( Type * type ) { 34 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { 35 return new ReferenceType( refType->get_qualifiers(), addrType( refType->get_base() ) ); 36 } else { 37 return new PointerType( Type::Qualifiers(), type->clone() ); 38 } 39 } 40 } 41 23 42 AddressExpr::AddressExpr( Expression *arg, Expression *_aname ) : Expression( _aname ), arg( arg ) { 24 43 if ( arg->has_result() ) { 25 set_result( new PointerType( Type::Qualifiers(), arg->get_result()->clone() ) ); 44 if ( arg->get_result()->get_lvalue() ) { 45 // lvalue, retains all layers of reference and gains a pointer inside the references 46 set_result( addrType( arg->get_result() ) ); 47 } else { 48 // taking address of non-lvalue -- must be a reference, loses one layer of reference 49 ReferenceType * refType = safe_dynamic_cast< ReferenceType * >( arg->get_result() ); 50 set_result( addrType( refType->get_base() ) ); 51 } 52 // result of & is never an lvalue 53 get_result()->set_lvalue( false ); 26 54 } 27 55 } -
src/SynTree/ApplicationExpr.cc
r135b431 r6b224a52 49 49 } 50 50 51 ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list< Expression * > & argList ) : function( funcExpr ), args( argList) {51 ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list<Expression *> & args ) : function( funcExpr ), args( args ) { 52 52 PointerType *pointer = safe_dynamic_cast< PointerType* >( funcExpr->get_result() ); 53 53 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); -
src/SynTree/Declaration.h
r135b431 r6b224a52 109 109 virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; 110 110 111 virtual Type * get_type() const = 0;111 virtual Type * get_type() const = 0; 112 112 virtual void set_type(Type *) = 0; 113 113 … … 128 128 virtual ~ObjectDecl(); 129 129 130 virtual Type * get_type() const { return type; }130 virtual Type * get_type() const { return type; } 131 131 virtual void set_type(Type *newType) { type = newType; } 132 132 … … 155 155 virtual ~FunctionDecl(); 156 156 157 Type * get_type() const;157 Type * get_type() const; 158 158 virtual void set_type(Type *); 159 159 160 FunctionType * get_functionType() const { return type; }160 FunctionType * get_functionType() const { return type; } 161 161 void set_functionType( FunctionType *newValue ) { type = newValue; } 162 162 CompoundStmt *get_statements() const { return statements; } -
src/SynTree/Expression.cc
r135b431 r6b224a52 31 31 #include "TypeSubstitution.h" // for TypeSubstitution 32 32 33 #include "GenPoly/Lvalue.h" 33 34 34 35 Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {} … … 89 90 } 90 91 92 VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) { 93 VariableExpr * funcExpr = new VariableExpr( func ); 94 funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) ); 95 return funcExpr; 96 } 97 91 98 void VariableExpr::print( std::ostream &os, int indent ) const { 92 99 os << "Variable Expression: "; … … 149 156 150 157 void AlignofExpr::print( std::ostream &os, int indent) const { 151 os << std::string( indent, ' ' ) <<"Alignof Expression on: ";158 os << "Alignof Expression on: "; 152 159 153 160 if (isType) … … 258 265 259 266 void AttrExpr::print( std::ostream &os, int indent) const { 260 os << std::string( indent, ' ' ) <<"Attr ";267 os << "Attr "; 261 268 attr->print( os, indent + 2 ); 262 269 if ( isType || expr ) { … … 357 364 namespace { 358 365 TypeSubstitution makeSub( Type * t ) { 359 if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) { 366 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) { 367 return makeSub( refType->get_base() ); 368 } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) { 360 369 return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() ); 361 370 } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) { … … 422 431 if ( Type * type = expr->get_result() ) { 423 432 Type * base = InitTweak::getPointerBase( type ); 424 if ( ! base ) { 425 std::cerr << type << std::endl; 433 assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() ); 434 ret->set_result( base->clone() ); 435 if ( GenPoly::referencesPermissable() ) { 436 // if references are still allowed in the AST, dereference returns a reference 437 ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) ); 438 } else { 439 // references have been removed, in which case dereference returns an lvalue of the base type. 440 ret->get_result()->set_lvalue( true ); 426 441 } 427 assertf( base, "expected pointer type in dereference\n" );428 ret->set_result( maybeClone( base ) );429 442 } 430 443 return ret; … … 490 503 491 504 void LogicalExpr::print( std::ostream &os, int indent )const { 492 os << std::string( indent, ' ' ) <<"Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";505 os << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; 493 506 arg1->print(os); 494 507 os << " and "; … … 595 608 CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) { 596 609 assert( type && initializer ); 610 type->set_lvalue( true ); 597 611 set_result( type ); 598 612 } -
src/SynTree/Expression.h
r135b431 r6b224a52 284 284 void set_var( DeclarationWithType * newValue ) { var = newValue; } 285 285 286 static VariableExpr * functionPointer( FunctionDecl * decl ); 287 286 288 virtual VariableExpr * clone() const { return new VariableExpr( * this ); } 287 289 virtual void accept( Visitor & v ) { v.visit( this ); } -
src/SynTree/Mutator.cc
r135b431 r6b224a52 502 502 } 503 503 504 Type * Mutator::mutate( FunctionType *functionType ) { 504 Type * Mutator::mutate( ReferenceType * refType ) { 505 mutateAll( refType->get_forall(), *this ); 506 refType->set_base( maybeMutate( refType->get_base(), *this ) ); 507 return refType; 508 } 509 510 Type * Mutator::mutate( FunctionType * functionType ) { 505 511 mutateAll( functionType->get_forall(), *this ); 506 512 mutateAll( functionType->get_returnVals(), *this ); -
src/SynTree/Mutator.h
r135b431 r6b224a52 93 93 virtual Type* mutate( PointerType *pointerType ); 94 94 virtual Type* mutate( ArrayType *arrayType ); 95 virtual Type* mutate( ReferenceType *refType ); 95 96 virtual Type* mutate( FunctionType *functionType ); 96 97 virtual Type* mutate( StructInstType *aggregateUseType ); -
src/SynTree/SynTree.h
r135b431 r6b224a52 102 102 class PointerType; 103 103 class ArrayType; 104 class ReferenceType; 104 105 class FunctionType; 105 106 class ReferenceToType; -
src/SynTree/TupleExpr.cc
r135b431 r6b224a52 67 67 assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested index %d in expr %s", type->size(), index, toString( tuple ).c_str() ); 68 68 set_result( (*std::next( type->get_types().begin(), index ))->clone() ); 69 get_result()->set_lvalue( type->get_lvalue() ); 69 // like MemberExpr, TupleIndexExpr is always an lvalue 70 get_result()->set_lvalue( true ); 70 71 } 71 72 -
src/SynTree/Type.cc
r135b431 r6b224a52 64 64 const char * Type::QualifiersNames[] = { "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic" }; 65 65 66 Type * Type::stripDeclarator() {66 Type * Type::stripDeclarator() { 67 67 Type * type = this; 68 68 while ( Type * at = InitTweak::getPointerBase( type ) ) { … … 71 71 return type; 72 72 } 73 74 Type * Type::stripReferences() { 75 Type * type = this; 76 while ( ReferenceType * ref = dynamic_cast<ReferenceType *>( type ) ) { 77 type = ref->get_base(); 78 } 79 return type; 80 } 81 82 int Type::referenceDepth() const { return 0; } 73 83 74 84 void Type::print( std::ostream &os, int indent ) const { -
src/SynTree/Type.h
r135b431 r6b224a52 168 168 169 169 /// return type without outer pointers and arrays 170 Type *stripDeclarator(); 170 Type * stripDeclarator(); 171 172 /// return type without outer references 173 Type * stripReferences(); 174 175 /// return the number of references occuring consecutively on the outermost layer of this type (i.e. do not count references nested within other types) 176 virtual int referenceDepth() const; 171 177 172 178 virtual bool isComplete() const { return true; } … … 262 268 bool is_array() const { return isStatic || isVarLen || dimension; } 263 269 270 virtual bool isComplete() const { return ! isVarLen; } 271 264 272 virtual PointerType *clone() const { return new PointerType( *this ); } 265 273 virtual void accept( Visitor & v ) { v.visit( this ); } … … 296 304 }; 297 305 306 class ReferenceType : public Type { 307 public: 308 Type *base; 309 310 ReferenceType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); 311 ReferenceType( const ReferenceType & ); 312 virtual ~ReferenceType(); 313 314 Type *get_base() { return base; } 315 void set_base( Type *newValue ) { base = newValue; } 316 317 virtual int referenceDepth() const; 318 319 // Since reference types act like value types, their size is the size of the base. 320 // This makes it simple to cast the empty tuple to a reference type, since casts that increase 321 // the number of values are disallowed. 322 virtual unsigned size() const { return base->size(); } 323 324 virtual ReferenceType *clone() const { return new ReferenceType( *this ); } 325 virtual void accept( Visitor & v ) { v.visit( this ); } 326 virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } 327 virtual void print( std::ostream & os, int indent = 0 ) const; 328 }; 329 298 330 class FunctionType : public Type { 299 331 public: -
src/SynTree/TypeExpr.cc
r135b431 r6b224a52 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // TypeExpr.cc -- 7 // TypeExpr.cc -- 8 8 // 9 9 // Author : Richard C. Bilson -
src/SynTree/TypeSubstitution.h
r135b431 r6b224a52 117 117 } // if 118 118 } else { 119 throw SemanticError( "Attempt to provide non-type parameter for type parameter", formal );119 throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal ); 120 120 } // if 121 121 } else { -
src/SynTree/Visitor.cc
r135b431 r6b224a52 389 389 void Visitor::visit( PointerType *pointerType ) { 390 390 acceptAll( pointerType->get_forall(), *this ); 391 // xxx - should PointerType visit/mutate dimension? 391 392 maybeAccept( pointerType->get_base(), *this ); 392 393 } … … 396 397 maybeAccept( arrayType->get_dimension(), *this ); 397 398 maybeAccept( arrayType->get_base(), *this ); 399 } 400 401 void Visitor::visit( ReferenceType *refType ) { 402 acceptAll( refType->get_forall(), *this ); 403 maybeAccept( refType->get_base(), *this ); 398 404 } 399 405 -
src/SynTree/Visitor.h
r135b431 r6b224a52 95 95 virtual void visit( PointerType *pointerType ); 96 96 virtual void visit( ArrayType *arrayType ); 97 virtual void visit( ReferenceType *refType ); 97 98 virtual void visit( FunctionType *functionType ); 98 99 virtual void visit( StructInstType *aggregateUseType ); -
src/SynTree/module.mk
r135b431 r6b224a52 20 20 SynTree/PointerType.cc \ 21 21 SynTree/ArrayType.cc \ 22 SynTree/ReferenceType.cc \ 22 23 SynTree/FunctionType.cc \ 23 24 SynTree/ReferenceToType.cc \
Note:
See TracChangeset
for help on using the changeset viewer.