Changeset 5b51f5e for src/ResolvExpr
- Timestamp:
- Jan 5, 2018, 3:21:42 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:
- 65deb18, cae28da
- Parents:
- 5c4f2c2 (diff), b834e98 (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/ResolvExpr
- Files:
-
- 8 edited
-
CommonType.cc (modified) (6 diffs)
-
ConversionCost.cc (modified) (5 diffs)
-
CurrentObject.cc (modified) (4 diffs)
-
FindOpenVars.cc (modified) (3 diffs)
-
Occurs.cc (modified) (3 diffs)
-
PolyCost.cc (modified) (1 diff)
-
PtrsAssignable.cc (modified) (1 diff)
-
Resolver.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/CommonType.cc
r5c4f2c2 r5b51f5e 91 91 // special case where one type has a reference depth of 1 larger than the other 92 92 if ( diff > 0 || diff < 0 ) { 93 // std::cerr << "reference depth diff: " << diff << std::endl; 93 94 Type * result = nullptr; 94 if ( ReferenceType * ref1 = dynamic_cast< ReferenceType * >( type1 ) ) { 95 ReferenceType * ref1 = dynamic_cast< ReferenceType * >( type1 ); 96 ReferenceType * ref2 = dynamic_cast< ReferenceType * >( type2 ); 97 if ( diff > 0 ) { 98 // deeper on the left 99 assert( ref1 ); 100 result = handleReference( ref1->base, type2, widenFirst, widenSecond, indexer, env, openVars ); 101 } else { 102 // deeper on the right 103 assert( ref2 ); 104 result = handleReference( type1, ref2->base, widenFirst, widenSecond, indexer, env, openVars ); 105 } 106 if ( result && ref1 ) { 95 107 // formal is reference, so result should be reference 96 result = handleReference( ref1->base, type2, widenFirst, widenSecond, indexer, env, openVars ); 97 if ( result ) result = new ReferenceType( ref1->get_qualifiers(), result ); 98 } else { 99 // formal is value, so result should be value 100 ReferenceType * ref2 = strict_dynamic_cast< ReferenceType * > ( type2 ); 101 result = handleReference( type1, ref2->base, widenFirst, widenSecond, indexer, env, openVars ); 108 // std::cerr << "formal is reference; result should be reference" << std::endl; 109 result = new ReferenceType( ref1->get_qualifiers(), result ); 102 110 } 103 111 // std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is [" << result << "]" << std::endl; … … 180 188 } 181 189 182 void CommonType::visit( __attribute((unused)) VoidType *voidType) {}190 void CommonType::visit( VoidType * ) {} 183 191 184 192 void CommonType::visit( BasicType *basicType ) { … … 246 254 } 247 255 248 void CommonType::visit( __attribute((unused)) ArrayType *arrayType) {}256 void CommonType::visit( ArrayType * ) {} 249 257 250 258 void CommonType::visit( ReferenceType *refType ) { … … 283 291 } 284 292 285 void CommonType::visit( __attribute((unused)) FunctionType *functionType) {}286 void CommonType::visit( __attribute((unused)) StructInstType *aggregateUseType) {}287 void CommonType::visit( __attribute((unused)) UnionInstType *aggregateUseType) {}293 void CommonType::visit( FunctionType * ) {} 294 void CommonType::visit( StructInstType * ) {} 295 void CommonType::visit( UnionInstType * ) {} 288 296 289 297 void CommonType::visit( EnumInstType *enumInstType ) { … … 296 304 } 297 305 298 void CommonType::visit( __attribute((unused)) TraitInstType *aggregateUseType) {306 void CommonType::visit( TraitInstType * ) { 299 307 } 300 308 … … 321 329 } 322 330 323 void CommonType::visit( __attribute((unused)) TupleType *tupleType) {}324 void CommonType::visit( __attribute((unused)) VarArgsType *varArgsType) {}331 void CommonType::visit( TupleType * ) {} 332 void CommonType::visit( VarArgsType * ) {} 325 333 326 334 void CommonType::visit( ZeroType *zeroType ) { -
src/ResolvExpr/ConversionCost.cc
r5c4f2c2 r5b51f5e 92 92 93 93 Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { 94 PRINT( std::cerr << "convert to reference cost... diff " << diff << std::endl; )94 PRINT( std::cerr << "convert to reference cost... diff " << diff << " " << src << " / " << dest << std::endl; ) 95 95 if ( diff > 0 ) { 96 96 // TODO: document this … … 108 108 if ( srcAsRef && destAsRef ) { // pointer-like conversions between references 109 109 PRINT( std::cerr << "converting between references" << std::endl; ) 110 if ( srcAsRef->get_base()->get_qualifiers() <= destAsRef->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( srcAsRef->get_base(), destAsRef->get_base(), indexer, env ) ) { 111 return Cost::safe; 110 Type::Qualifiers tq1 = srcAsRef->get_base()->get_qualifiers(); 111 Type::Qualifiers tq2 = destAsRef->get_base()->get_qualifiers(); 112 if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( srcAsRef->get_base(), destAsRef->get_base(), indexer, env ) ) { 113 PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) 114 if ( tq1 == tq2 ) { 115 // types are the same 116 return Cost::zero; 117 } else { 118 // types are the same, except otherPointer has more qualifiers 119 return Cost::safe; 120 } 112 121 } else { // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? 113 122 int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer ); … … 248 257 }; 249 258 250 void ConversionCost::visit( __attribute((unused)) VoidType *voidType) {259 void ConversionCost::visit( VoidType * ) { 251 260 cost = Cost::infinity; 252 261 } … … 271 280 void ConversionCost::visit( PointerType * pointerType ) { 272 281 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { 273 PRINT( std::cerr << pointerType << " ===> " << destAsPtr ; )282 PRINT( std::cerr << pointerType << " ===> " << destAsPtr << std::endl; ) 274 283 Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(); 275 284 Type::Qualifiers tq2 = destAsPtr->get_base()->get_qualifiers(); 276 285 if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { 286 PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) 277 287 if ( tq1 == tq2 ) { 278 288 // types are the same … … 280 290 } else { 281 291 // types are the same, except otherPointer has more qualifiers 282 PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; )283 292 cost = Cost::safe; 284 293 } 285 } else { // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?294 } else { 286 295 int assignResult = ptrsAssignable( pointerType->base, destAsPtr->base, env ); 287 296 PRINT( std::cerr << " :: " << assignResult << std::endl; ) 288 if ( assignResult > 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) { 289 cost = Cost::safe; 297 if ( assignResult > 0 && tq1 <= tq2 ) { 298 // xxx - want the case where qualifiers are added to be more expensive than the case where qualifiers are the same. Is 1 safe vs. 2 safe correct? 299 if ( tq1 == tq2 ) { 300 cost = Cost::safe; 301 } else if ( tq1 < tq2 ) { 302 cost = Cost::safe+Cost::safe; 303 } 290 304 } else if ( assignResult < 0 ) { 291 305 cost = Cost::unsafe; -
src/ResolvExpr/CurrentObject.cc
r5c4f2c2 r5b51f5e 38 38 39 39 namespace ResolvExpr { 40 long long int getConstValue( ConstantExpr * constExpr ) {41 if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) {42 if ( basicType->isInteger() ) {43 return constExpr->get_constant()->get_ival();44 } else {45 assertf( false, "Non-integer constant expression in getConstValue %s", toString( constExpr ).c_str() ); // xxx - might be semantic error46 }47 } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) {48 return 1;49 } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) {50 return 0;51 } else {52 assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error53 }54 }55 56 40 template< typename AggrInst > 57 41 TypeSubstitution makeGenericSubstitution( AggrInst * inst ) { … … 141 125 base = at->get_base(); 142 126 memberIter = createMemberIterator( base ); 127 if ( at->isVarLen ) throw SemanticError( "VLA initialization does not support @=", at ); 143 128 setSize( at->get_dimension() ); 144 129 } … … 151 136 void setSize( Expression * expr ) { 152 137 if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { 153 size = getConstValue( constExpr ); 154 PRINT( std::cerr << "array type with size: " << size << std::endl; ) 138 try { 139 size = constExpr->intValue(); 140 PRINT( std::cerr << "array type with size: " << size << std::endl; ) 141 } catch ( SemanticError & ) { 142 throw SemanticError( "Constant expression of non-integral type in array dimension: ", expr ); 143 } 155 144 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { 156 145 setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast 146 } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { 147 if ( EnumInstType * inst = dynamic_cast< EnumInstType * > ( varExpr->result ) ) { 148 long long int value; 149 if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { 150 size = value; 151 } 152 } 157 153 } else { 158 154 assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this … … 164 160 // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions 165 161 if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { 166 index = getConstValue( constExpr ); 162 try { 163 index = constExpr->intValue(); 164 } catch( SemanticError & ) { 165 throw SemanticError( "Constant expression of non-integral type in array designator: ", expr ); 166 } 167 167 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { 168 168 setPosition( castExpr->get_arg() ); 169 169 } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { 170 assertf( dynamic_cast<EnumInstType *> ( varExpr->get_result() ), "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() ); 171 index = 0; // xxx - get actual value of enum constant 170 EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() ); 171 assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() ); 172 long long int value; 173 if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { 174 index = value; 175 } 172 176 } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) { 173 177 index = 0; // xxx - get actual sizeof/alignof value? -
src/ResolvExpr/FindOpenVars.cc
r5c4f2c2 r5b51f5e 19 19 #include <map> // for map<>::mapped_type 20 20 21 #include "Common/PassVisitor.h" 21 22 #include "SynTree/Declaration.h" // for TypeDecl, DeclarationWithType (ptr ... 22 23 #include "SynTree/Type.h" // for Type, Type::ForallList, ArrayType 23 #include "SynTree/Visitor.h" // for Visitor24 24 25 25 namespace ResolvExpr { 26 class FindOpenVars : public Visitor { 27 public: 26 struct FindOpenVars : public WithGuards { 28 27 FindOpenVars( OpenVarSet &openVars, OpenVarSet &closedVars, AssertionSet &needAssertions, AssertionSet &haveAssertions, bool firstIsOpen ); 29 28 30 private: 31 virtual void visit(PointerType *pointerType); 32 virtual void visit(ArrayType *arrayType); 33 virtual void visit(FunctionType *functionType); 34 virtual void visit(TupleType *tupleType); 29 void previsit( PointerType * pointerType ); 30 void previsit( ArrayType * arrayType ); 31 void previsit( FunctionType * functionType ); 32 void previsit( TupleType * tupleType ); 35 33 36 34 void common_action( Type *type ); … … 42 40 43 41 void findOpenVars( Type *type, OpenVarSet &openVars, OpenVarSet &closedVars, AssertionSet &needAssertions, AssertionSet &haveAssertions, bool firstIsOpen ) { 44 FindOpenVarsfinder( openVars, closedVars, needAssertions, haveAssertions, firstIsOpen );42 PassVisitor<FindOpenVars> finder( openVars, closedVars, needAssertions, haveAssertions, firstIsOpen ); 45 43 type->accept( finder ); 46 44 } … … 70 68 } // for 71 69 } // if 72 /// std::c out<< "type is ";73 /// type->print( std::c out);74 /// std::c out<< std::endl << "need is" << std::endl;75 /// printAssertionSet( needAssertions, std::c out);76 /// std::c out<< std::endl << "have is" << std::endl;77 /// printAssertionSet( haveAssertions, std::c out);70 /// std::cerr << "type is "; 71 /// type->print( std::cerr ); 72 /// std::cerr << std::endl << "need is" << std::endl; 73 /// printAssertionSet( needAssertions, std::cerr ); 74 /// std::cerr << std::endl << "have is" << std::endl; 75 /// printAssertionSet( haveAssertions, std::cerr ); 78 76 } 79 77 80 void FindOpenVars:: visit(PointerType *pointerType) {78 void FindOpenVars::previsit(PointerType *pointerType) { 81 79 common_action( pointerType ); 82 Visitor::visit( pointerType );83 80 } 84 81 85 void FindOpenVars:: visit(ArrayType *arrayType) {82 void FindOpenVars::previsit(ArrayType *arrayType) { 86 83 common_action( arrayType ); 87 Visitor::visit( arrayType );88 84 } 89 85 90 void FindOpenVars:: visit(FunctionType *functionType) {86 void FindOpenVars::previsit(FunctionType *functionType) { 91 87 common_action( functionType ); 92 88 nextIsOpen = ! nextIsOpen; 93 Visitor::visit( functionType ); 94 nextIsOpen = ! nextIsOpen; 89 GuardAction( [this](){ nextIsOpen = ! nextIsOpen; } ); 95 90 } 96 91 97 void FindOpenVars:: visit(TupleType *tupleType) {92 void FindOpenVars::previsit(TupleType *tupleType) { 98 93 common_action( tupleType ); 99 Visitor::visit( tupleType );100 94 } 101 95 } // namespace ResolvExpr -
src/ResolvExpr/Occurs.cc
r5c4f2c2 r5b51f5e 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Occurs.cc -- 7 // Occurs.cc -- 8 8 // 9 9 // Author : Richard C. Bilson … … 17 17 #include <string> // for string 18 18 19 #include "Common/PassVisitor.h" 19 20 #include "SynTree/Type.h" // for TypeInstType, Type 20 #include "SynTree/Visitor.h" // for Visitor21 21 #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment 22 22 23 23 namespace ResolvExpr { 24 class Occurs : public Visitor { 25 public: 24 struct Occurs : public WithVisitorRef<Occurs> { 26 25 Occurs( std::string varName, const TypeEnvironment &env ); 27 bool get_result() const { return result; } 28 virtual void visit( TypeInstType *typeInst ); 29 private: 26 void previsit( TypeInstType * typeInst ); 27 30 28 bool result; 31 29 std::set< std::string > eqvVars; 32 const TypeEnvironment & env;30 const TypeEnvironment &tenv; 33 31 }; 34 32 35 33 bool occurs( Type *type, std::string varName, const TypeEnvironment &env ) { 36 Occursoccur( varName, env );34 PassVisitor<Occurs> occur( varName, env ); 37 35 type->accept( occur ); 38 return occur. get_result();36 return occur.pass.result; 39 37 } 40 38 41 Occurs::Occurs( std::string varName, const TypeEnvironment & env ) : result( false ),env( env ) {39 Occurs::Occurs( std::string varName, const TypeEnvironment & env ) : result( false ), tenv( env ) { 42 40 EqvClass eqvClass; 43 if ( env.lookup( varName, eqvClass ) ) {41 if ( tenv.lookup( varName, eqvClass ) ) { 44 42 eqvVars = eqvClass.vars; 45 43 } else { … … 48 46 } 49 47 50 void Occurs:: visit( TypeInstType *typeInst ) {48 void Occurs::previsit( TypeInstType * typeInst ) { 51 49 EqvClass eqvClass; 52 /// std::c out<< "searching for vars: ";53 /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::c out, " " ) );54 /// std::c out<< std::endl;50 /// std::cerr << "searching for vars: "; 51 /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cerr, " " ) ); 52 /// std::cerr << std::endl; 55 53 if ( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) { 56 54 result = true; 57 } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) {55 } else if ( tenv.lookup( typeInst->get_name(), eqvClass ) ) { 58 56 if ( eqvClass.type ) { 59 /// std::c out<< typeInst->get_name() << " is bound to";60 /// eqvClass.type->print( std::c out);61 /// std::c out<< std::endl;62 eqvClass.type->accept( * this);57 /// std::cerr << typeInst->get_name() << " is bound to"; 58 /// eqvClass.type->print( std::cerr ); 59 /// std::cerr << std::endl; 60 eqvClass.type->accept( *visitor ); 63 61 } // if 64 62 } // if -
src/ResolvExpr/PolyCost.cc
r5c4f2c2 r5b51f5e 14 14 // 15 15 16 #include "Common/PassVisitor.h" 16 17 #include "SymTab/Indexer.h" // for Indexer 17 18 #include "SynTree/Type.h" // for TypeInstType, Type 18 #include "SynTree/Visitor.h" // for Visitor19 19 #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment 20 20 21 21 namespace ResolvExpr { 22 class PolyCost : public Visitor { 23 public: 22 struct PolyCost { 24 23 PolyCost( const TypeEnvironment &env, const SymTab::Indexer &indexer ); 25 int get_result() const { return result; } 26 private: 27 virtual void visit(TypeInstType *aggregateUseType); 24 25 void previsit( TypeInstType * aggregateUseType ); 28 26 int result; 29 const TypeEnvironment & env;27 const TypeEnvironment &tenv; 30 28 const SymTab::Indexer &indexer; 31 29 }; 32 30 33 31 int polyCost( Type *type, const TypeEnvironment & env, const SymTab::Indexer &indexer ) { 34 P olyCostcoster( env, indexer );32 PassVisitor<PolyCost> coster( env, indexer ); 35 33 type->accept( coster ); 36 return coster. get_result();34 return coster.pass.result; 37 35 } 38 36 39 PolyCost::PolyCost( const TypeEnvironment & env, const SymTab::Indexer & indexer ) : result( 0 ), env( env ), indexer( indexer ) {37 PolyCost::PolyCost( const TypeEnvironment & env, const SymTab::Indexer & indexer ) : result( 0 ), tenv( env ), indexer( indexer ) { 40 38 } 41 39 42 void PolyCost:: visit(TypeInstType * typeInst) {40 void PolyCost::previsit(TypeInstType * typeInst) { 43 41 EqvClass eqvClass; 44 if ( env.lookup( typeInst->name, eqvClass ) ) {42 if ( tenv.lookup( typeInst->name, eqvClass ) ) { 45 43 if ( eqvClass.type ) { 46 44 if ( TypeInstType * otherTypeInst = dynamic_cast< TypeInstType* >( eqvClass.type ) ) { -
src/ResolvExpr/PtrsAssignable.cc
r5c4f2c2 r5b51f5e 67 67 PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} 68 68 69 void PtrsAssignable::visit( __attribute((unused)) VoidType *voidType) {69 void PtrsAssignable::visit( VoidType * ) { 70 70 // T * = void * is disallowed - this is a change from C, where any 71 71 // void * can be assigned or passed to a non-void pointer without a cast. -
src/ResolvExpr/Resolver.h
r5c4f2c2 r5b51f5e 33 33 void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ); 34 34 void findSingleExpression( Expression *& untyped, const SymTab::Indexer &indexer ); 35 void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer &indexer ); 35 36 void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ); 36 37 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer );
Note:
See TracChangeset
for help on using the changeset viewer.