Changes in / [81e1f32b:d2d50d7]
- Location:
- src
- Files:
-
- 2 added
- 1 deleted
- 25 edited
-
CodeGen/GenType.cc (modified) (13 diffs)
-
CodeTools/DeclStats.cc (modified) (19 diffs)
-
Common/PassVisitor.h (modified) (2 diffs)
-
Common/PassVisitor.impl.h (modified) (1 diff)
-
ControlStruct/ExceptTranslate.cc (modified) (1 diff)
-
ResolvExpr/AlternativeFinder.cc (modified) (41 diffs)
-
ResolvExpr/AlternativeFinder.h (modified) (2 diffs)
-
ResolvExpr/CommonType.cc (modified) (10 diffs)
-
ResolvExpr/CurrentObject.cc (modified) (1 diff)
-
ResolvExpr/PtrsAssignable.cc (modified) (6 diffs)
-
ResolvExpr/PtrsCastable.cc (modified) (7 diffs)
-
ResolvExpr/RenameVars.cc (modified) (2 diffs)
-
ResolvExpr/RenameVars.h (modified) (1 diff)
-
ResolvExpr/Resolver.cc (modified) (1 diff)
-
ResolvExpr/TypeMap.h (added)
-
ResolvExpr/Unify.cc (modified) (10 diffs)
-
SymTab/Mangler.cc (modified) (2 diffs)
-
SymTab/Mangler.h (modified) (1 diff)
-
SynTree/CompoundStmt.cc (modified) (1 diff)
-
SynTree/FunctionDecl.cc (modified) (1 diff)
-
SynTree/VarExprReplacer.cc (modified) (1 diff)
-
SynTree/VarExprReplacer.h (modified) (1 diff)
-
tests/.expect/alloc-ERROR.txt (modified) (3 diffs)
-
tests/.expect/alloc.txt (modified) (1 diff)
-
tests/.expect/libcfa_vector.txt (added)
-
tests/.expect/vector.txt (deleted)
-
tests/alloc.c (modified) (4 diffs)
-
tests/vector.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/GenType.cc
r81e1f32b rd2d50d7 26 26 27 27 namespace CodeGen { 28 struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting { 28 class GenType : public Visitor { 29 public: 29 30 GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false ); 30 31 std::string get_typeString() const { return typeString; } 31 32 void set_typeString( const std::string &newValue ) { typeString = newValue; } 32 33 33 void previsit( BaseSyntaxNode * ); 34 void postvisit( BaseSyntaxNode * ); 35 36 void postvisit( FunctionType * funcType ); 37 void postvisit( VoidType * voidType ); 38 void postvisit( BasicType * basicType ); 39 void postvisit( PointerType * pointerType ); 40 void postvisit( ArrayType * arrayType ); 41 void postvisit( ReferenceType * refType ); 42 void postvisit( StructInstType * structInst ); 43 void postvisit( UnionInstType * unionInst ); 44 void postvisit( EnumInstType * enumInst ); 45 void postvisit( TypeInstType * typeInst ); 46 void postvisit( TupleType * tupleType ); 47 void postvisit( VarArgsType * varArgsType ); 48 void postvisit( ZeroType * zeroType ); 49 void postvisit( OneType * oneType ); 34 virtual void visit( FunctionType *funcType ); 35 virtual void visit( VoidType *voidType ); 36 virtual void visit( BasicType *basicType ); 37 virtual void visit( PointerType *pointerType ); 38 virtual void visit( ArrayType *arrayType ); 39 virtual void visit( ReferenceType *refType ); 40 virtual void visit( StructInstType *structInst ); 41 virtual void visit( UnionInstType *unionInst ); 42 virtual void visit( EnumInstType *enumInst ); 43 virtual void visit( TypeInstType *typeInst ); 44 virtual void visit( TupleType * tupleType ); 45 virtual void visit( VarArgsType *varArgsType ); 46 virtual void visit( ZeroType *zeroType ); 47 virtual void visit( OneType *oneType ); 50 48 51 49 private: … … 61 59 62 60 std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) { 63 PassVisitor<GenType>gt( baseString, pretty, genC, lineMarks );61 GenType gt( baseString, pretty, genC, lineMarks ); 64 62 std::ostringstream os; 65 63 … … 70 68 71 69 type->accept( gt ); 72 return os.str() + gt. pass.get_typeString();70 return os.str() + gt.get_typeString(); 73 71 } 74 72 … … 79 77 GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} 80 78 81 // *** BaseSyntaxNode 82 void GenType::previsit( BaseSyntaxNode * ) { 83 // turn off automatic recursion for all nodes, to allow each visitor to 84 // precisely control the order in which its children are visited. 85 visit_children = false; 86 } 87 88 void GenType::postvisit( BaseSyntaxNode * node ) { 89 std::stringstream ss; 90 node->print( ss ); 91 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() ); 92 } 93 94 void GenType::postvisit( VoidType * voidType ) { 79 void GenType::visit( VoidType *voidType ) { 95 80 typeString = "void " + typeString; 96 81 handleQualifiers( voidType ); 97 82 } 98 83 99 void GenType:: postvisit( BasicType *basicType ) {100 BasicType::Kind kind = basicType-> kind;84 void GenType::visit( BasicType *basicType ) { 85 BasicType::Kind kind = basicType->get_kind(); 101 86 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); 102 87 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; … … 104 89 } 105 90 106 void GenType::genArray( const Type::Qualifiers & qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {91 void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { 107 92 std::ostringstream os; 108 93 if ( typeString != "" ) { … … 141 126 typeString = os.str(); 142 127 143 base->accept( * visitor);144 } 145 146 void GenType:: postvisit( PointerType *pointerType ) {147 assert( pointerType-> base!= 0);148 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType-> dimension) {149 genArray( pointerType->get_qualifiers(), pointerType-> base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );128 base->accept( *this ); 129 } 130 131 void GenType::visit( PointerType *pointerType ) { 132 assert( pointerType->get_base() != 0); 133 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { 134 genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); 150 135 } else { 151 136 handleQualifiers( pointerType ); … … 155 140 typeString = "*" + typeString; 156 141 } // if 157 pointerType-> base->accept( *visitor);158 } // if 159 } 160 161 void GenType:: postvisit( ArrayType *arrayType ) {162 genArray( arrayType->get_qualifiers(), arrayType-> base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );163 } 164 165 void GenType:: postvisit( ReferenceType *refType ) {166 assert( refType-> base!= 0);142 pointerType->get_base()->accept( *this ); 143 } // if 144 } 145 146 void GenType::visit( ArrayType *arrayType ) { 147 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); 148 } 149 150 void GenType::visit( ReferenceType *refType ) { 151 assert( refType->get_base() != 0); 167 152 assertf( ! genC, "Reference types should not reach code generation." ); 168 153 handleQualifiers( refType ); 169 154 typeString = "&" + typeString; 170 refType-> base->accept( *visitor);171 } 172 173 void GenType:: postvisit( FunctionType *funcType ) {155 refType->get_base()->accept( *this ); 156 } 157 158 void GenType::visit( FunctionType *funcType ) { 174 159 std::ostringstream os; 175 160 … … 184 169 /************* parameters ***************/ 185 170 186 const std::list<DeclarationWithType *> &pars = funcType-> parameters;171 const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); 187 172 188 173 if ( pars.empty() ) { … … 206 191 typeString = os.str(); 207 192 208 if ( funcType-> returnVals.size() == 0 ) {193 if ( funcType->get_returnVals().size() == 0 ) { 209 194 typeString = "void " + typeString; 210 195 } else { 211 funcType-> returnVals.front()->get_type()->accept( *visitor);196 funcType->get_returnVals().front()->get_type()->accept( *this ); 212 197 } // if 213 198 214 199 // add forall 215 if( ! funcType-> forall.empty() && ! genC ) {200 if( ! funcType->get_forall().empty() && ! genC ) { 216 201 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 217 202 std::ostringstream os; 218 203 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 219 204 os << "forall("; 220 cg.pass.genCommaList( funcType-> forall.begin(), funcType->forall.end() );205 cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() ); 221 206 os << ")" << std::endl; 222 207 typeString = os.str() + typeString; … … 236 221 } 237 222 238 void GenType:: postvisit( StructInstType *structInst ) {239 typeString = structInst-> name+ handleGeneric( structInst ) + " " + typeString;223 void GenType::visit( StructInstType *structInst ) { 224 typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString; 240 225 if ( genC ) typeString = "struct " + typeString; 241 226 handleQualifiers( structInst ); 242 227 } 243 228 244 void GenType:: postvisit( UnionInstType *unionInst ) {245 typeString = unionInst-> name+ handleGeneric( unionInst ) + " " + typeString;229 void GenType::visit( UnionInstType *unionInst ) { 230 typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString; 246 231 if ( genC ) typeString = "union " + typeString; 247 232 handleQualifiers( unionInst ); 248 233 } 249 234 250 void GenType:: postvisit( EnumInstType *enumInst ) {251 typeString = enumInst-> name+ " " + typeString;235 void GenType::visit( EnumInstType *enumInst ) { 236 typeString = enumInst->get_name() + " " + typeString; 252 237 if ( genC ) typeString = "enum " + typeString; 253 238 handleQualifiers( enumInst ); 254 239 } 255 240 256 void GenType:: postvisit( TypeInstType *typeInst ) {257 typeString = typeInst-> name+ " " + typeString;241 void GenType::visit( TypeInstType *typeInst ) { 242 typeString = typeInst->get_name() + " " + typeString; 258 243 handleQualifiers( typeInst ); 259 244 } 260 245 261 void GenType:: postvisit( TupleType * tupleType ) {246 void GenType::visit( TupleType * tupleType ) { 262 247 assertf( ! genC, "Tuple types should not reach code generation." ); 263 248 unsigned int i = 0; … … 272 257 } 273 258 274 void GenType:: postvisit( VarArgsType *varArgsType ) {259 void GenType::visit( VarArgsType *varArgsType ) { 275 260 typeString = "__builtin_va_list " + typeString; 276 261 handleQualifiers( varArgsType ); 277 262 } 278 263 279 void GenType:: postvisit( ZeroType *zeroType ) {264 void GenType::visit( ZeroType *zeroType ) { 280 265 // ideally these wouldn't hit codegen at all, but should be safe to make them ints 281 266 typeString = (pretty ? "zero_t " : "long int ") + typeString; … … 283 268 } 284 269 285 void GenType:: postvisit( OneType *oneType ) {270 void GenType::visit( OneType *oneType ) { 286 271 // ideally these wouldn't hit codegen at all, but should be safe to make them ints 287 272 typeString = (pretty ? "one_t " : "long int ") + typeString; … … 289 274 } 290 275 291 void GenType::handleQualifiers( Type * type ) {276 void GenType::handleQualifiers( Type *type ) { 292 277 if ( type->get_const() ) { 293 278 typeString = "const " + typeString; -
src/CodeTools/DeclStats.cc
r81e1f32b rd2d50d7 23 23 #include <utility> // for pair, make_pair 24 24 25 #include "Common/PassVisitor.h"26 25 #include "Common/SemanticError.h" // for SemanticError 27 26 #include "Common/VectorMap.h" // for VectorMap … … 36 35 namespace CodeTools { 37 36 38 struct DeclStats : public WithShortCircuiting{37 class DeclStats : public Visitor { 39 38 template<typename T> 40 39 static void sum(T& a, const T& b) { a += b; } … … 62 61 63 62 struct ArgPackStats { 64 VectorMap<unsigned> n; ///< Count of decls with each number of elements 65 VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements 66 VectorMap<unsigned> n_generic; ///< Count of decls with each number of generic type elements 67 VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements 68 VectorMap<unsigned> n_compound; ///< Count of decls with each number of non-generic compound types 69 std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements 70 std::map<unsigned, unsigned> p_generic; ///< Count of decls with each percentage of generic type elements 71 std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements 72 std::map<unsigned, unsigned> p_compound; ///< Count of decls with each percentage of non-generic compound type elements 73 VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack 63 VectorMap<unsigned> n; ///< Count of decls with each number of elements 64 VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements 65 VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements 66 std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements 67 std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements 68 VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack 74 69 /// Count of decls with each percentage of new types in lists. 75 70 /// Types used in the parameter list that recur in the return list are not considered to be new. … … 79 74 sum(n, o.n); 80 75 sum(n_basic, o.n_basic); 81 sum(n_generic, o.n_generic);82 76 sum(n_poly, o.n_poly); 83 sum(n_compound, o.n_compound);84 77 sum(p_basic, o.p_basic); 85 sum(p_generic, o.p_generic);86 78 sum(p_poly, o.p_poly); 87 sum(p_compound, o.p_compound);88 79 sum(n_types, o.n_types); 89 80 sum(p_new, o.p_new); … … 97 88 /// Count of declarations with each number of assertion parameters 98 89 VectorMap<unsigned> n_type_params; 99 /// Count of generic types with each number of type parameters100 VectorMap<unsigned> n_generic_params;101 /// Count of maximum nesting depth of types102 VectorMap<unsigned> n_generic_nesting;103 90 /// Count of declarations with each name 104 91 std::unordered_map<std::string, unsigned> by_name; 105 92 /// Count of uses of each basic type 106 93 std::unordered_map<std::string, unsigned> basic_type_names; 107 /// Count of uses of each generic type name (includes "*", "[]", "(*)", "[,]") 108 std::unordered_map<std::string, unsigned> generic_type_names; 109 /// Count of uses of each non-generic aggregate type 94 /// Count of uses of each non-basic type 110 95 std::unordered_map<std::string, unsigned> compound_type_names; 111 96 /// Count of decls using each basic type 112 97 std::unordered_map<std::string, unsigned> basic_type_decls; 113 /// Count of decls using each generic type (includes "*", "[]", "(*)", "[,]")114 std::unordered_map<std::string, unsigned> generic_type_decls;115 98 /// Count of decls using each compound type 116 99 std::unordered_map<std::string, unsigned> compound_type_decls; … … 127 110 ArgPackStats assn_returns; 128 111 129 Stats() : n_decls(0), n_type_params(), n_generic_params(), n_generic_nesting(), 130 by_name(), basic_type_names(), generic_type_names(), compound_type_names(), 131 basic_type_decls(), generic_type_decls(), compound_type_decls(), params(), 132 returns(), n_assns(), assn_params(), assn_returns() {} 112 Stats() : n_decls(0), n_type_params(), by_name(), basic_type_names(), compound_type_names(), basic_type_decls(), compound_type_decls(), params(), returns(), n_assns(), assn_params(), assn_returns() {} 133 113 134 114 public: … … 136 116 sum( n_decls, o.n_decls ); 137 117 sum( n_type_params, o.n_type_params ); 138 sum( n_generic_params, o.n_generic_params );139 sum( n_generic_nesting, o.n_generic_nesting );140 118 sum( by_name, o.by_name ); 141 119 sum( basic_type_names, o.basic_type_names ); 142 sum( generic_type_names, o.generic_type_names );143 120 sum( compound_type_names, o.compound_type_names ); 144 121 sum( basic_type_decls, o.basic_type_decls ); 145 sum( generic_type_decls, o.generic_type_decls );146 122 sum( compound_type_decls, o.compound_type_decls ); 147 123 sum( params, o.params ); … … 155 131 }; 156 132 157 /// number of counting bins for linkages 158 static const unsigned n_named_specs = 8; 159 /// map from total number of specs to bins 160 static const unsigned ind_for_linkage[16]; 161 162 Stats for_linkage[n_named_specs]; ///< Stores separate stats per linkage 133 Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage 163 134 std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting 164 135 Stats total; … … 166 137 std::map<std::pair<unsigned, unsigned>, unsigned> exprs_by_fanout_at_depth; 167 138 168 void countType( const std::string& name, unsigned& n, std::unordered_map<std::string,169 unsigned>& names, std::unordered_map<std::string, unsigned>& decls,170 std::unordered_set<std::string>& elSeen ) {171 ++n;172 ++names[ name ];173 if ( elSeen.insert( name ).second ) { ++decls[ name ]; }174 }175 176 void update_max( unsigned& max, unsigned crnt ) {177 if ( crnt > max ) max = crnt;178 }179 180 void analyzeSubtype( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen,181 unsigned& n_poly, bool& seen_poly, unsigned& max_depth, unsigned depth ) {182 unsigned x;183 analyzeType( ty, stats, elSeen, x, x, n_poly, x, seen_poly, max_depth, depth + 1 );184 }185 186 void analyzeSubtypes( std::list<DeclarationWithType*>& tys, Stats& stats,187 std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly,188 unsigned& max_depth, unsigned depth, unsigned& n_subs ) {189 for ( DeclarationWithType* dwt : tys ) {190 Type* ty = dwt->get_type();191 n_subs += (unsigned)( dynamic_cast<VoidType*>(ty) != nullptr );192 analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth );193 }194 }195 196 void analyzeSubtypes( std::list<Expression*>& tys, Stats& stats,197 std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly,198 unsigned& max_depth, unsigned depth ) {199 for ( Expression* expr : tys ) {200 TypeExpr* texpr = dynamic_cast<TypeExpr*>(expr);201 if ( ! texpr ) continue;202 Type* ty = texpr->get_type();203 analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth );204 }205 }206 207 void analyzeSubtypes( std::list<Type*>& tys, Stats& stats,208 std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly,209 unsigned& max_depth, unsigned depth ) {210 for ( Type* ty : tys ) {211 analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth );212 }213 }214 215 void analyzeType( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen,216 unsigned& n_basic, unsigned& n_generic, unsigned& n_poly, unsigned& n_agg,217 bool& seen_poly, unsigned& max_depth, unsigned depth = 0 ) {218 if ( BasicType* bt = dynamic_cast<BasicType*>(ty) ) {219 std::string name = BasicType::typeNames[ bt->get_kind() ];220 countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen );221 update_max( max_depth, depth );222 } else if ( PointerType* pt = dynamic_cast<PointerType*>(ty) ) {223 std::string name = "*";224 countType(225 name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen);226 analyzeSubtype(227 pt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth );228 ++stats.n_generic_params.at( 1 );229 } else if ( ArrayType* at = dynamic_cast<ArrayType*>(ty) ) {230 std::string name = "[]";231 countType(232 name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen);233 analyzeSubtype(234 at->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth );235 ++stats.n_generic_params.at( 1 );236 } else if ( ReferenceType* rt = dynamic_cast<ReferenceType*>(ty) ) {237 std::string name = "&";238 countType(239 name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen);240 analyzeSubtype(241 rt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth );242 ++stats.n_generic_params.at( 1 );243 } else if ( FunctionType* ft = dynamic_cast<FunctionType*>(ty) ) {244 std::string name = "(*)";245 countType(246 name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen);247 unsigned n_subs = 0;248 analyzeSubtypes(249 ft->get_returnVals(), stats, elSeen, n_poly, seen_poly, max_depth, depth,250 n_subs );251 analyzeSubtypes(252 ft->get_parameters(), stats, elSeen, n_poly, seen_poly, max_depth, depth,253 n_subs );254 ++stats.n_generic_params.at( n_subs );255 } else if ( TypeInstType* vt = dynamic_cast<TypeInstType*>(ty) ) {256 if ( ! seen_poly ) {257 ++n_poly;258 seen_poly = true;259 }260 countType(261 vt->get_name(), n_agg, stats.compound_type_names, stats.compound_type_decls,262 elSeen );263 update_max( max_depth, depth );264 } else if ( ReferenceToType* st = dynamic_cast<ReferenceToType*>(ty) ) {265 std::list<Expression*>& params = st->get_parameters();266 if ( params.empty() ) {267 countType(268 st->get_name(), n_agg, stats.compound_type_names,269 stats.compound_type_decls, elSeen );270 update_max( max_depth, depth );271 } else {272 countType(273 st->get_name(), n_generic, stats.generic_type_names,274 stats.generic_type_decls, elSeen);275 analyzeSubtypes( params, stats, elSeen, n_poly, seen_poly, max_depth, depth );276 ++stats.n_generic_params.at( params.size() );277 }278 } else if ( TupleType* tt = dynamic_cast<TupleType*>(ty) ) {279 std::string name = "[,]";280 countType(281 name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen);282 analyzeSubtypes(283 tt->get_types(), stats, elSeen, n_poly, seen_poly, max_depth, depth );284 ++stats.n_generic_params.at( tt->size() );285 } else if ( dynamic_cast<VarArgsType*>(ty) ) {286 std::string name = "...";287 countType(288 name, n_agg, stats.compound_type_names, stats.compound_type_decls, elSeen );289 update_max( max_depth, depth );290 } else if ( dynamic_cast<ZeroType*>(ty) ) {291 std::string name = "0";292 countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen );293 update_max( max_depth, depth );294 } else if ( dynamic_cast<OneType*>(ty) ) {295 std::string name = "1";296 countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen );297 update_max( max_depth, depth );298 }299 }300 301 139 /// Update arg pack stats based on a declaration list 302 void analyze( Stats& stats, std::unordered_set<std::string>& seen, 303 std::unordered_set<std::string>& elSeen, ArgPackStats& pstats, 304 std::list<DeclarationWithType*>& decls ) { 140 void analyze( Stats& stats, std::unordered_set<std::string>& seen, ArgPackStats& pstats, std::list<DeclarationWithType*>& decls ) { 305 141 std::unordered_set<std::string> types; 306 unsigned n = 0; ///< number of args/returns 307 unsigned n_basic = 0; ///< number of basic types 308 unsigned n_generic = 0; ///< number of generic types (includes "*", "&", "[]", "(*)", "[,]") 309 unsigned n_poly = 0; ///< number of polymorphic types 310 unsigned n_agg = 0; ///< number of non-generic aggregate types 311 unsigned n_new = 0; ///< number of new types 312 142 unsigned n = 0; ///< number of args/returns 143 unsigned n_basic = 0; ///< number of basic types 144 unsigned n_poly = 0; ///< number of polymorphic types 145 unsigned n_new = 0; ///< number of new types 313 146 for ( auto decl : decls ) { 314 147 Type* dt = decl->get_type(); … … 319 152 dt->print( ss ); 320 153 types.insert( ss.str() ); 321 if ( seen.insert( ss.str() ).second ) { ++n_new; } 322 323 bool seen_poly = false; 324 unsigned max_depth = 0; 325 analyzeType( 326 dt, stats, elSeen, n_basic, n_generic, n_poly, n_agg, seen_poly, max_depth ); 327 ++stats.n_generic_nesting.at( max_depth ); 328 } 329 154 bool this_new = seen.insert( ss.str() ).second; 155 if ( this_new ) { ++n_new; } 156 157 if ( dynamic_cast<BasicType*>( dt ) ) { 158 ++n_basic; 159 ++stats.basic_type_names[ ss.str() ]; 160 if ( this_new ) { 161 ++stats.basic_type_decls[ ss.str() ]; 162 } 163 } else if ( GenPoly::hasPolyBase( dt ) ) { 164 ++n_poly; 165 } else { 166 ++stats.compound_type_names[ ss.str() ]; 167 if ( this_new ) { 168 ++stats.compound_type_decls[ ss.str() ]; 169 } 170 } 171 } 330 172 ++pstats.n.at( n ); 331 173 ++pstats.n_basic.at( n_basic ); 332 ++pstats.n_generic.at( n_generic );333 174 ++pstats.n_poly.at( n_poly ); 334 ++pstats.n_compound.at( n_agg );335 175 if ( n > 0 ) { 336 176 ++pstats.p_basic[ n_basic*100/n ]; 337 ++pstats.p_generic[ n_generic*100/n ];338 177 ++pstats.p_poly[ n_poly*100/n ]; 339 ++pstats.p_compound[ n_agg*100/n ]; 340 if ( n > 1 ) ++pstats.p_new[ (n_new-1)*100/(n-1) ]; 178 ++pstats.p_new[ n_new*100/n ]; 341 179 } 342 180 ++pstats.n_types.at( types.size() ); … … 345 183 void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) { 346 184 std::unordered_set<std::string> seen; 347 std::unordered_set<std::string> elSeen; 348 analyze( stats, seen, elSeen, params, fnTy->get_parameters() ); 349 analyze( stats, seen, elSeen, returns, fnTy->get_returnVals() ); 185 analyze( stats, seen, params, fnTy->get_parameters() ); 186 analyze( stats, seen, returns, fnTy->get_returnVals() ); 350 187 } 351 188 … … 363 200 364 201 public: 365 void previsit( FunctionDecl *decl ) { 202 using Visitor::visit; 203 204 virtual void visit( FunctionDecl *decl ) { 366 205 // skip if already seen declaration for this function 367 const std::string& mangleName = decl->get_mangleName().empty() ? decl->name : decl->get_mangleName(); 368 if ( seen_names.insert( mangleName ).second ) { 369 Stats& stats = for_linkage[ ind_for_linkage[ decl->linkage ] ]; 370 371 ++stats.n_decls; 372 FunctionType* fnTy = decl->type; 373 const Type::ForallList& forall = fnTy->forall; 374 ++stats.n_type_params.at( forall.size() ); 375 unsigned n_assns = 0; 376 for ( TypeDecl* fdecl : forall ) { 377 n_assns += fdecl->assertions.size(); 378 for ( DeclarationWithType* assn : fdecl->assertions ) { 379 FunctionType *assnTy = nullptr; 380 if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { 381 if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { 382 assnTy = dynamic_cast<FunctionType*>(ptrTy->base); 383 } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); 384 } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { 385 assnTy = assnDecl->type; 386 } 387 if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); 206 const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName(); 207 if ( ! seen_names.insert( mangleName ).second ) { 208 maybeAccept( decl->get_statements(), *this ); 209 return; 210 } 211 212 Stats& stats = for_linkage[ decl->get_linkage() ]; 213 214 ++stats.n_decls; 215 FunctionType* fnTy = decl->get_functionType(); 216 const Type::ForallList& forall = fnTy->get_forall(); 217 ++stats.n_type_params.at( forall.size() ); 218 unsigned n_assns = 0; 219 for ( TypeDecl* fdecl : forall ) { 220 n_assns += fdecl->get_assertions().size(); 221 for ( DeclarationWithType* assn : fdecl->get_assertions() ) { 222 FunctionType *assnTy = 0; 223 if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { 224 if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { 225 assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base()); 226 } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); 227 } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { 228 assnTy = assnDecl->get_functionType(); 388 229 } 389 } 390 ++stats.n_assns[ n_assns ]; 391 ++stats.by_name[ decl->name ]; 392 analyzeFunc( fnTy, stats, stats.params, stats.returns ); 393 } 394 } 395 396 void previsit( UntypedExpr *expr ) { 397 visit_children = false; 230 if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); 231 } 232 } 233 ++stats.n_assns[ n_assns ]; 234 235 ++stats.by_name[ decl->get_name() ]; 236 237 analyzeFunc( fnTy, stats, stats.params, stats.returns ); 238 239 // analyze expressions in decl statements 240 maybeAccept( decl->get_statements(), *this ); 241 } 242 243 virtual void visit( UntypedExpr *expr ) { 398 244 analyzeExpr( expr, 0 ); 399 245 } … … 426 272 template<typename F> 427 273 void printAllHisto( const std::string& name, F extract ) { 428 VectorMap<unsigned> histos[ n_named_specs];274 VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs]; 429 275 VectorMap<unsigned> thisto; 430 276 431 277 for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } 432 278 433 for ( unsigned i = 0; i < n_named_specs; ++i ) {279 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { 434 280 // can't be a higher count in one of the sub-histograms than the total 435 281 histos[i].reserve( thisto.size() ); … … 449 295 template<typename F> 450 296 void printAllSparseHisto( const std::string& name, F extract ) { 451 std::map<unsigned, unsigned> histos[ n_named_specs];297 std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs]; 452 298 std::map<unsigned, unsigned> thisto; 453 299 454 300 for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } 455 301 456 for ( unsigned i = 0; i < n_named_specs; ++i ) {302 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { 457 303 for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } 458 304 } … … 461 307 const auto& key = entry.first; 462 308 std::cout << "\"" << name << "\"," << key; 463 for ( unsigned i = 0; i < n_named_specs; ++i ) {309 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { 464 310 auto it = histos[i].find( key ); 465 311 if ( it == histos[i].end() ) std::cout << ",0"; … … 473 319 void printAllPack( const std::string& name, F extract ) { 474 320 printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); 475 printAllMap("n_generic_" + name, [&extract](const Stats& stats) { return extract(stats).n_generic; });476 321 printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); 477 printAllMap("n_compound_" + name, [&extract](const Stats& stats) { return extract(stats).n_compound; });478 322 printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); 479 323 printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); 480 printAllMap("%_generic_" + name, [&extract](const Stats& stats) { return extract(stats).p_generic; });481 324 printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); 482 printAllMap("%_compound_" + name, [&extract](const Stats& stats) { return extract(stats).p_compound; });483 325 printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; }); 484 326 printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; }); … … 500 342 } 501 343 502 std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\" compiler\",\"builtinCFA\",\"builtinC\",\"other\",\"TOTAL\"" << std::endl;344 std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl; 503 345 504 346 printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); 505 printAllMap("n_generic_params", [](const Stats& stats) { return stats.n_generic_params; });506 printAllMap("n_generic_nesting", [](const Stats& stats) { return stats.n_generic_nesting; });507 347 printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); 508 348 printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); … … 511 351 printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; }); 512 352 printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; }); 513 printAll("generic_type_names", [](const Stats& stats) { return stats.generic_type_names.size(); });514 printAllSparseHisto("generic_type_uses", [](const Stats& stats) { return stats.generic_type_names; });515 printAllSparseHisto("decls_using_generic_type", [](const Stats& stats) { return stats.generic_type_decls; });516 353 printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); }); 517 354 printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; }); … … 528 365 }; 529 366 530 const unsigned DeclStats::ind_for_linkage[]531 = { 7, 7, 2, 1, 7, 7, 7, 3, 4, 7, 6, 5, 7, 7, 7, 0 };532 533 367 void printDeclStats( std::list< Declaration * > &translationUnit ) { 534 PassVisitor<DeclStats>stats;368 DeclStats stats; 535 369 acceptAll( translationUnit, stats ); 536 stats.p ass.print();370 stats.print(); 537 371 } 538 372 -
src/Common/PassVisitor.h
r81e1f32b rd2d50d7 119 119 virtual void visit( StmtExpr * stmtExpr ) override final; 120 120 virtual void visit( UniqueExpr * uniqueExpr ) override final; 121 virtual void visit( UntypedInitExpr * initExpr ) override final;122 virtual void visit( InitExpr * initExpr ) override final;123 121 124 122 virtual void visit( VoidType * basicType ) override final; … … 213 211 virtual Expression * mutate( StmtExpr * stmtExpr ) override final; 214 212 virtual Expression * mutate( UniqueExpr * uniqueExpr ) override final; 215 virtual Expression * mutate( UntypedInitExpr * initExpr ) override final;216 virtual Expression * mutate( InitExpr * initExpr ) override final;217 213 218 214 virtual Type * mutate( VoidType * basicType ) override final; -
src/Common/PassVisitor.impl.h
r81e1f32b rd2d50d7 1853 1853 } 1854 1854 1855 //--------------------------------------------------------------------------1856 // UntypedInitExpr1857 template< typename pass_type >1858 void PassVisitor< pass_type >::visit( UntypedInitExpr * node ) {1859 VISIT_START( node );1860 1861 indexerScopedAccept( node->result, *this );1862 maybeAccept_impl ( node->expr , *this );1863 // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.1864 1865 VISIT_END( node );1866 }1867 1868 template< typename pass_type >1869 Expression * PassVisitor< pass_type >::mutate( UntypedInitExpr * node ) {1870 MUTATE_START( node );1871 1872 indexerScopedMutate( node->env , *this );1873 indexerScopedMutate( node->result, *this );1874 maybeMutate_impl ( node->expr , *this );1875 // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.1876 1877 MUTATE_END( Expression, node );1878 }1879 1880 //--------------------------------------------------------------------------1881 // InitExpr1882 template< typename pass_type >1883 void PassVisitor< pass_type >::visit( InitExpr * node ) {1884 VISIT_START( node );1885 1886 indexerScopedAccept( node->result, *this );1887 maybeAccept_impl ( node->expr , *this );1888 maybeAccept_impl ( node->designation, *this );1889 1890 VISIT_END( node );1891 }1892 1893 template< typename pass_type >1894 Expression * PassVisitor< pass_type >::mutate( InitExpr * node ) {1895 MUTATE_START( node );1896 1897 indexerScopedMutate( node->env , *this );1898 indexerScopedMutate( node->result, *this );1899 maybeMutate_impl ( node->expr , *this );1900 maybeMutate_impl ( node->designation, *this );1901 1902 MUTATE_END( Expression, node );1903 }1904 1905 1855 template< typename pass_type > 1906 1856 void PassVisitor< pass_type >::visit( VoidType * node ) { -
src/ControlStruct/ExceptTranslate.cc
r81e1f32b rd2d50d7 316 316 VarExprReplacer::DeclMap mapping; 317 317 mapping[ handler_decl ] = local_except; 318 VarExprReplacer::replace( handler->body, mapping ); 318 VarExprReplacer mapper( mapping ); 319 handler->get_body()->accept( mapper ); 319 320 } 320 321 -
src/ResolvExpr/AlternativeFinder.cc
r81e1f32b rd2d50d7 60 60 61 61 namespace ResolvExpr { 62 struct AlternativeFinder::Finder : public WithShortCircuiting {63 Finder( AlternativeFinder & altFinder ) : altFinder( altFinder ), indexer( altFinder.indexer ), alternatives( altFinder.alternatives ), env( altFinder.env ), targetType( altFinder.targetType ) {}64 65 void previsit( BaseSyntaxNode * ) { visit_children = false; }66 67 void postvisit( ApplicationExpr * applicationExpr );68 void postvisit( UntypedExpr * untypedExpr );69 void postvisit( AddressExpr * addressExpr );70 void postvisit( LabelAddressExpr * labelExpr );71 void postvisit( CastExpr * castExpr );72 void postvisit( VirtualCastExpr * castExpr );73 void postvisit( UntypedMemberExpr * memberExpr );74 void postvisit( MemberExpr * memberExpr );75 void postvisit( NameExpr * variableExpr );76 void postvisit( VariableExpr * variableExpr );77 void postvisit( ConstantExpr * constantExpr );78 void postvisit( SizeofExpr * sizeofExpr );79 void postvisit( AlignofExpr * alignofExpr );80 void postvisit( UntypedOffsetofExpr * offsetofExpr );81 void postvisit( OffsetofExpr * offsetofExpr );82 void postvisit( OffsetPackExpr * offsetPackExpr );83 void postvisit( AttrExpr * attrExpr );84 void postvisit( LogicalExpr * logicalExpr );85 void postvisit( ConditionalExpr * conditionalExpr );86 void postvisit( CommaExpr * commaExpr );87 void postvisit( ImplicitCopyCtorExpr * impCpCtorExpr );88 void postvisit( ConstructorExpr * ctorExpr );89 void postvisit( RangeExpr * rangeExpr );90 void postvisit( UntypedTupleExpr * tupleExpr );91 void postvisit( TupleExpr * tupleExpr );92 void postvisit( TupleIndexExpr * tupleExpr );93 void postvisit( TupleAssignExpr * tupleExpr );94 void postvisit( UniqueExpr * unqExpr );95 void postvisit( StmtExpr * stmtExpr );96 void postvisit( UntypedInitExpr * initExpr );97 98 /// Adds alternatives for anonymous members99 void addAnonConversions( const Alternative & alt );100 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member101 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );102 /// Adds alternatives for member expressions where the left side has tuple type103 void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );104 /// Adds alternatives for offsetof expressions, given the base type and name of the member105 template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );106 /// Takes a final result and checks if its assertions can be satisfied107 template<typename OutputIterator>108 void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );109 /// Finds matching alternatives for a function, given a set of arguments110 template<typename OutputIterator>111 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out );112 /// Checks if assertion parameters match for a new alternative113 template< typename OutputIterator >114 void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );115 private:116 AlternativeFinder & altFinder;117 const SymTab::Indexer &indexer;118 AltList & alternatives;119 const TypeEnvironment &env;120 Type *& targetType;121 };122 123 62 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) { 124 63 CastExpr *castToVoid = new CastExpr( expr ); … … 213 152 214 153 void renameTypes( Expression *expr ) { 215 renameTyVars( expr->result);154 expr->get_result()->accept( global_renamer ); 216 155 } 217 156 } // namespace … … 246 185 247 186 void AlternativeFinder::find( Expression *expr, bool adjust, bool prune, bool failFast ) { 248 PassVisitor<Finder> finder( *this ); 249 expr->accept( finder ); 187 expr->accept( *this ); 250 188 if ( failFast && alternatives.empty() ) { 251 189 PRINT( … … 306 244 } 307 245 308 void AlternativeFinder:: Finder::addAnonConversions( const Alternative & alt ) {246 void AlternativeFinder::addAnonConversions( const Alternative & alt ) { 309 247 // adds anonymous member interpretations whenever an aggregate value type is seen. 310 248 // it's okay for the aggregate expression to have reference type -- cast it to the base type to treat the aggregate as the referenced value … … 327 265 328 266 template< typename StructOrUnionType > 329 void AlternativeFinder:: Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {267 void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { 330 268 // by this point, member must be a name expr 331 269 NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ); … … 346 284 } 347 285 348 void AlternativeFinder:: Finder::addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {286 void AlternativeFinder::addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { 349 287 if ( ConstantExpr * constantExpr = dynamic_cast< ConstantExpr * >( member ) ) { 350 288 // get the value of the constant expression as an int, must be between 0 and the length of the tuple type to have meaning … … 370 308 } 371 309 372 void AlternativeFinder:: Finder::postvisit( ApplicationExpr *applicationExpr ) {310 void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) { 373 311 alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) ); 374 312 } … … 547 485 Type *adjType = candidate->get_type()->clone(); 548 486 adjustExprType( adjType, newEnv, indexer ); 549 renameTyVars( adjType);487 adjType->accept( global_renamer ); 550 488 PRINT( 551 489 std::cerr << "unifying "; … … 603 541 604 542 template< typename OutputIterator > 605 void AlternativeFinder:: Finder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {543 void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) { 606 544 // PRINT( 607 545 // std::cerr << "inferParameters: assertions needed are" << std::endl; … … 657 595 658 596 ArgPack() 659 : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0), 597 : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0), 660 598 tupleStart(0), nextExpl(0), explAlt(0) {} 661 599 … … 768 706 769 707 if ( nTuples > 0 || ! results[i].expr ) { 770 // first iteration or no expression to clone, 708 // first iteration or no expression to clone, 771 709 // push empty tuple expression 772 710 newResult.parent = i; … … 954 892 955 893 template<typename OutputIterator> 956 void AlternativeFinder:: Finder::validateFunctionAlternative( const Alternative &func, ArgPack& result,894 void AlternativeFinder::validateFunctionAlternative( const Alternative &func, ArgPack& result, 957 895 const std::vector<ArgPack>& results, OutputIterator out ) { 958 896 ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() ); … … 977 915 978 916 template<typename OutputIterator> 979 void AlternativeFinder:: Finder::makeFunctionAlternatives( const Alternative &func,917 void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, 980 918 FunctionType *funcType, const ExplodedArgs &args, OutputIterator out ) { 981 919 OpenVarSet funcOpenVars; … … 1084 1022 } 1085 1023 1086 void AlternativeFinder:: Finder::postvisit( UntypedExpr *untypedExpr ) {1024 void AlternativeFinder::visit( UntypedExpr *untypedExpr ) { 1087 1025 AlternativeFinder funcFinder( indexer, env ); 1088 1026 funcFinder.findWithAdjustment( untypedExpr->get_function() ); … … 1091 1029 1092 1030 std::vector< AlternativeFinder > argAlternatives; 1093 altFinder.findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(),1031 findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), 1094 1032 back_inserter( argAlternatives ) ); 1095 1033 1096 1034 // take care of possible tuple assignments 1097 1035 // if not tuple assignment, assignment is taken care of as a normal function call 1098 Tuples::handleTupleAssignment( altFinder, untypedExpr, argAlternatives );1036 Tuples::handleTupleAssignment( *this, untypedExpr, argAlternatives ); 1099 1037 1100 1038 // find function operators … … 1234 1172 // fix this issue in a more robust way. 1235 1173 targetType = nullptr; 1236 postvisit( untypedExpr );1174 visit( untypedExpr ); 1237 1175 } 1238 1176 } … … 1243 1181 } 1244 1182 1245 void AlternativeFinder:: Finder::postvisit( AddressExpr *addressExpr ) {1183 void AlternativeFinder::visit( AddressExpr *addressExpr ) { 1246 1184 AlternativeFinder finder( indexer, env ); 1247 1185 finder.find( addressExpr->get_arg() ); … … 1254 1192 } 1255 1193 1256 void AlternativeFinder:: Finder::postvisit( LabelAddressExpr * expr ) {1194 void AlternativeFinder::visit( LabelAddressExpr * expr ) { 1257 1195 alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } ); 1258 1196 } … … 1285 1223 } 1286 1224 1287 void AlternativeFinder:: Finder::postvisit( CastExpr *castExpr ) {1225 void AlternativeFinder::visit( CastExpr *castExpr ) { 1288 1226 Type *& toType = castExpr->get_result(); 1289 1227 assert( toType ); … … 1340 1278 } 1341 1279 1342 void AlternativeFinder:: Finder::postvisit( VirtualCastExpr * castExpr ) {1280 void AlternativeFinder::visit( VirtualCastExpr * castExpr ) { 1343 1281 assertf( castExpr->get_result(), "Implicate virtual cast targets not yet supported." ); 1344 1282 AlternativeFinder finder( indexer, env ); … … 1352 1290 } 1353 1291 1354 void AlternativeFinder:: Finder::postvisit( UntypedMemberExpr *memberExpr ) {1292 void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) { 1355 1293 AlternativeFinder funcFinder( indexer, env ); 1356 1294 funcFinder.findWithAdjustment( memberExpr->get_aggregate() ); … … 1374 1312 } 1375 1313 1376 void AlternativeFinder:: Finder::postvisit( MemberExpr *memberExpr ) {1314 void AlternativeFinder::visit( MemberExpr *memberExpr ) { 1377 1315 alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) ); 1378 1316 } 1379 1317 1380 void AlternativeFinder:: Finder::postvisit( NameExpr *nameExpr ) {1318 void AlternativeFinder::visit( NameExpr *nameExpr ) { 1381 1319 std::list< SymTab::Indexer::IdData > declList; 1382 1320 indexer.lookupId( nameExpr->get_name(), declList ); … … 1399 1337 } 1400 1338 1401 void AlternativeFinder:: Finder::postvisit( VariableExpr *variableExpr ) {1339 void AlternativeFinder::visit( VariableExpr *variableExpr ) { 1402 1340 // not sufficient to clone here, because variable's type may have changed 1403 1341 // since the VariableExpr was originally created. … … 1405 1343 } 1406 1344 1407 void AlternativeFinder:: Finder::postvisit( ConstantExpr *constantExpr ) {1345 void AlternativeFinder::visit( ConstantExpr *constantExpr ) { 1408 1346 alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) ); 1409 1347 } 1410 1348 1411 void AlternativeFinder:: Finder::postvisit( SizeofExpr *sizeofExpr ) {1349 void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) { 1412 1350 if ( sizeofExpr->get_isType() ) { 1413 1351 Type * newType = sizeofExpr->get_type()->clone(); … … 1430 1368 } 1431 1369 1432 void AlternativeFinder:: Finder::postvisit( AlignofExpr *alignofExpr ) {1370 void AlternativeFinder::visit( AlignofExpr *alignofExpr ) { 1433 1371 if ( alignofExpr->get_isType() ) { 1434 1372 Type * newType = alignofExpr->get_type()->clone(); … … 1452 1390 1453 1391 template< typename StructOrUnionType > 1454 void AlternativeFinder:: Finder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {1392 void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) { 1455 1393 std::list< Declaration* > members; 1456 1394 aggInst->lookup( name, members ); … … 1465 1403 } 1466 1404 1467 void AlternativeFinder:: Finder::postvisit( UntypedOffsetofExpr *offsetofExpr ) {1405 void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) { 1468 1406 AlternativeFinder funcFinder( indexer, env ); 1469 1407 // xxx - resolveTypeof? … … 1475 1413 } 1476 1414 1477 void AlternativeFinder:: Finder::postvisit( OffsetofExpr *offsetofExpr ) {1415 void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) { 1478 1416 alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) ); 1479 1417 } 1480 1418 1481 void AlternativeFinder:: Finder::postvisit( OffsetPackExpr *offsetPackExpr ) {1419 void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) { 1482 1420 alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) ); 1483 1421 } … … 1506 1444 } 1507 1445 1508 void AlternativeFinder:: Finder::postvisit( AttrExpr *attrExpr ) {1446 void AlternativeFinder::visit( AttrExpr *attrExpr ) { 1509 1447 // assume no 'pointer-to-attribute' 1510 1448 NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() ); … … 1520 1458 if ( function->get_parameters().size() == 1 ) { 1521 1459 if ( attrExpr->get_isType() ) { 1522 resolveAttr( data, function, attrExpr->get_type(), env, altFinder);1460 resolveAttr( data, function, attrExpr->get_type(), env, *this ); 1523 1461 } else { 1524 1462 AlternativeFinder finder( indexer, env ); … … 1526 1464 for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) { 1527 1465 if ( choice->expr->get_result()->size() == 1 ) { 1528 resolveAttr(data, function, choice->expr->get_result(), choice->env, altFinder);1466 resolveAttr(data, function, choice->expr->get_result(), choice->env, *this ); 1529 1467 } // fi 1530 1468 } // for … … 1541 1479 } 1542 1480 1543 void AlternativeFinder:: Finder::postvisit( LogicalExpr *logicalExpr ) {1481 void AlternativeFinder::visit( LogicalExpr *logicalExpr ) { 1544 1482 AlternativeFinder firstFinder( indexer, env ); 1545 1483 firstFinder.findWithAdjustment( logicalExpr->get_arg1() ); … … 1554 1492 } 1555 1493 1556 void AlternativeFinder:: Finder::postvisit( ConditionalExpr *conditionalExpr ) {1494 void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) { 1557 1495 // find alternatives for condition 1558 1496 AlternativeFinder firstFinder( indexer, env ); … … 1586 1524 } 1587 1525 1588 void AlternativeFinder:: Finder::postvisit( CommaExpr *commaExpr ) {1526 void AlternativeFinder::visit( CommaExpr *commaExpr ) { 1589 1527 TypeEnvironment newEnv( env ); 1590 1528 Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv ); … … 1597 1535 } 1598 1536 1599 void AlternativeFinder:: Finder::postvisit( RangeExpr * rangeExpr ) {1537 void AlternativeFinder::visit( RangeExpr * rangeExpr ) { 1600 1538 // resolve low and high, accept alternatives whose low and high types unify 1601 1539 AlternativeFinder firstFinder( indexer, env ); … … 1619 1557 } 1620 1558 1621 void AlternativeFinder:: Finder::postvisit( UntypedTupleExpr *tupleExpr ) {1559 void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) { 1622 1560 std::vector< AlternativeFinder > subExprAlternatives; 1623 altFinder.findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(),1561 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), 1624 1562 back_inserter( subExprAlternatives ) ); 1625 1563 std::vector< AltList > possibilities; … … 1637 1575 } 1638 1576 1639 void AlternativeFinder:: Finder::postvisit( TupleExpr *tupleExpr ) {1577 void AlternativeFinder::visit( TupleExpr *tupleExpr ) { 1640 1578 alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); 1641 1579 } 1642 1580 1643 void AlternativeFinder:: Finder::postvisit( ImplicitCopyCtorExpr * impCpCtorExpr ) {1581 void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) { 1644 1582 alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) ); 1645 1583 } 1646 1584 1647 void AlternativeFinder:: Finder::postvisit( ConstructorExpr * ctorExpr ) {1585 void AlternativeFinder::visit( ConstructorExpr * ctorExpr ) { 1648 1586 AlternativeFinder finder( indexer, env ); 1649 1587 // don't prune here, since it's guaranteed all alternatives will have the same type … … 1655 1593 } 1656 1594 1657 void AlternativeFinder:: Finder::postvisit( TupleIndexExpr *tupleExpr ) {1595 void AlternativeFinder::visit( TupleIndexExpr *tupleExpr ) { 1658 1596 alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); 1659 1597 } 1660 1598 1661 void AlternativeFinder:: Finder::postvisit( TupleAssignExpr *tupleAssignExpr ) {1599 void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) { 1662 1600 alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) ); 1663 1601 } 1664 1602 1665 void AlternativeFinder:: Finder::postvisit( UniqueExpr *unqExpr ) {1603 void AlternativeFinder::visit( UniqueExpr *unqExpr ) { 1666 1604 AlternativeFinder finder( indexer, env ); 1667 1605 finder.findWithAdjustment( unqExpr->get_expr() ); … … 1673 1611 } 1674 1612 1675 void AlternativeFinder:: Finder::postvisit( StmtExpr *stmtExpr ) {1613 void AlternativeFinder::visit( StmtExpr *stmtExpr ) { 1676 1614 StmtExpr * newStmtExpr = stmtExpr->clone(); 1677 1615 ResolvExpr::resolveStmtExpr( newStmtExpr, indexer ); … … 1680 1618 } 1681 1619 1682 void AlternativeFinder:: Finder::postvisit( UntypedInitExpr *initExpr ) {1620 void AlternativeFinder::visit( UntypedInitExpr *initExpr ) { 1683 1621 // handle each option like a cast 1684 1622 AltList candidates; 1685 PRINT( 1686 std::cerr << "untyped init expr: " << initExpr << std::endl; 1687 ) 1623 PRINT( std::cerr << "untyped init expr: " << initExpr << std::endl; ) 1688 1624 // O(N^2) checks of d-types with e-types 1689 1625 for ( InitAlternative & initAlt : initExpr->get_initAlts() ) { … … 1701 1637 AssertionSet needAssertions, haveAssertions; 1702 1638 OpenVarSet openVars; // find things in env that don't have a "representative type" and claim those are open vars? 1703 PRINT( 1704 std::cerr << " @ " << toType << " " << initAlt.designation << std::endl; 1705 ) 1639 PRINT( std::cerr << " @ " << toType << " " << initAlt.designation << std::endl; ) 1706 1640 // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a 1707 1641 // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results -
src/ResolvExpr/AlternativeFinder.h
r81e1f32b rd2d50d7 38 38 using ExplodedArgs = std::vector< std::vector< ExplodedActual > >; 39 39 40 class AlternativeFinder {40 class AlternativeFinder : public Visitor { 41 41 public: 42 42 AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); … … 94 94 void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); 95 95 private: 96 struct Finder; 96 virtual void visit( ApplicationExpr *applicationExpr ); 97 virtual void visit( UntypedExpr *untypedExpr ); 98 virtual void visit( AddressExpr *addressExpr ); 99 virtual void visit( LabelAddressExpr *labelExpr ); 100 virtual void visit( CastExpr *castExpr ); 101 virtual void visit( VirtualCastExpr *castExpr ); 102 virtual void visit( UntypedMemberExpr *memberExpr ); 103 virtual void visit( MemberExpr *memberExpr ); 104 virtual void visit( NameExpr *variableExpr ); 105 virtual void visit( VariableExpr *variableExpr ); 106 virtual void visit( ConstantExpr *constantExpr ); 107 virtual void visit( SizeofExpr *sizeofExpr ); 108 virtual void visit( AlignofExpr *alignofExpr ); 109 virtual void visit( UntypedOffsetofExpr *offsetofExpr ); 110 virtual void visit( OffsetofExpr *offsetofExpr ); 111 virtual void visit( OffsetPackExpr *offsetPackExpr ); 112 virtual void visit( AttrExpr *attrExpr ); 113 virtual void visit( LogicalExpr *logicalExpr ); 114 virtual void visit( ConditionalExpr *conditionalExpr ); 115 virtual void visit( CommaExpr *commaExpr ); 116 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); 117 virtual void visit( ConstructorExpr * ctorExpr ); 118 virtual void visit( RangeExpr * rangeExpr ); 119 virtual void visit( UntypedTupleExpr *tupleExpr ); 120 virtual void visit( TupleExpr *tupleExpr ); 121 virtual void visit( TupleIndexExpr *tupleExpr ); 122 virtual void visit( TupleAssignExpr *tupleExpr ); 123 virtual void visit( UniqueExpr *unqExpr ); 124 virtual void visit( StmtExpr *stmtExpr ); 125 virtual void visit( UntypedInitExpr *initExpr ); 126 127 /// Adds alternatives for anonymous members 128 void addAnonConversions( const Alternative & alt ); 129 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member 130 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); 131 /// Adds alternatives for member expressions where the left side has tuple type 132 void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); 133 /// Adds alternatives for offsetof expressions, given the base type and name of the member 134 template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name ); 135 /// Takes a final result and checks if its assertions can be satisfied 136 template<typename OutputIterator> 137 void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out ); 138 /// Finds matching alternatives for a function, given a set of arguments 139 template<typename OutputIterator> 140 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out ); 141 /// Checks if assertion parameters match for a new alternative 142 template< typename OutputIterator > 143 void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); 144 97 145 const SymTab::Indexer &indexer; 98 146 AltList alternatives; -
src/ResolvExpr/CommonType.cc
r81e1f32b rd2d50d7 18 18 #include <utility> // for pair 19 19 20 #include "Common/PassVisitor.h"21 20 #include "ResolvExpr/TypeEnvironment.h" // for OpenVarSet, AssertionSet 22 21 #include "SymTab/Indexer.h" // for Indexer … … 30 29 31 30 namespace ResolvExpr { 32 struct CommonType : public WithShortCircuiting { 31 class CommonType : public Visitor { 32 public: 33 33 CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ); 34 34 Type *get_result() const { return result; } 35 36 void previsit( BaseSyntaxNode * ) { visit_children = false; }37 38 void postvisit( VoidType * voidType );39 void postvisit( BasicType * basicType );40 void postvisit( PointerType * pointerType );41 void postvisit( ArrayType * arrayType );42 void postvisit( ReferenceType * refType );43 void postvisit( FunctionType * functionType );44 void postvisit( StructInstType * aggregateUseType );45 void postvisit( UnionInstType * aggregateUseType );46 void postvisit( EnumInstType * aggregateUseType );47 void postvisit( TraitInstType * aggregateUseType );48 void postvisit( TypeInstType * aggregateUseType );49 void postvisit( TupleType * tupleType );50 void postvisit( VarArgsType * varArgsType );51 void postvisit( ZeroType * zeroType );52 void postvisit( OneType * oneType );53 54 35 private: 36 virtual void visit( VoidType *voidType ); 37 virtual void visit( BasicType *basicType ); 38 virtual void visit( PointerType *pointerType ); 39 virtual void visit( ArrayType *arrayType ); 40 virtual void visit( ReferenceType *refType ); 41 virtual void visit( FunctionType *functionType ); 42 virtual void visit( StructInstType *aggregateUseType ); 43 virtual void visit( UnionInstType *aggregateUseType ); 44 virtual void visit( EnumInstType *aggregateUseType ); 45 virtual void visit( TraitInstType *aggregateUseType ); 46 virtual void visit( TypeInstType *aggregateUseType ); 47 virtual void visit( TupleType *tupleType ); 48 virtual void visit( VarArgsType *varArgsType ); 49 virtual void visit( ZeroType *zeroType ); 50 virtual void visit( OneType *oneType ); 51 55 52 template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ); 56 53 template< typename RefType > void handleRefType( RefType *inst, Type *other ); … … 83 80 84 81 Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) { 85 PassVisitor<CommonType>visitor( type2, widenFirst, widenSecond, indexer, env, openVars );82 CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars ); 86 83 87 84 int depth1 = type1->referenceDepth(); … … 119 116 120 117 type1->accept( visitor ); 121 Type *result = visitor. pass.get_result();118 Type *result = visitor.get_result(); 122 119 if ( ! result ) { 123 120 // this appears to be handling for opaque type declarations … … 191 188 } 192 189 193 void CommonType:: postvisit( VoidType * ) {}194 195 void CommonType:: postvisit( BasicType *basicType ) {190 void CommonType::visit( VoidType * ) {} 191 192 void CommonType::visit( BasicType *basicType ) { 196 193 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { 197 194 BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; … … 222 219 } 223 220 224 void CommonType:: postvisit( PointerType *pointerType ) {221 void CommonType::visit( PointerType *pointerType ) { 225 222 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { 226 223 // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl; … … 257 254 } 258 255 259 void CommonType:: postvisit( ArrayType * ) {}260 261 void CommonType:: postvisit( ReferenceType *refType ) {256 void CommonType::visit( ArrayType * ) {} 257 258 void CommonType::visit( ReferenceType *refType ) { 262 259 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { 263 260 // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl; … … 294 291 } 295 292 296 void CommonType:: postvisit( FunctionType * ) {}297 void CommonType:: postvisit( StructInstType * ) {}298 void CommonType:: postvisit( UnionInstType * ) {}299 300 void CommonType:: postvisit( EnumInstType *enumInstType ) {293 void CommonType::visit( FunctionType * ) {} 294 void CommonType::visit( StructInstType * ) {} 295 void CommonType::visit( UnionInstType * ) {} 296 297 void CommonType::visit( EnumInstType *enumInstType ) { 301 298 if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { 302 299 // reuse BasicType, EnumInstType code by swapping type2 with enumInstType 303 result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars ); 304 } // if 305 } 306 307 void CommonType::postvisit( TraitInstType * ) { 308 } 309 310 void CommonType::postvisit( TypeInstType *inst ) { 300 ValueGuard< Type * > temp( type2 ); 301 type2 = enumInstType; 302 temp.old->accept( *this ); 303 } // if 304 } 305 306 void CommonType::visit( TraitInstType * ) { 307 } 308 309 void CommonType::visit( TypeInstType *inst ) { 311 310 if ( widenFirst ) { 312 311 NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); … … 330 329 } 331 330 332 void CommonType:: postvisit( TupleType * ) {}333 void CommonType:: postvisit( VarArgsType * ) {}334 335 void CommonType:: postvisit( ZeroType *zeroType ) {331 void CommonType::visit( TupleType * ) {} 332 void CommonType::visit( VarArgsType * ) {} 333 334 void CommonType::visit( ZeroType *zeroType ) { 336 335 if ( widenFirst ) { 337 336 if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { … … 347 346 } 348 347 349 void CommonType:: postvisit( OneType *oneType ) {348 void CommonType::visit( OneType *oneType ) { 350 349 if ( widenFirst ) { 351 350 if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { -
src/ResolvExpr/CurrentObject.cc
r81e1f32b rd2d50d7 443 443 return new UnionIterator( uit ); 444 444 } else { 445 assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str());445 assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" ); 446 446 return new SimpleIterator( type ); 447 447 } -
src/ResolvExpr/PtrsAssignable.cc
r81e1f32b rd2d50d7 14 14 // 15 15 16 #include "Common/PassVisitor.h"17 16 #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment 18 17 #include "SynTree/Type.h" // for TypeInstType, Type, BasicType … … 21 20 22 21 namespace ResolvExpr { 23 struct PtrsAssignable : public WithShortCircuiting { 22 class PtrsAssignable : public Visitor { 23 public: 24 24 PtrsAssignable( Type *dest, const TypeEnvironment &env ); 25 25 26 26 int get_result() const { return result; } 27 27 28 void previsit( Type * ) { visit_children = false; } 29 30 void postvisit( VoidType * voidType ); 31 void postvisit( BasicType * basicType ); 32 void postvisit( PointerType * pointerType ); 33 void postvisit( ArrayType * arrayType ); 34 void postvisit( FunctionType * functionType ); 35 void postvisit( StructInstType * inst ); 36 void postvisit( UnionInstType * inst ); 37 void postvisit( EnumInstType * inst ); 38 void postvisit( TraitInstType * inst ); 39 void postvisit( TypeInstType * inst ); 40 void postvisit( TupleType * tupleType ); 41 void postvisit( VarArgsType * varArgsType ); 42 void postvisit( ZeroType * zeroType ); 43 void postvisit( OneType * oneType ); 28 virtual void visit( VoidType *voidType ); 29 virtual void visit( BasicType *basicType ); 30 virtual void visit( PointerType *pointerType ); 31 virtual void visit( ArrayType *arrayType ); 32 virtual void visit( FunctionType *functionType ); 33 virtual void visit( StructInstType *inst ); 34 virtual void visit( UnionInstType *inst ); 35 virtual void visit( EnumInstType *inst ); 36 virtual void visit( TraitInstType *inst ); 37 virtual void visit( TypeInstType *inst ); 38 virtual void visit( TupleType *tupleType ); 39 virtual void visit( VarArgsType *varArgsType ); 40 virtual void visit( ZeroType *zeroType ); 41 virtual void visit( OneType *oneType ); 44 42 private: 45 43 Type *dest; … … 61 59 return -1; 62 60 } else { 63 P assVisitor<PtrsAssignable>ptrs( dest, env );61 PtrsAssignable ptrs( dest, env ); 64 62 src->accept( ptrs ); 65 return ptrs. pass.get_result();63 return ptrs.get_result(); 66 64 } // if 67 65 } … … 69 67 PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} 70 68 71 void PtrsAssignable:: postvisit( VoidType * ) {69 void PtrsAssignable::visit( VoidType * ) { 72 70 // T * = void * is disallowed - this is a change from C, where any 73 71 // void * can be assigned or passed to a non-void pointer without a cast. 74 72 } 75 73 76 void PtrsAssignable:: postvisit( __attribute__((unused)) BasicType *basicType ) {}77 void PtrsAssignable:: postvisit( __attribute__((unused)) PointerType *pointerType ) {}78 void PtrsAssignable:: postvisit( __attribute__((unused)) ArrayType *arrayType ) {}79 void PtrsAssignable:: postvisit( __attribute__((unused)) FunctionType *functionType ) {}74 void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {} 75 void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {} 76 void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {} 77 void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {} 80 78 81 void PtrsAssignable:: postvisit( __attribute__((unused)) StructInstType *inst ) {}82 void PtrsAssignable:: postvisit( __attribute__((unused)) UnionInstType *inst ) {}79 void PtrsAssignable::visit( __attribute__((unused)) StructInstType *inst ) {} 80 void PtrsAssignable::visit( __attribute__((unused)) UnionInstType *inst ) {} 83 81 84 void PtrsAssignable:: postvisit( EnumInstType * ) {82 void PtrsAssignable::visit( EnumInstType * ) { 85 83 if ( dynamic_cast< BasicType* >( dest ) ) { 86 84 // int * = E *, etc. is safe. This isn't technically correct, as each … … 93 91 } 94 92 95 void PtrsAssignable:: postvisit( __attribute__((unused)) TraitInstType *inst ) {}96 void PtrsAssignable:: postvisit( TypeInstType *inst ) {93 void PtrsAssignable::visit( __attribute__((unused)) TraitInstType *inst ) {} 94 void PtrsAssignable::visit( TypeInstType *inst ) { 97 95 EqvClass eqvClass; 98 96 if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) { … … 102 100 } 103 101 104 void PtrsAssignable:: postvisit( __attribute__((unused)) TupleType *tupleType ) {}105 void PtrsAssignable:: postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {}106 void PtrsAssignable:: postvisit( __attribute__((unused)) ZeroType *zeroType ) {}107 void PtrsAssignable:: postvisit( __attribute__((unused)) OneType *oneType ) {}102 void PtrsAssignable::visit( __attribute__((unused)) TupleType *tupleType ) {} 103 void PtrsAssignable::visit( __attribute__((unused)) VarArgsType *varArgsType ) {} 104 void PtrsAssignable::visit( __attribute__((unused)) ZeroType *zeroType ) {} 105 void PtrsAssignable::visit( __attribute__((unused)) OneType *oneType ) {} 108 106 109 107 } // namespace ResolvExpr -
src/ResolvExpr/PtrsCastable.cc
r81e1f32b rd2d50d7 14 14 // 15 15 16 #include "Common/PassVisitor.h"17 16 #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment 18 17 #include "SymTab/Indexer.h" // for Indexer … … 22 21 #include "typeops.h" // for ptrsAssignable 23 22 23 24 24 namespace ResolvExpr { 25 struct PtrsCastable : public WithShortCircuiting{25 class PtrsCastable : public Visitor { 26 26 public: 27 27 PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); … … 29 29 int get_result() const { return result; } 30 30 31 void previsit( Type * ) { visit_children = false; } 32 33 void postvisit( VoidType * voidType ); 34 void postvisit( BasicType * basicType ); 35 void postvisit( PointerType * pointerType ); 36 void postvisit( ArrayType * arrayType ); 37 void postvisit( FunctionType * functionType ); 38 void postvisit( StructInstType * inst ); 39 void postvisit( UnionInstType * inst ); 40 void postvisit( EnumInstType * inst ); 41 void postvisit( TraitInstType * inst ); 42 void postvisit( TypeInstType * inst ); 43 void postvisit( TupleType * tupleType ); 44 void postvisit( VarArgsType * varArgsType ); 45 void postvisit( ZeroType * zeroType ); 46 void postvisit( OneType * oneType ); 31 virtual void visit(VoidType *voidType); 32 virtual void visit(BasicType *basicType); 33 virtual void visit(PointerType *pointerType); 34 virtual void visit(ArrayType *arrayType); 35 virtual void visit(FunctionType *functionType); 36 virtual void visit(StructInstType *inst); 37 virtual void visit(UnionInstType *inst); 38 virtual void visit(EnumInstType *inst); 39 virtual void visit(TraitInstType *inst); 40 virtual void visit(TypeInstType *inst); 41 virtual void visit(TupleType *tupleType); 42 virtual void visit(VarArgsType *varArgsType); 43 virtual void visit(ZeroType *zeroType); 44 virtual void visit(OneType *oneType); 47 45 private: 48 46 Type *dest; … … 81 79 EqvClass eqvClass; 82 80 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { 83 // xxx - should this be ptrsCastable?84 81 return ptrsAssignable( src, eqvClass.type, env ); 85 82 } // if … … 88 85 return objectCast( src, env, indexer ); 89 86 } else { 90 P assVisitor<PtrsCastable>ptrs( dest, env, indexer );87 PtrsCastable ptrs( dest, env, indexer ); 91 88 src->accept( ptrs ); 92 return ptrs. pass.get_result();89 return ptrs.get_result(); 93 90 } // if 94 91 } … … 98 95 } 99 96 100 void PtrsCastable:: postvisit( VoidType * ) {97 void PtrsCastable::visit( VoidType * ) { 101 98 result = objectCast( dest, env, indexer ); 102 99 } 103 100 104 void PtrsCastable:: postvisit( BasicType * ) {101 void PtrsCastable::visit( BasicType * ) { 105 102 result = objectCast( dest, env, indexer ); 106 103 } 107 104 108 void PtrsCastable:: postvisit( PointerType * ) {105 void PtrsCastable::visit( PointerType * ) { 109 106 result = objectCast( dest, env, indexer ); 110 107 } 111 108 112 void PtrsCastable:: postvisit( ArrayType * ) {109 void PtrsCastable::visit( ArrayType * ) { 113 110 result = objectCast( dest, env, indexer ); 114 111 } 115 112 116 void PtrsCastable:: postvisit( FunctionType * ) {113 void PtrsCastable::visit( FunctionType * ) { 117 114 // result = -1; 118 115 result = functionCast( dest, env, indexer ); 119 116 } 120 117 121 void PtrsCastable:: postvisit( StructInstType * ) {118 void PtrsCastable::visit( StructInstType * ) { 122 119 result = objectCast( dest, env, indexer ); 123 120 } 124 121 125 void PtrsCastable:: postvisit( UnionInstType * ) {122 void PtrsCastable::visit( UnionInstType * ) { 126 123 result = objectCast( dest, env, indexer ); 127 124 } 128 125 129 void PtrsCastable:: postvisit( EnumInstType * ) {126 void PtrsCastable::visit( EnumInstType * ) { 130 127 if ( dynamic_cast< EnumInstType* >( dest ) ) { 131 128 result = 1; … … 141 138 } 142 139 143 void PtrsCastable:: postvisit( TraitInstType * ) {}140 void PtrsCastable::visit( TraitInstType * ) {} 144 141 145 void PtrsCastable:: postvisit(TypeInstType *inst) {142 void PtrsCastable::visit(TypeInstType *inst) { 146 143 //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; 147 144 result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; 148 145 } 149 146 150 void PtrsCastable:: postvisit( TupleType * ) {147 void PtrsCastable::visit( TupleType * ) { 151 148 result = objectCast( dest, env, indexer ); 152 149 } 153 150 154 void PtrsCastable:: postvisit( VarArgsType * ) {151 void PtrsCastable::visit( VarArgsType * ) { 155 152 result = objectCast( dest, env, indexer ); 156 153 } 157 154 158 void PtrsCastable:: postvisit( ZeroType * ) {155 void PtrsCastable::visit( ZeroType * ) { 159 156 result = objectCast( dest, env, indexer ); 160 157 } 161 158 162 void PtrsCastable:: postvisit( OneType * ) {159 void PtrsCastable::visit( OneType * ) { 163 160 result = objectCast( dest, env, indexer ); 164 161 } -
src/ResolvExpr/RenameVars.cc
r81e1f32b rd2d50d7 19 19 #include <utility> // for pair 20 20 21 #include "Common/PassVisitor.h"22 21 #include "Common/SemanticError.h" // for SemanticError 23 22 #include "RenameVars.h" … … 28 27 29 28 namespace ResolvExpr { 30 namespace { 31 struct RenameVars { 32 RenameVars(); 33 void reset(); 29 RenameVars global_renamer; 34 30 35 void previsit( TypeInstType * instType ); 36 void previsit( Type * ); 37 void postvisit( Type * ); 38 39 private: 40 int level, resetCount; 41 std::list< std::map< std::string, std::string > > mapStack; 42 }; 43 44 PassVisitor<RenameVars> global_renamer; 45 } // namespace 46 47 void renameTyVars( Type * t ) { 48 t->accept( global_renamer ); 31 RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) { 32 mapStack.push_front( std::map< std::string, std::string >() ); 49 33 } 50 34 51 void resetTyVarRenaming() { 52 global_renamer.pass.reset(); 35 void RenameVars::reset() { 36 level = 0; 37 resetCount++; 53 38 } 54 39 55 namespace{56 RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {57 mapStack.push_front( std::map< std::string, std::string >());58 }40 void RenameVars::visit( VoidType *voidType ) { 41 typeBefore( voidType ); 42 typeAfter( voidType ); 43 } 59 44 60 void RenameVars::reset() {61 level = 0;62 resetCount++;63 }45 void RenameVars::visit( BasicType *basicType ) { 46 typeBefore( basicType ); 47 typeAfter( basicType ); 48 } 64 49 65 void RenameVars::previsit( TypeInstType * instType ) { 66 previsit( (Type *)instType ); 67 std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->name ); 68 if ( i != mapStack.front().end() ) { 69 instType->name = i->second; 70 } // if 71 } 50 void RenameVars::visit( PointerType *pointerType ) { 51 typeBefore( pointerType ); 52 maybeAccept( pointerType->get_base(), *this ); 53 typeAfter( pointerType ); 54 } 72 55 73 void RenameVars::previsit( Type * type ) { 74 if ( ! type->forall.empty() ) { 75 // copies current name mapping into new mapping 76 mapStack.push_front( mapStack.front() ); 77 // renames all "forall" type names to `_${level}_${name}' 78 for ( auto td : type->forall ) { 79 std::ostringstream output; 80 output << "_" << resetCount << "_" << level << "_" << td->name; 81 std::string newname( output.str() ); 82 mapStack.front()[ td->get_name() ] = newname; 83 td->name = newname; 84 // ditto for assertion names, the next level in 85 level++; 86 // acceptAll( td->assertions, *this ); 87 } // for 88 } // if 89 } 56 void RenameVars::visit( ArrayType *arrayType ) { 57 typeBefore( arrayType ); 58 maybeAccept( arrayType->get_dimension(), *this ); 59 maybeAccept( arrayType->get_base(), *this ); 60 typeAfter( arrayType ); 61 } 90 62 91 void RenameVars::postvisit( Type * type ) { 92 // clears name mapping added by typeBefore() 93 if ( ! type->forall.empty() ) { 94 mapStack.pop_front(); 95 } // if 96 } 97 } // namespace 63 void RenameVars::visit( FunctionType *functionType ) { 64 typeBefore( functionType ); 65 acceptAll( functionType->get_returnVals(), *this ); 66 acceptAll( functionType->get_parameters(), *this ); 67 typeAfter( functionType ); 68 } 69 70 void RenameVars::visit( StructInstType *aggregateUseType ) { 71 typeBefore( aggregateUseType ); 72 acceptAll( aggregateUseType->get_parameters(), *this ); 73 typeAfter( aggregateUseType ); 74 } 75 76 void RenameVars::visit( UnionInstType *aggregateUseType ) { 77 typeBefore( aggregateUseType ); 78 acceptAll( aggregateUseType->get_parameters(), *this ); 79 typeAfter( aggregateUseType ); 80 } 81 82 void RenameVars::visit( EnumInstType *aggregateUseType ) { 83 typeBefore( aggregateUseType ); 84 acceptAll( aggregateUseType->get_parameters(), *this ); 85 typeAfter( aggregateUseType ); 86 } 87 88 void RenameVars::visit( TraitInstType *aggregateUseType ) { 89 typeBefore( aggregateUseType ); 90 acceptAll( aggregateUseType->get_parameters(), *this ); 91 typeAfter( aggregateUseType ); 92 } 93 94 void RenameVars::visit( TypeInstType *instType ) { 95 typeBefore( instType ); 96 std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() ); 97 if ( i != mapStack.front().end() ) { 98 instType->set_name( i->second ); 99 } else { 100 } // if 101 acceptAll( instType->get_parameters(), *this ); 102 typeAfter( instType ); 103 } 104 105 void RenameVars::visit( TupleType *tupleType ) { 106 typeBefore( tupleType ); 107 acceptAll( tupleType->get_types(), *this ); 108 typeAfter( tupleType ); 109 } 110 111 void RenameVars::visit( VarArgsType *varArgsType ) { 112 typeBefore( varArgsType ); 113 typeAfter( varArgsType ); 114 } 115 116 void RenameVars::visit( ZeroType *zeroType ) { 117 typeBefore( zeroType ); 118 typeAfter( zeroType ); 119 } 120 121 void RenameVars::visit( OneType *oneType ) { 122 typeBefore( oneType ); 123 typeAfter( oneType ); 124 } 125 126 void RenameVars::typeBefore( Type *type ) { 127 if ( ! type->get_forall().empty() ) { 128 // copies current name mapping into new mapping 129 mapStack.push_front( mapStack.front() ); 130 // renames all "forall" type names to `_${level}_${name}' 131 for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { 132 std::ostringstream output; 133 output << "_" << resetCount << "_" << level << "_" << (*i)->get_name(); 134 std::string newname( output.str() ); 135 mapStack.front()[ (*i)->get_name() ] = newname; 136 (*i)->set_name( newname ); 137 // ditto for assertion names, the next level in 138 level++; 139 acceptAll( (*i)->get_assertions(), *this ); 140 } // for 141 } // if 142 } 143 144 void RenameVars::typeAfter( Type *type ) { 145 // clears name mapping added by typeBefore() 146 if ( ! type->get_forall().empty() ) { 147 mapStack.pop_front(); 148 } // if 149 } 150 98 151 } // namespace ResolvExpr 99 152 -
src/ResolvExpr/RenameVars.h
r81e1f32b rd2d50d7 24 24 25 25 namespace ResolvExpr { 26 26 27 /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID 27 void renameTyVars( Type * ); 28 class RenameVars : public Visitor { 29 public: 30 RenameVars(); 31 void reset(); 32 private: 33 virtual void visit( VoidType *basicType ); 34 virtual void visit( BasicType *basicType ); 35 virtual void visit( PointerType *pointerType ); 36 virtual void visit( ArrayType *arrayType ); 37 virtual void visit( FunctionType *functionType ); 38 virtual void visit( StructInstType *aggregateUseType ); 39 virtual void visit( UnionInstType *aggregateUseType ); 40 virtual void visit( EnumInstType *aggregateUseType ); 41 virtual void visit( TraitInstType *aggregateUseType ); 42 virtual void visit( TypeInstType *aggregateUseType ); 43 virtual void visit( TupleType *tupleType ); 44 virtual void visit( VarArgsType *varArgsType ); 45 virtual void visit( ZeroType *zeroType ); 46 virtual void visit( OneType *oneType ); 28 47 29 /// resets internal state of renamer to avoid overflow 30 void resetTyVarRenaming(); 48 void typeBefore( Type *type ); 49 void typeAfter( Type *type ); 50 int level, resetCount; 51 std::list< std::map< std::string, std::string > > mapStack; 52 }; 53 54 extern RenameVars global_renamer; 31 55 } // namespace ResolvExpr 32 56 -
src/ResolvExpr/Resolver.cc
r81e1f32b rd2d50d7 132 132 133 133 void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) { 134 resetTyVarRenaming();134 global_renamer.reset(); 135 135 TypeEnvironment env; 136 136 Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); -
src/ResolvExpr/Unify.cc
r81e1f32b rd2d50d7 44 44 namespace ResolvExpr { 45 45 46 struct Unify : public WithShortCircuiting { 46 class Unify : public Visitor { 47 public: 47 48 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ); 48 49 49 50 bool get_result() const { return result; } 50 51 void previsit( BaseSyntaxNode * ) { visit_children = false; }52 53 void postvisit( VoidType * voidType );54 void postvisit( BasicType * basicType );55 void postvisit( PointerType * pointerType );56 void postvisit( ArrayType * arrayType );57 void postvisit( ReferenceType * refType );58 void postvisit( FunctionType * functionType );59 void postvisit( StructInstType * aggregateUseType );60 void postvisit( UnionInstType * aggregateUseType );61 void postvisit( EnumInstType * aggregateUseType );62 void postvisit( TraitInstType * aggregateUseType );63 void postvisit( TypeInstType * aggregateUseType );64 void postvisit( TupleType * tupleType );65 void postvisit( VarArgsType * varArgsType );66 void postvisit( ZeroType * zeroType );67 void postvisit( OneType * oneType );68 69 51 private: 52 virtual void visit(VoidType *voidType); 53 virtual void visit(BasicType *basicType); 54 virtual void visit(PointerType *pointerType); 55 virtual void visit(ArrayType *arrayType); 56 virtual void visit(ReferenceType *refType); 57 virtual void visit(FunctionType *functionType); 58 virtual void visit(StructInstType *aggregateUseType); 59 virtual void visit(UnionInstType *aggregateUseType); 60 virtual void visit(EnumInstType *aggregateUseType); 61 virtual void visit(TraitInstType *aggregateUseType); 62 virtual void visit(TypeInstType *aggregateUseType); 63 virtual void visit(TupleType *tupleType); 64 virtual void visit(VarArgsType *varArgsType); 65 virtual void visit(ZeroType *zeroType); 66 virtual void visit(OneType *oneType); 67 70 68 template< typename RefType > void handleRefType( RefType *inst, Type *other ); 71 69 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other ); … … 327 325 result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); 328 326 } else { 329 PassVisitor<Unify>comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );327 Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); 330 328 type1->accept( comparator ); 331 result = comparator. pass.get_result();329 result = comparator.get_result(); 332 330 } // if 333 331 #ifdef DEBUG … … 406 404 } 407 405 408 void Unify:: postvisit( __attribute__((unused)) VoidType *voidType) {406 void Unify::visit( __attribute__((unused)) VoidType *voidType) { 409 407 result = dynamic_cast< VoidType* >( type2 ); 410 408 } 411 409 412 void Unify:: postvisit(BasicType *basicType) {410 void Unify::visit(BasicType *basicType) { 413 411 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { 414 412 result = basicType->get_kind() == otherBasic->get_kind(); … … 438 436 } 439 437 440 void Unify:: postvisit(PointerType *pointerType) {438 void Unify::visit(PointerType *pointerType) { 441 439 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { 442 440 result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); … … 446 444 } 447 445 448 void Unify:: postvisit(ReferenceType *refType) {446 void Unify::visit(ReferenceType *refType) { 449 447 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { 450 448 result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); … … 454 452 } 455 453 456 void Unify:: postvisit(ArrayType *arrayType) {454 void Unify::visit(ArrayType *arrayType) { 457 455 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 ); 458 456 // to unify, array types must both be VLA or both not VLA … … 569 567 } 570 568 571 void Unify:: postvisit(FunctionType *functionType) {569 void Unify::visit(FunctionType *functionType) { 572 570 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 ); 573 571 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) { … … 671 669 } 672 670 673 void Unify:: postvisit(StructInstType *structInst) {671 void Unify::visit(StructInstType *structInst) { 674 672 handleGenericRefType( structInst, type2 ); 675 673 } 676 674 677 void Unify:: postvisit(UnionInstType *unionInst) {675 void Unify::visit(UnionInstType *unionInst) { 678 676 handleGenericRefType( unionInst, type2 ); 679 677 } 680 678 681 void Unify:: postvisit(EnumInstType *enumInst) {679 void Unify::visit(EnumInstType *enumInst) { 682 680 handleRefType( enumInst, type2 ); 683 681 } 684 682 685 void Unify:: postvisit(TraitInstType *contextInst) {683 void Unify::visit(TraitInstType *contextInst) { 686 684 handleRefType( contextInst, type2 ); 687 685 } 688 686 689 void Unify:: postvisit(TypeInstType *typeInst) {687 void Unify::visit(TypeInstType *typeInst) { 690 688 assert( openVars.find( typeInst->get_name() ) == openVars.end() ); 691 689 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 ); … … 742 740 } 743 741 744 void Unify:: postvisit(TupleType *tupleType) {742 void Unify::visit(TupleType *tupleType) { 745 743 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) { 746 744 std::unique_ptr<TupleType> flat1( tupleType->clone() ); … … 759 757 } 760 758 761 void Unify:: postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {759 void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) { 762 760 result = dynamic_cast< VarArgsType* >( type2 ); 763 761 } 764 762 765 void Unify:: postvisit( __attribute__((unused)) ZeroType *zeroType ) {763 void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) { 766 764 result = dynamic_cast< ZeroType* >( type2 ); 767 765 } 768 766 769 void Unify:: postvisit( __attribute__((unused)) OneType *oneType ) {767 void Unify::visit( __attribute__((unused)) OneType *oneType ) { 770 768 result = dynamic_cast< OneType* >( type2 ); 771 769 } -
src/SymTab/Mangler.cc
r81e1f32b rd2d50d7 23 23 24 24 #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup 25 #include "Common/PassVisitor.h"26 25 #include "Common/SemanticError.h" // for SemanticError 27 26 #include "Common/utility.h" // for toString … … 32 31 33 32 namespace SymTab { 34 namespace Mangler{35 namespace {36 /// Mangles names to a unique C identifier37 struct Mangler : public WithShortCircuiting, public WithVisitorRef<Mangler> {38 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams );39 Mangler( const Mangler & ); 40 41 void previsit( BaseSyntaxNode * ) { visit_children = false; }42 43 void postvisit( ObjectDecl * declaration);44 void postvisit( FunctionDecl * declaration );45 void postvisit( TypeDecl * declaration ); 46 47 void postvisit( VoidType * voidType );48 void postvisit( BasicType * basicType ); 49 void postvisit( PointerType * pointerType );50 void postvisit( ArrayType * arrayType );51 void postvisit( ReferenceType * refType );52 void postvisit( FunctionType * functionType );53 void postvisit( StructInstType * aggregateUseType );54 void postvisit( UnionInstType * aggregateUseType );55 void postvisit( EnumInstType * aggregateUseType );56 void postvisit( TypeInstType * aggregateUseType ); 57 void postvisit( TupleType * tupleType );58 void postvisit( VarArgsType * varArgsType );59 void postvisit( ZeroType * zeroType );60 void postvisit( OneType * oneType);61 62 std::string get_mangleName() { return mangleName.str(); }63 private:64 std::ostringstream mangleName; ///< Mangled name being constructed65 typedef std::map< std::string, std::pair< int, int > > VarMapType;66 VarMapType varNums; ///< Map of type variables to indices67 int nextVarNum; ///< Next type variable index68 bool isTopLevel; ///< Is the Mangler at the top level69 bool mangleOverridable; ///< Specially mangle overridable built-in methods70 bool typeMode; ///< Produce a unique mangled name for a type71 bool mangleGenericParams; ///< Include generic parameters in name mangling if true72 73 void mangleDecl( DeclarationWithType *declaration );74 void mangleRef( ReferenceToType *refType, std::string prefix );75 76 void printQualifiers( Type *type );77 }; // Mangler78 } // namespace79 80 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ){81 PassVisitor<Mangler> mangler( mangleOverridable, typeMode, mangleGenericParams );82 maybeAccept( decl, mangler);83 return mangler.pass.get_mangleName();33 std::string Mangler::mangleType( Type * ty ) { 34 Mangler mangler( false, true, true ); 35 maybeAccept( ty, mangler ); 36 return mangler.get_mangleName(); 37 } 38 39 std::string Mangler::mangleConcrete( Type* ty ) { 40 Mangler mangler( false, false, false ); 41 maybeAccept( ty, mangler ); 42 return mangler.get_mangleName(); 43 } 44 45 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) 46 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ), mangleGenericParams( mangleGenericParams ) {} 47 48 Mangler::Mangler( const Mangler &rhs ) : mangleName() { 49 varNums = rhs.varNums; 50 nextVarNum = rhs.nextVarNum; 51 isTopLevel = rhs.isTopLevel; 52 mangleOverridable = rhs.mangleOverridable; 53 typeMode = rhs.typeMode; 54 } 55 56 void Mangler::mangleDecl( DeclarationWithType * declaration ) { 57 bool wasTopLevel = isTopLevel; 58 if ( isTopLevel ) { 59 varNums.clear(); 60 nextVarNum = 0; 61 isTopLevel = false; 62 } // if 63 mangleName << "__"; 64 CodeGen::OperatorInfo opInfo; 65 if ( operatorLookup( declaration->get_name(), opInfo ) ) { 66 mangleName << opInfo.outputName; 67 } else { 68 mangleName << declaration->get_name(); 69 } // if 70 mangleName << "__"; 71 maybeAccept( declaration->get_type(), *this ); 72 if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { 73 // want to be able to override autogenerated and intrinsic routines, 74 // so they need a different name mangling 75 if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { 76 mangleName << "autogen__"; 77 } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { 78 mangleName << "intrinsic__"; 79 } else { 80 // if we add another kind of overridable function, this has to change 81 assert( false && "unknown overrideable linkage" ); 82 } // if 84 83 } 85 86 std::string mangleType( Type * ty ) { 87 PassVisitor<Mangler> mangler( false, true, true ); 88 maybeAccept( ty, mangler ); 89 return mangler.pass.get_mangleName(); 84 isTopLevel = wasTopLevel; 85 } 86 87 void Mangler::visit( ObjectDecl * declaration ) { 88 mangleDecl( declaration ); 89 } 90 91 void Mangler::visit( FunctionDecl * declaration ) { 92 mangleDecl( declaration ); 93 } 94 95 void Mangler::visit( VoidType * voidType ) { 96 printQualifiers( voidType ); 97 mangleName << "v"; 98 } 99 100 void Mangler::visit( BasicType * basicType ) { 101 static const char *btLetter[] = { 102 "b", // Bool 103 "c", // Char 104 "Sc", // SignedChar 105 "Uc", // UnsignedChar 106 "s", // ShortSignedInt 107 "Us", // ShortUnsignedInt 108 "i", // SignedInt 109 "Ui", // UnsignedInt 110 "l", // LongSignedInt 111 "Ul", // LongUnsignedInt 112 "q", // LongLongSignedInt 113 "Uq", // LongLongUnsignedInt 114 "f", // Float 115 "d", // Double 116 "r", // LongDouble 117 "Xf", // FloatComplex 118 "Xd", // DoubleComplex 119 "Xr", // LongDoubleComplex 120 "If", // FloatImaginary 121 "Id", // DoubleImaginary 122 "Ir", // LongDoubleImaginary 123 "w", // SignedInt128 124 "Uw", // UnsignedInt128 125 }; 126 127 printQualifiers( basicType ); 128 mangleName << btLetter[ basicType->get_kind() ]; 129 } 130 131 void Mangler::visit( PointerType * pointerType ) { 132 printQualifiers( pointerType ); 133 mangleName << "P"; 134 maybeAccept( pointerType->get_base(), *this ); 135 } 136 137 void Mangler::visit( ArrayType * arrayType ) { 138 // TODO: encode dimension 139 printQualifiers( arrayType ); 140 mangleName << "A0"; 141 maybeAccept( arrayType->get_base(), *this ); 142 } 143 144 void Mangler::visit( ReferenceType * refType ) { 145 printQualifiers( refType ); 146 mangleName << "R"; 147 maybeAccept( refType->get_base(), *this ); 148 } 149 150 namespace { 151 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { 152 std::list< Type* > ret; 153 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 154 std::mem_fun( &DeclarationWithType::get_type ) ); 155 return ret; 90 156 } 91 92 std::string mangleConcrete( Type * ty ) { 93 PassVisitor<Mangler> mangler( false, false, false ); 94 maybeAccept( ty, mangler ); 95 return mangler.pass.get_mangleName(); 96 } 97 98 namespace { 99 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) 100 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ), mangleGenericParams( mangleGenericParams ) {} 101 102 Mangler::Mangler( const Mangler &rhs ) : mangleName() { 103 varNums = rhs.varNums; 104 nextVarNum = rhs.nextVarNum; 105 isTopLevel = rhs.isTopLevel; 106 mangleOverridable = rhs.mangleOverridable; 107 typeMode = rhs.typeMode; 108 } 109 110 void Mangler::mangleDecl( DeclarationWithType * declaration ) { 111 bool wasTopLevel = isTopLevel; 112 if ( isTopLevel ) { 113 varNums.clear(); 114 nextVarNum = 0; 115 isTopLevel = false; 116 } // if 117 mangleName << "__"; 118 CodeGen::OperatorInfo opInfo; 119 if ( operatorLookup( declaration->get_name(), opInfo ) ) { 120 mangleName << opInfo.outputName; 121 } else { 122 mangleName << declaration->get_name(); 123 } // if 124 mangleName << "__"; 125 maybeAccept( declaration->get_type(), *visitor ); 126 if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { 127 // want to be able to override autogenerated and intrinsic routines, 128 // so they need a different name mangling 129 if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { 130 mangleName << "autogen__"; 131 } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { 132 mangleName << "intrinsic__"; 133 } else { 134 // if we add another kind of overridable function, this has to change 135 assert( false && "unknown overrideable linkage" ); 136 } // if 157 } 158 159 void Mangler::visit( FunctionType * functionType ) { 160 printQualifiers( functionType ); 161 mangleName << "F"; 162 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); 163 acceptAll( returnTypes, *this ); 164 mangleName << "_"; 165 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); 166 acceptAll( paramTypes, *this ); 167 mangleName << "_"; 168 } 169 170 void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { 171 printQualifiers( refType ); 172 173 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 174 175 if ( mangleGenericParams ) { 176 std::list< Expression* >& params = refType->get_parameters(); 177 if ( ! params.empty() ) { 178 mangleName << "_"; 179 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { 180 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 181 assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); 182 maybeAccept( paramType->get_type(), *this ); 137 183 } 138 isTopLevel = wasTopLevel;139 }140 141 void Mangler::postvisit( ObjectDecl * declaration ) {142 mangleDecl( declaration );143 }144 145 void Mangler::postvisit( FunctionDecl * declaration ) {146 mangleDecl( declaration );147 }148 149 void Mangler::postvisit( VoidType * voidType ) {150 printQualifiers( voidType );151 mangleName << "v";152 }153 154 void Mangler::postvisit( BasicType * basicType ) {155 static const char *btLetter[] = {156 "b", // Bool157 "c", // Char158 "Sc", // SignedChar159 "Uc", // UnsignedChar160 "s", // ShortSignedInt161 "Us", // ShortUnsignedInt162 "i", // SignedInt163 "Ui", // UnsignedInt164 "l", // LongSignedInt165 "Ul", // LongUnsignedInt166 "q", // LongLongSignedInt167 "Uq", // LongLongUnsignedInt168 "f", // Float169 "d", // Double170 "r", // LongDouble171 "Xf", // FloatComplex172 "Xd", // DoubleComplex173 "Xr", // LongDoubleComplex174 "If", // FloatImaginary175 "Id", // DoubleImaginary176 "Ir", // LongDoubleImaginary177 "w", // SignedInt128178 "Uw", // UnsignedInt128179 };180 181 printQualifiers( basicType );182 mangleName << btLetter[ basicType->get_kind() ];183 }184 185 void Mangler::postvisit( PointerType * pointerType ) {186 printQualifiers( pointerType );187 mangleName << "P";188 maybeAccept( pointerType->get_base(), *visitor );189 }190 191 void Mangler::postvisit( ArrayType * arrayType ) {192 // TODO: encode dimension193 printQualifiers( arrayType );194 mangleName << "A0";195 maybeAccept( arrayType->get_base(), *visitor );196 }197 198 void Mangler::postvisit( ReferenceType * refType ) {199 printQualifiers( refType );200 mangleName << "R";201 maybeAccept( refType->get_base(), *visitor );202 }203 204 namespace {205 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) {206 std::list< Type* > ret;207 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ),208 std::mem_fun( &DeclarationWithType::get_type ) );209 return ret;210 }211 }212 213 void Mangler::postvisit( FunctionType * functionType ) {214 printQualifiers( functionType );215 mangleName << "F";216 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() );217 acceptAll( returnTypes, *visitor );218 mangleName << "_";219 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() );220 acceptAll( paramTypes, *visitor );221 184 mangleName << "_"; 222 185 } 223 224 void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { 225 printQualifiers( refType ); 226 227 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 228 229 if ( mangleGenericParams ) { 230 std::list< Expression* >& params = refType->get_parameters(); 231 if ( ! params.empty() ) { 232 mangleName << "_"; 233 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { 234 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 235 assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); 236 maybeAccept( paramType->get_type(), *visitor ); 237 } 238 mangleName << "_"; 239 } 240 } 241 } 242 243 void Mangler::postvisit( StructInstType * aggregateUseType ) { 244 mangleRef( aggregateUseType, "s" ); 245 } 246 247 void Mangler::postvisit( UnionInstType * aggregateUseType ) { 248 mangleRef( aggregateUseType, "u" ); 249 } 250 251 void Mangler::postvisit( EnumInstType * aggregateUseType ) { 252 mangleRef( aggregateUseType, "e" ); 253 } 254 255 void Mangler::postvisit( TypeInstType * typeInst ) { 256 VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); 257 if ( varNum == varNums.end() ) { 258 mangleRef( typeInst, "t" ); 259 } else { 260 printQualifiers( typeInst ); 261 std::ostringstream numStream; 262 numStream << varNum->second.first; 263 switch ( (TypeDecl::Kind )varNum->second.second ) { 264 case TypeDecl::Dtype: 265 mangleName << "d"; 266 break; 267 case TypeDecl::Ftype: 268 mangleName << "f"; 269 break; 270 case TypeDecl::Ttype: 271 mangleName << "tVARGS"; 272 break; 273 default: 274 assert( false ); 275 } // switch 276 mangleName << numStream.str(); 277 } // if 278 } 279 280 void Mangler::postvisit( TupleType * tupleType ) { 281 printQualifiers( tupleType ); 282 mangleName << "T"; 283 acceptAll( tupleType->types, *visitor ); 284 mangleName << "_"; 285 } 286 287 void Mangler::postvisit( VarArgsType * varArgsType ) { 288 printQualifiers( varArgsType ); 289 mangleName << "VARGS"; 290 } 291 292 void Mangler::postvisit( ZeroType * ) { 293 mangleName << "Z"; 294 } 295 296 void Mangler::postvisit( OneType * ) { 297 mangleName << "O"; 298 } 299 300 void Mangler::postvisit( TypeDecl * decl ) { 301 static const char *typePrefix[] = { "BT", "BD", "BF" }; 302 mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name; 303 } 304 305 __attribute__((unused)) void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { 306 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { 307 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; 186 } 187 } 188 189 void Mangler::visit( StructInstType * aggregateUseType ) { 190 mangleRef( aggregateUseType, "s" ); 191 } 192 193 void Mangler::visit( UnionInstType * aggregateUseType ) { 194 mangleRef( aggregateUseType, "u" ); 195 } 196 197 void Mangler::visit( EnumInstType * aggregateUseType ) { 198 mangleRef( aggregateUseType, "e" ); 199 } 200 201 void Mangler::visit( TypeInstType * typeInst ) { 202 VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); 203 if ( varNum == varNums.end() ) { 204 mangleRef( typeInst, "t" ); 205 } else { 206 printQualifiers( typeInst ); 207 std::ostringstream numStream; 208 numStream << varNum->second.first; 209 switch ( (TypeDecl::Kind )varNum->second.second ) { 210 case TypeDecl::Dtype: 211 mangleName << "d"; 212 break; 213 case TypeDecl::Ftype: 214 mangleName << "f"; 215 break; 216 case TypeDecl::Ttype: 217 mangleName << "tVARGS"; 218 break; 219 default: 220 assert( false ); 221 } // switch 222 mangleName << numStream.str(); 223 } // if 224 } 225 226 void Mangler::visit( TupleType * tupleType ) { 227 printQualifiers( tupleType ); 228 mangleName << "T"; 229 acceptAll( tupleType->types, *this ); 230 mangleName << "_"; 231 } 232 233 void Mangler::visit( VarArgsType * varArgsType ) { 234 printQualifiers( varArgsType ); 235 mangleName << "VARGS"; 236 } 237 238 void Mangler::visit( ZeroType * ) { 239 mangleName << "Z"; 240 } 241 242 void Mangler::visit( OneType * ) { 243 mangleName << "O"; 244 } 245 246 void Mangler::visit( TypeDecl * decl ) { 247 static const char *typePrefix[] = { "BT", "BD", "BF" }; 248 mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name; 249 } 250 251 void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { 252 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { 253 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; 254 } // for 255 } 256 257 void Mangler::printQualifiers( Type * type ) { 258 // skip if not including qualifiers 259 if ( typeMode ) return; 260 261 if ( ! type->get_forall().empty() ) { 262 std::list< std::string > assertionNames; 263 int tcount = 0, dcount = 0, fcount = 0, vcount = 0; 264 mangleName << "A"; 265 for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { 266 switch ( (*i)->get_kind() ) { 267 case TypeDecl::Dtype: 268 dcount++; 269 break; 270 case TypeDecl::Ftype: 271 fcount++; 272 break; 273 case TypeDecl::Ttype: 274 vcount++; 275 break; 276 default: 277 assert( false ); 278 } // switch 279 varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); 280 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { 281 Mangler sub_mangler( mangleOverridable, typeMode, mangleGenericParams ); 282 sub_mangler.nextVarNum = nextVarNum; 283 sub_mangler.isTopLevel = false; 284 sub_mangler.varNums = varNums; 285 (*assert)->accept( sub_mangler ); 286 assertionNames.push_back( sub_mangler.mangleName.str() ); 308 287 } // for 309 } 310 311 void Mangler::printQualifiers( Type * type ) { 312 // skip if not including qualifiers 313 if ( typeMode ) return; 314 315 if ( ! type->get_forall().empty() ) { 316 std::list< std::string > assertionNames; 317 int tcount = 0, dcount = 0, fcount = 0, vcount = 0; 318 mangleName << "A"; 319 for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { 320 switch ( (*i)->get_kind() ) { 321 case TypeDecl::Dtype: 322 dcount++; 323 break; 324 case TypeDecl::Ftype: 325 fcount++; 326 break; 327 case TypeDecl::Ttype: 328 vcount++; 329 break; 330 default: 331 assert( false ); 332 } // switch 333 varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); 334 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { 335 PassVisitor<Mangler> sub_mangler( mangleOverridable, typeMode, mangleGenericParams ); 336 sub_mangler.pass.nextVarNum = nextVarNum; 337 sub_mangler.pass.isTopLevel = false; 338 sub_mangler.pass.varNums = varNums; 339 (*assert)->accept( sub_mangler ); 340 assertionNames.push_back( sub_mangler.pass.mangleName.str() ); 341 } // for 342 } // for 343 mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; 344 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 345 mangleName << "_"; 346 } // if 347 if ( type->get_const() ) { 348 mangleName << "C"; 349 } // if 350 if ( type->get_volatile() ) { 351 mangleName << "V"; 352 } // if 353 if ( type->get_mutex() ) { 354 mangleName << "M"; 355 } // if 356 // Removed due to restrict not affecting function compatibility in GCC 357 // if ( type->get_isRestrict() ) { 358 // mangleName << "E"; 359 // } // if 360 if ( type->get_lvalue() ) { 361 // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues 362 mangleName << "L"; 363 } 364 if ( type->get_atomic() ) { 365 mangleName << "A"; 366 } // if 367 } 368 } // namespace 369 } // namespace Mangler 288 } // for 289 mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; 290 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 291 mangleName << "_"; 292 } // if 293 if ( type->get_const() ) { 294 mangleName << "C"; 295 } // if 296 if ( type->get_volatile() ) { 297 mangleName << "V"; 298 } // if 299 if ( type->get_mutex() ) { 300 mangleName << "M"; 301 } // if 302 // Removed due to restrict not affecting function compatibility in GCC 303 // if ( type->get_isRestrict() ) { 304 // mangleName << "E"; 305 // } // if 306 if ( type->get_lvalue() ) { 307 // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues 308 mangleName << "L"; 309 } 310 if ( type->get_atomic() ) { 311 mangleName << "A"; 312 } // if 313 } 370 314 } // namespace SymTab 371 315 -
src/SymTab/Mangler.h
r81e1f32b rd2d50d7 25 25 26 26 namespace SymTab { 27 namespace Mangler { 27 /// Mangles names to a unique C identifier 28 class Mangler : public Visitor { 29 public: 28 30 /// Mangle syntax tree object; primary interface to clients 29 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true ); 31 template< typename SynTreeClass > 32 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true ); 33 /// Mangle a type name; secondary interface 34 static std::string mangleType( Type* ty ); 35 /// Mangle ignoring generic type parameters 36 static std::string mangleConcrete( Type* ty ); 30 37 31 /// Mangle a type name; secondary interface 32 std::string mangleType( Type* ty ); 33 /// Mangle ignoring generic type parameters 34 std::string mangleConcrete( Type* ty ); 35 } // Mangler 38 39 virtual void visit( ObjectDecl *declaration ); 40 virtual void visit( FunctionDecl *declaration ); 41 virtual void visit( TypeDecl *declaration ); 42 43 virtual void visit( VoidType *voidType ); 44 virtual void visit( BasicType *basicType ); 45 virtual void visit( PointerType *pointerType ); 46 virtual void visit( ArrayType *arrayType ); 47 virtual void visit( ReferenceType *refType ); 48 virtual void visit( FunctionType *functionType ); 49 virtual void visit( StructInstType *aggregateUseType ); 50 virtual void visit( UnionInstType *aggregateUseType ); 51 virtual void visit( EnumInstType *aggregateUseType ); 52 virtual void visit( TypeInstType *aggregateUseType ); 53 virtual void visit( TupleType *tupleType ); 54 virtual void visit( VarArgsType *varArgsType ); 55 virtual void visit( ZeroType *zeroType ); 56 virtual void visit( OneType *oneType ); 57 58 std::string get_mangleName() { return mangleName.str(); } 59 private: 60 std::ostringstream mangleName; ///< Mangled name being constructed 61 typedef std::map< std::string, std::pair< int, int > > VarMapType; 62 VarMapType varNums; ///< Map of type variables to indices 63 int nextVarNum; ///< Next type variable index 64 bool isTopLevel; ///< Is the Mangler at the top level 65 bool mangleOverridable; ///< Specially mangle overridable built-in methods 66 bool typeMode; ///< Produce a unique mangled name for a type 67 bool mangleGenericParams; ///< Include generic parameters in name mangling if true 68 69 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); 70 Mangler( const Mangler & ); 71 72 void mangleDecl( DeclarationWithType *declaration ); 73 void mangleRef( ReferenceToType *refType, std::string prefix ); 74 75 void printQualifiers( Type *type ); 76 }; // Mangler 77 78 template< typename SynTreeClass > 79 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) { 80 Mangler mangler( mangleOverridable, typeMode, mangleGenericParams ); 81 maybeAccept( decl, mangler ); 82 return mangler.get_mangleName(); 83 } 36 84 } // SymTab 37 85 -
src/SynTree/CompoundStmt.cc
r81e1f32b rd2d50d7 64 64 } 65 65 if ( ! declMap.empty() ) { 66 VarExprReplacer::replace( this, declMap ); 66 VarExprReplacer replacer( declMap ); 67 accept( replacer ); 67 68 } 68 69 } -
src/SynTree/FunctionDecl.cc
r81e1f32b rd2d50d7 49 49 } 50 50 if ( ! declMap.empty() ) { 51 VarExprReplacer::replace( this, declMap ); 51 VarExprReplacer replacer( declMap ); 52 accept( replacer ); 52 53 } 53 54 } -
src/SynTree/VarExprReplacer.cc
r81e1f32b rd2d50d7 16 16 #include <iostream> // for operator<<, basic_ostream, ostream, basic_o... 17 17 18 #include "Common/PassVisitor.h"19 18 #include "Declaration.h" // for operator<<, DeclarationWithType 20 19 #include "Expression.h" // for VariableExpr 21 20 #include "VarExprReplacer.h" 22 21 23 namespace VarExprReplacer { 24 namespace { 25 /// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping 26 struct VarExprReplacer { 27 private: 28 const DeclMap & declMap; 29 bool debug; 30 public: 31 VarExprReplacer( const DeclMap & declMap, bool debug = false ); 22 VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {} 32 23 33 // replace variable with new node from decl map 34 void previsit( VariableExpr * varExpr ); 35 }; 24 // replace variable with new node from decl map 25 void VarExprReplacer::visit( VariableExpr * varExpr ) { 26 // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are) 27 if ( declMap.count( varExpr->get_var() ) ) { 28 if ( debug ) { 29 std::cerr << "replacing variable reference: " << (void*)varExpr->get_var() << " " << varExpr->get_var() << " with " << (void*)declMap.at( varExpr->get_var() ) << " " << declMap.at( varExpr->get_var() ) << std::endl; 30 } 31 varExpr->set_var( declMap.at( varExpr->get_var() ) ); 36 32 } 37 38 void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) { 39 PassVisitor<VarExprReplacer> replacer( declMap, debug ); 40 maybeAccept( node, replacer ); 41 } 42 43 namespace { 44 VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {} 45 46 // replace variable with new node from decl map 47 void VarExprReplacer::previsit( VariableExpr * varExpr ) { 48 // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are) 49 if ( declMap.count( varExpr->var ) ) { 50 if ( debug ) { 51 std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)declMap.at( varExpr->var ) << " " << declMap.at( varExpr->var ) << std::endl; 52 } 53 varExpr->var = declMap.at( varExpr->var ); 54 } 55 } 56 } 57 } // namespace VarExprReplacer 58 59 60 61 62 63 64 33 } -
src/SynTree/VarExprReplacer.h
r81e1f32b rd2d50d7 23 23 class VariableExpr; 24 24 25 namespace VarExprReplacer { 25 /// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping 26 class VarExprReplacer : public Visitor { 27 public: 26 28 typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap; 29 private: 30 const DeclMap & declMap; 31 bool debug; 32 public: 33 VarExprReplacer( const DeclMap & declMap, bool debug = false ); 27 34 28 void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug = false ); 29 } 35 // replace variable with new node from decl map 36 virtual void visit( VariableExpr * varExpr ); 37 38 static void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug = false ) { 39 VarExprReplacer replacer( declMap, debug ); 40 maybeAccept( node, replacer ); 41 } 42 }; 30 43 31 44 // Local Variables: // -
src/tests/.expect/alloc-ERROR.txt
r81e1f32b rd2d50d7 4 4 Name: p 5 5 Applying untyped: 6 Name: realloc6 Name: alloc 7 7 ...to: 8 8 Name: stp … … 24 24 Name: p 25 25 Applying untyped: 26 Name: alloc27 ...to:28 Name: stp29 Applying untyped:30 Name: ?*?31 ...to:32 Name: dim33 Sizeof Expression on: Applying untyped:34 Name: *?35 ...to:36 Name: stp37 38 39 40 41 alloc.c:266:1 error: No reasonable alternatives for expression Applying untyped:42 Name: ?=?43 ...to:44 Name: p45 Applying untyped:46 26 Name: memset 47 27 ...to: … … 50 30 51 31 52 alloc.c:26 7:1 error: No reasonable alternatives for expression Applying untyped:32 alloc.c:266:1 error: No reasonable alternatives for expression Applying untyped: 53 33 Name: ?=? 54 34 ...to: -
src/tests/.expect/alloc.txt
r81e1f32b rd2d50d7 60 60 pointer arithmetic 0 61 61 CFA deep malloc 0xdeadbeef 62 63 SHOULD FAIL -
src/tests/alloc.c
r81e1f32b rd2d50d7 10 10 // Created On : Wed Feb 3 07:56:22 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 22 21:26:40 201813 // Update Count : 3 2612 // Last Modified On : Fri Nov 24 23:06:42 2017 13 // Update Count : 319 14 14 // 15 15 … … 32 32 // allocation, non-array types 33 33 34 // int & r = malloc();34 // int &r = malloc(); 35 35 // r = 0xdeadbeef; 36 36 // printf( "C malloc %#x\n", r ); 37 37 // free( &r ); 38 38 39 p = (int *) malloc( sizeof(*p) );// C malloc, type unsafe39 p = (int *)(void *)malloc( sizeof(*p) ); // C malloc, type unsafe 40 40 *p = 0xdeadbeef; 41 41 printf( "C malloc %#x\n", *p ); … … 88 88 printf( "\n" ); 89 89 90 p = (int *) realloc( p, dim * sizeof(*p) );// C realloc90 p = (int *)(void *)realloc( p, dim * sizeof(*p) ); // C realloc 91 91 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 92 92 printf( "C realloc\n" ); … … 259 259 free( p ); 260 260 261 #ifdef ERR1262 261 stp = malloc(); 263 262 printf( "\nSHOULD FAIL\n" ); 264 p = realloc( stp, dim * sizeof( *stp ) ); 265 p = alloc( stp, dim * sizeof( *stp) );263 #ifdef ERR1 264 p = alloc( stp, dim * sizeof(*stp) ); 266 265 p = memset( stp, 10 ); 267 266 p = memcpy( &st1, &st ); -
src/tests/vector.c
r81e1f32b rd2d50d7 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // vector.c --7 // libcfa_vector.c -- 8 8 // 9 9 // Author : Thierry Delisle 10 10 // Created On : Mon Jul 4 23:36:19 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Jan 18 17:08:08 201813 // Update Count : 2 712 // Last Modified On : Tue Jul 5 15:08:05 2016 13 // Update Count : 26 14 14 // 15 15 … … 63 63 // Local Variables: // 64 64 // tab-width: 4 // 65 // compile-command: "cfa vector.c" //65 // compile-command: "cfa libcfa_vector.c" // 66 66 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.