Changes in / [891c3e3:4bf3b2b]


Ignore:
Location:
src
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/GenType.cc

    r891c3e3 r4bf3b2b  
    2626
    2727namespace CodeGen {
    28         struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
     28        class GenType : public Visitor {
     29          public:
    2930                GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false );
    3031                std::string get_typeString() const { return typeString; }
    3132                void set_typeString( const std::string &newValue ) { typeString = newValue; }
    3233
    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 );
    5048
    5149          private:
     
    6159
    6260        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 );
    6462                std::ostringstream os;
    6563
     
    7068
    7169                type->accept( gt );
    72                 return os.str() + gt.pass.get_typeString();
     70                return os.str() + gt.get_typeString();
    7371        }
    7472
     
    7977        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    8078
    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 ) {
    9580                typeString = "void " + typeString;
    9681                handleQualifiers( voidType );
    9782        }
    9883
    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();
    10186                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    10287                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
     
    10489        }
    10590
    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 ) {
    10792                std::ostringstream os;
    10893                if ( typeString != "" ) {
     
    141126                typeString = os.str();
    142127
    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() );
    150135                } else {
    151136                        handleQualifiers( pointerType );
     
    155140                                typeString = "*" + typeString;
    156141                        } // 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);
    167152                assertf( ! genC, "Reference types should not reach code generation." );
    168153                handleQualifiers( refType );
    169154                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 ) {
    174159                std::ostringstream os;
    175160
     
    184169                /************* parameters ***************/
    185170
    186                 const std::list<DeclarationWithType *> &pars = funcType->parameters;
     171                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
    187172
    188173                if ( pars.empty() ) {
     
    206191                typeString = os.str();
    207192
    208                 if ( funcType->returnVals.size() == 0 ) {
     193                if ( funcType->get_returnVals().size() == 0 ) {
    209194                        typeString = "void " + typeString;
    210195                } else {
    211                         funcType->returnVals.front()->get_type()->accept( *visitor );
     196                        funcType->get_returnVals().front()->get_type()->accept( *this );
    212197                } // if
    213198
    214199                // add forall
    215                 if( ! funcType->forall.empty() && ! genC ) {
     200                if( ! funcType->get_forall().empty() && ! genC ) {
    216201                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
    217202                        std::ostringstream os;
    218203                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    219204                        os << "forall(";
    220                         cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
     205                        cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
    221206                        os << ")" << std::endl;
    222207                        typeString = os.str() + typeString;
     
    236221        }
    237222
    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;
    240225                if ( genC ) typeString = "struct " + typeString;
    241226                handleQualifiers( structInst );
    242227        }
    243228
    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;
    246231                if ( genC ) typeString = "union " + typeString;
    247232                handleQualifiers( unionInst );
    248233        }
    249234
    250         void GenType::postvisit( EnumInstType * enumInst ) {
    251                 typeString = enumInst->name + " " + typeString;
     235        void GenType::visit( EnumInstType *enumInst ) {
     236                typeString = enumInst->get_name() + " " + typeString;
    252237                if ( genC ) typeString = "enum " + typeString;
    253238                handleQualifiers( enumInst );
    254239        }
    255240
    256         void GenType::postvisit( TypeInstType * typeInst ) {
    257                 typeString = typeInst->name + " " + typeString;
     241        void GenType::visit( TypeInstType *typeInst ) {
     242                typeString = typeInst->get_name() + " " + typeString;
    258243                handleQualifiers( typeInst );
    259244        }
    260245
    261         void GenType::postvisit( TupleType * tupleType ) {
     246        void GenType::visit( TupleType * tupleType ) {
    262247                assertf( ! genC, "Tuple types should not reach code generation." );
    263248                unsigned int i = 0;
     
    272257        }
    273258
    274         void GenType::postvisit( VarArgsType * varArgsType ) {
     259        void GenType::visit( VarArgsType *varArgsType ) {
    275260                typeString = "__builtin_va_list " + typeString;
    276261                handleQualifiers( varArgsType );
    277262        }
    278263
    279         void GenType::postvisit( ZeroType * zeroType ) {
     264        void GenType::visit( ZeroType *zeroType ) {
    280265                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    281266                typeString = (pretty ? "zero_t " : "long int ") + typeString;
     
    283268        }
    284269
    285         void GenType::postvisit( OneType * oneType ) {
     270        void GenType::visit( OneType *oneType ) {
    286271                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    287272                typeString = (pretty ? "one_t " : "long int ") + typeString;
     
    289274        }
    290275
    291         void GenType::handleQualifiers( Type * type ) {
     276        void GenType::handleQualifiers( Type *type ) {
    292277                if ( type->get_const() ) {
    293278                        typeString = "const " + typeString;
  • src/ControlStruct/ExceptTranslate.cc

    r891c3e3 r4bf3b2b  
    316316                                VarExprReplacer::DeclMap mapping;
    317317                                mapping[ handler_decl ] = local_except;
    318                                 VarExprReplacer::replace( handler->body, mapping );
     318                                VarExprReplacer mapper( mapping );
     319                                handler->get_body()->accept( mapper );
    319320                        }
    320321
  • src/ResolvExpr/AlternativeFinder.cc

    r891c3e3 r4bf3b2b  
    152152
    153153                void renameTypes( Expression *expr ) {
    154                         renameTyVars( expr->result );
     154                        expr->get_result()->accept( global_renamer );
    155155                }
    156156        } // namespace
     
    485485                        Type *adjType = candidate->get_type()->clone();
    486486                        adjustExprType( adjType, newEnv, indexer );
    487                         renameTyVars( adjType );
     487                        adjType->accept( global_renamer );
    488488                        PRINT(
    489489                                std::cerr << "unifying ";
     
    595595
    596596                ArgPack()
    597                         : 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), 
    598598                          tupleStart(0), nextExpl(0), explAlt(0) {}
    599599
     
    706706
    707707                                                if ( nTuples > 0 || ! results[i].expr ) {
    708                                                         // first iteration or no expression to clone,
     708                                                        // first iteration or no expression to clone, 
    709709                                                        // push empty tuple expression
    710710                                                        newResult.parent = i;
  • src/ResolvExpr/CommonType.cc

    r891c3e3 r4bf3b2b  
    1818#include <utility>                       // for pair
    1919
    20 #include "Common/PassVisitor.h"
    2120#include "ResolvExpr/TypeEnvironment.h"  // for OpenVarSet, AssertionSet
    2221#include "SymTab/Indexer.h"              // for Indexer
     
    3029
    3130namespace ResolvExpr {
    32         struct CommonType : public WithShortCircuiting {
     31        class CommonType : public Visitor {
     32          public:
    3333                CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
    3434                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 
    5435          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
    5552                template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer );
    5653                template< typename RefType > void handleRefType( RefType *inst, Type *other );
     
    8380
    8481        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 );
    8683
    8784                int depth1 = type1->referenceDepth();
     
    119116
    120117                type1->accept( visitor );
    121                 Type *result = visitor.pass.get_result();
     118                Type *result = visitor.get_result();
    122119                if ( ! result ) {
    123120                        // this appears to be handling for opaque type declarations
     
    191188        }
    192189
    193         void CommonType::postvisit( VoidType * ) {}
    194 
    195         void CommonType::postvisit( BasicType *basicType ) {
     190        void CommonType::visit( VoidType * ) {}
     191
     192        void CommonType::visit( BasicType *basicType ) {
    196193                if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
    197194                        BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ];
     
    222219        }
    223220
    224         void CommonType::postvisit( PointerType *pointerType ) {
     221        void CommonType::visit( PointerType *pointerType ) {
    225222                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
    226223                        // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl;
     
    257254        }
    258255
    259         void CommonType::postvisit( ArrayType * ) {}
    260 
    261         void CommonType::postvisit( ReferenceType *refType ) {
     256        void CommonType::visit( ArrayType * ) {}
     257
     258        void CommonType::visit( ReferenceType *refType ) {
    262259                if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
    263260                        // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl;
     
    294291        }
    295292
    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 ) {
    301298                if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
    302299                        // 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 ) {
    311310                if ( widenFirst ) {
    312311                        NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
     
    330329        }
    331330
    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 ) {
    336335                if ( widenFirst ) {
    337336                        if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
     
    347346        }
    348347
    349         void CommonType::postvisit( OneType *oneType ) {
     348        void CommonType::visit( OneType *oneType ) {
    350349                if ( widenFirst ) {
    351350                        if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
  • src/ResolvExpr/CurrentObject.cc

    r891c3e3 r4bf3b2b  
    443443                                return new UnionIterator( uit );
    444444                        } 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" );
    446446                                return new SimpleIterator( type );
    447447                        }
  • src/ResolvExpr/PtrsAssignable.cc

    r891c3e3 r4bf3b2b  
    1414//
    1515
    16 #include "Common/PassVisitor.h"
    1716#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
    1817#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
     
    2120
    2221namespace ResolvExpr {
    23         struct PtrsAssignable : public WithShortCircuiting {
     22        class PtrsAssignable : public Visitor {
     23          public:
    2424                PtrsAssignable( Type *dest, const TypeEnvironment &env );
    2525
    2626                int get_result() const { return result; }
    2727
    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 );
    4442          private:
    4543                Type *dest;
     
    6159                        return -1;
    6260                } else {
    63                         PassVisitor<PtrsAssignable> ptrs( dest, env );
     61                        PtrsAssignable ptrs( dest, env );
    6462                        src->accept( ptrs );
    65                         return ptrs.pass.get_result();
     63                        return ptrs.get_result();
    6664                } // if
    6765        }
     
    6967        PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}
    7068
    71         void PtrsAssignable::postvisit( VoidType * ) {
     69        void PtrsAssignable::visit( VoidType * ) {
    7270                // T * = void * is disallowed - this is a change from C, where any
    7371                // void * can be assigned or passed to a non-void pointer without a cast.
    7472        }
    7573
    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 ) {}
    8078
    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 ) {}
    8381
    84         void PtrsAssignable::postvisit( EnumInstType * ) {
     82        void PtrsAssignable::visit( EnumInstType * ) {
    8583                if ( dynamic_cast< BasicType* >( dest ) ) {
    8684                        // int * = E *, etc. is safe. This isn't technically correct, as each
     
    9391        }
    9492
    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 ) {
    9795                EqvClass eqvClass;
    9896                if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) {
     
    102100        }
    103101
    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 ) {}
    108106
    109107} // namespace ResolvExpr
  • src/ResolvExpr/PtrsCastable.cc

    r891c3e3 r4bf3b2b  
    1414//
    1515
    16 #include "Common/PassVisitor.h"
    1716#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
    1817#include "SymTab/Indexer.h"              // for Indexer
     
    2221#include "typeops.h"                     // for ptrsAssignable
    2322
     23
    2424namespace ResolvExpr {
    25         struct PtrsCastable : public WithShortCircuiting {
     25        class PtrsCastable : public Visitor {
    2626          public:
    2727                PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
     
    2929                int get_result() const { return result; }
    3030
    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);
    4745          private:
    4846                Type *dest;
     
    8179                        EqvClass eqvClass;
    8280                        if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
    83                                 // xxx - should this be ptrsCastable?
    8481                                return ptrsAssignable( src, eqvClass.type, env );
    8582                        } // if
     
    8885                        return objectCast( src, env, indexer );
    8986                } else {
    90                         PassVisitor<PtrsCastable> ptrs( dest, env, indexer );
     87                        PtrsCastable ptrs( dest, env, indexer );
    9188                        src->accept( ptrs );
    92                         return ptrs.pass.get_result();
     89                        return ptrs.get_result();
    9390                } // if
    9491        }
     
    9895        }
    9996
    100         void PtrsCastable::postvisit( VoidType * ) {
     97        void PtrsCastable::visit( VoidType * ) {
    10198                result = objectCast( dest, env, indexer );
    10299        }
    103100
    104         void PtrsCastable::postvisit( BasicType * ) {
     101        void PtrsCastable::visit( BasicType * ) {
    105102                result = objectCast( dest, env, indexer );
    106103        }
    107104
    108         void PtrsCastable::postvisit( PointerType * ) {
     105        void PtrsCastable::visit( PointerType * ) {
    109106                result = objectCast( dest, env, indexer );
    110107        }
    111108
    112         void PtrsCastable::postvisit( ArrayType * ) {
     109        void PtrsCastable::visit( ArrayType * ) {
    113110                result = objectCast( dest, env, indexer );
    114111        }
    115112
    116         void PtrsCastable::postvisit( FunctionType * ) {
     113        void PtrsCastable::visit( FunctionType * ) {
    117114                // result = -1;
    118115                result = functionCast( dest, env, indexer );
    119116        }
    120117
    121         void PtrsCastable::postvisit( StructInstType * ) {
     118        void PtrsCastable::visit( StructInstType * ) {
    122119                result = objectCast( dest, env, indexer );
    123120        }
    124121
    125         void PtrsCastable::postvisit( UnionInstType * ) {
     122        void PtrsCastable::visit( UnionInstType * ) {
    126123                result = objectCast( dest, env, indexer );
    127124        }
    128125
    129         void PtrsCastable::postvisit( EnumInstType * ) {
     126        void PtrsCastable::visit( EnumInstType * ) {
    130127                if ( dynamic_cast< EnumInstType* >( dest ) ) {
    131128                        result = 1;
     
    141138        }
    142139
    143         void PtrsCastable::postvisit( TraitInstType * ) {}
     140        void PtrsCastable::visit( TraitInstType * ) {}
    144141
    145         void PtrsCastable::postvisit(TypeInstType *inst) {
     142        void PtrsCastable::visit(TypeInstType *inst) {
    146143                //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
    147144                result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
    148145        }
    149146
    150         void PtrsCastable::postvisit( TupleType * ) {
     147        void PtrsCastable::visit( TupleType * ) {
    151148                result = objectCast( dest, env, indexer );
    152149        }
    153150
    154         void PtrsCastable::postvisit( VarArgsType * ) {
     151        void PtrsCastable::visit( VarArgsType * ) {
    155152                result = objectCast( dest, env, indexer );
    156153        }
    157154
    158         void PtrsCastable::postvisit( ZeroType * ) {
     155        void PtrsCastable::visit( ZeroType * ) {
    159156                result = objectCast( dest, env, indexer );
    160157        }
    161158
    162         void PtrsCastable::postvisit( OneType * ) {
     159        void PtrsCastable::visit( OneType * ) {
    163160                result = objectCast( dest, env, indexer );
    164161        }
  • src/ResolvExpr/RenameVars.cc

    r891c3e3 r4bf3b2b  
    1919#include <utility>                 // for pair
    2020
    21 #include "Common/PassVisitor.h"
    2221#include "Common/SemanticError.h"  // for SemanticError
    2322#include "RenameVars.h"
     
    2827
    2928namespace ResolvExpr {
    30         namespace {
    31                 struct RenameVars {
    32                         RenameVars();
    33                         void reset();
     29        RenameVars global_renamer;
    3430
    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 >() );
    4933        }
    5034
    51         void resetTyVarRenaming() {
    52                 global_renamer.pass.reset();
     35        void RenameVars::reset() {
     36                level = 0;
     37                resetCount++;
    5338        }
    5439
    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        }
    5944
    60                 void RenameVars::reset() {
    61                         level = 0;
    62                         resetCount++;
    63                 }
     45        void RenameVars::visit( BasicType *basicType ) {
     46                typeBefore( basicType );
     47                typeAfter( basicType );
     48        }
    6449
    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        }
    7255
    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        }
    9062
    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
    98151} // namespace ResolvExpr
    99152
  • src/ResolvExpr/RenameVars.h

    r891c3e3 r4bf3b2b  
    2424
    2525namespace ResolvExpr {
     26
    2627        /// 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 );
    2847
    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;
    3155} // namespace ResolvExpr
    3256
  • src/ResolvExpr/Resolver.cc

    r891c3e3 r4bf3b2b  
    132132
    133133        void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) {
    134                 resetTyVarRenaming();
     134                global_renamer.reset();
    135135                TypeEnvironment env;
    136136                Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
  • src/ResolvExpr/Unify.cc

    r891c3e3 r4bf3b2b  
    4444namespace ResolvExpr {
    4545
    46         struct Unify : public WithShortCircuiting {
     46        class Unify : public Visitor {
     47          public:
    4748                Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
    4849
    4950                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 
    6951          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
    7068                template< typename RefType > void handleRefType( RefType *inst, Type *other );
    7169                template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
     
    327325                        result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
    328326                } else {
    329                         PassVisitor<Unify> comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
     327                        Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
    330328                        type1->accept( comparator );
    331                         result = comparator.pass.get_result();
     329                        result = comparator.get_result();
    332330                } // if
    333331#ifdef DEBUG
     
    406404        }
    407405
    408         void Unify::postvisit( __attribute__((unused)) VoidType *voidType) {
     406        void Unify::visit( __attribute__((unused)) VoidType *voidType) {
    409407                result = dynamic_cast< VoidType* >( type2 );
    410408        }
    411409
    412         void Unify::postvisit(BasicType *basicType) {
     410        void Unify::visit(BasicType *basicType) {
    413411                if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
    414412                        result = basicType->get_kind() == otherBasic->get_kind();
     
    438436        }
    439437
    440         void Unify::postvisit(PointerType *pointerType) {
     438        void Unify::visit(PointerType *pointerType) {
    441439                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
    442440                        result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
     
    446444        }
    447445
    448         void Unify::postvisit(ReferenceType *refType) {
     446        void Unify::visit(ReferenceType *refType) {
    449447                if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
    450448                        result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
     
    454452        }
    455453
    456         void Unify::postvisit(ArrayType *arrayType) {
     454        void Unify::visit(ArrayType *arrayType) {
    457455                ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
    458456                // to unify, array types must both be VLA or both not VLA
     
    569567        }
    570568
    571         void Unify::postvisit(FunctionType *functionType) {
     569        void Unify::visit(FunctionType *functionType) {
    572570                FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
    573571                if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
     
    671669        }
    672670
    673         void Unify::postvisit(StructInstType *structInst) {
     671        void Unify::visit(StructInstType *structInst) {
    674672                handleGenericRefType( structInst, type2 );
    675673        }
    676674
    677         void Unify::postvisit(UnionInstType *unionInst) {
     675        void Unify::visit(UnionInstType *unionInst) {
    678676                handleGenericRefType( unionInst, type2 );
    679677        }
    680678
    681         void Unify::postvisit(EnumInstType *enumInst) {
     679        void Unify::visit(EnumInstType *enumInst) {
    682680                handleRefType( enumInst, type2 );
    683681        }
    684682
    685         void Unify::postvisit(TraitInstType *contextInst) {
     683        void Unify::visit(TraitInstType *contextInst) {
    686684                handleRefType( contextInst, type2 );
    687685        }
    688686
    689         void Unify::postvisit(TypeInstType *typeInst) {
     687        void Unify::visit(TypeInstType *typeInst) {
    690688                assert( openVars.find( typeInst->get_name() ) == openVars.end() );
    691689                TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
     
    742740        }
    743741
    744         void Unify::postvisit(TupleType *tupleType) {
     742        void Unify::visit(TupleType *tupleType) {
    745743                if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
    746744                        std::unique_ptr<TupleType> flat1( tupleType->clone() );
     
    759757        }
    760758
    761         void Unify::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {
     759        void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
    762760                result = dynamic_cast< VarArgsType* >( type2 );
    763761        }
    764762
    765         void Unify::postvisit( __attribute__((unused)) ZeroType *zeroType ) {
     763        void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
    766764                result = dynamic_cast< ZeroType* >( type2 );
    767765        }
    768766
    769         void Unify::postvisit( __attribute__((unused)) OneType *oneType ) {
     767        void Unify::visit( __attribute__((unused)) OneType *oneType ) {
    770768                result = dynamic_cast< OneType* >( type2 );
    771769        }
  • src/SymTab/Mangler.cc

    r891c3e3 r4bf3b2b  
    2323
    2424#include "CodeGen/OperatorTable.h"  // for OperatorInfo, operatorLookup
    25 #include "Common/PassVisitor.h"
    2625#include "Common/SemanticError.h"   // for SemanticError
    2726#include "Common/utility.h"         // for toString
     
    3231
    3332namespace SymTab {
    34         namespace Mangler {
    35                 namespace {
    36                         /// Mangles names to a unique C identifier
    37                         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 constructed
    65                                 typedef std::map< std::string, std::pair< int, int > > VarMapType;
    66                                 VarMapType varNums;             ///< Map of type variables to indices
    67                                 int nextVarNum;                 ///< Next type variable index
    68                                 bool isTopLevel;                ///< Is the Mangler at the top level
    69                                 bool mangleOverridable;         ///< Specially mangle overridable built-in methods
    70                                 bool typeMode;                  ///< Produce a unique mangled name for a type
    71                                 bool mangleGenericParams;       ///< Include generic parameters in name mangling if true
    72 
    73                                 void mangleDecl( DeclarationWithType *declaration );
    74                                 void mangleRef( ReferenceToType *refType, std::string prefix );
    75 
    76                                 void printQualifiers( Type *type );
    77                         }; // Mangler
    78                 } // namespace
    79 
    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
    8483                }
    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;
    90156                }
    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 );
    137183                                }
    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",    // Bool
    157                                         "c",    // Char
    158                                         "Sc",   // SignedChar
    159                                         "Uc",   // UnsignedChar
    160                                         "s",    // ShortSignedInt
    161                                         "Us",   // ShortUnsignedInt
    162                                         "i",    // SignedInt
    163                                         "Ui",   // UnsignedInt
    164                                         "l",    // LongSignedInt
    165                                         "Ul",   // LongUnsignedInt
    166                                         "q",    // LongLongSignedInt
    167                                         "Uq",   // LongLongUnsignedInt
    168                                         "f",    // Float
    169                                         "d",    // Double
    170                                         "r",    // LongDouble
    171                                         "Xf",   // FloatComplex
    172                                         "Xd",   // DoubleComplex
    173                                         "Xr",   // LongDoubleComplex
    174                                         "If",   // FloatImaginary
    175                                         "Id",   // DoubleImaginary
    176                                         "Ir",   // LongDoubleImaginary
    177                                         "w",    // SignedInt128
    178                                         "Uw",   // UnsignedInt128
    179                                 };
    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 dimension
    193                                 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 );
    221184                                mangleName << "_";
    222185                        }
    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() );
    308287                                } // 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        }
    370314} // namespace SymTab
    371315
  • src/SymTab/Mangler.h

    r891c3e3 r4bf3b2b  
    2525
    2626namespace SymTab {
    27         namespace Mangler {
     27        /// Mangles names to a unique C identifier
     28        class Mangler : public Visitor {
     29          public:
    2830                /// 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 );
    3037
    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        }
    3684} // SymTab
    3785
  • src/SynTree/CompoundStmt.cc

    r891c3e3 r4bf3b2b  
    6464        }
    6565        if ( ! declMap.empty() ) {
    66                 VarExprReplacer::replace( this, declMap );
     66                VarExprReplacer replacer( declMap );
     67                accept( replacer );
    6768        }
    6869}
  • src/SynTree/FunctionDecl.cc

    r891c3e3 r4bf3b2b  
    4949        }
    5050        if ( ! declMap.empty() ) {
    51                 VarExprReplacer::replace( this, declMap );
     51                VarExprReplacer replacer( declMap );
     52                accept( replacer );
    5253        }
    5354}
  • src/SynTree/VarExprReplacer.cc

    r891c3e3 r4bf3b2b  
    1616#include <iostream>       // for operator<<, basic_ostream, ostream, basic_o...
    1717
    18 #include "Common/PassVisitor.h"
    1918#include "Declaration.h"  // for operator<<, DeclarationWithType
    2019#include "Expression.h"   // for VariableExpr
    2120#include "VarExprReplacer.h"
    2221
    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 );
     22VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {}
    3223
    33                         // replace variable with new node from decl map
    34                         void previsit( VariableExpr * varExpr );
    35                 };
     24// replace variable with new node from decl map
     25void 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() ) );
    3632        }
    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

    r891c3e3 r4bf3b2b  
    2323class VariableExpr;
    2424
    25 namespace VarExprReplacer {
     25/// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping
     26class VarExprReplacer : public Visitor {
     27public:
    2628        typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap;
     29private:
     30        const DeclMap & declMap;
     31        bool debug;
     32public:
     33        VarExprReplacer( const DeclMap & declMap, bool debug = false );
    2734
    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};
    3043
    3144// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.