Changeset 7416d46a for src/SynTree


Ignore:
Timestamp:
Jan 30, 2018, 3:54:32 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, stuck-waitfor-destruct, with_gc
Children:
633a642
Parents:
f792cb8 (diff), 42be3c3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/SynTree
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/AggregateDecl.cc

    rf792cb8 r7416d46a  
    9292                } else if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    9393                        return constExpr->intValue();
     94                // can be -1, +1, etc.
     95                // } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) {
     96                //      if ( untypedExpr-> )
    9497                } else {
    9598                        assertf( false, "Unhandled expression type in getConstValue for enumerators: %s", toString( expr ).c_str() );
  • src/SynTree/CompoundStmt.cc

    rf792cb8 r7416d46a  
    6464        }
    6565        if ( ! declMap.empty() ) {
    66                 VarExprReplacer replacer( declMap );
    67                 accept( replacer );
     66                VarExprReplacer::replace( this, declMap );
    6867        }
    6968}
  • src/SynTree/Expression.cc

    rf792cb8 r7416d46a  
    108108        //      assert( inst->baseEnum );
    109109        //      EnumDecl * decl = inst->baseEnum;
    110         //      for ( Declaration * member : decl->members ) {
    111         //              if ( member == _var ) {
    112         //                      type->set_lvalue( false );
    113         //              }
     110        //      long long int value;
     111        //      if ( decl->valueOf( var, value ) ) {
     112        //              type->set_lvalue( false );
    114113        //      }
    115114        // }
     
    416415                } else {
    417416                        // references have been removed, in which case dereference returns an lvalue of the base type.
    418                         ret->get_result()->set_lvalue( true );
     417                        ret->result->set_lvalue( true );
    419418                }
    420419        }
  • src/SynTree/FunctionDecl.cc

    rf792cb8 r7416d46a  
    4949        }
    5050        if ( ! declMap.empty() ) {
    51                 VarExprReplacer replacer( declMap );
    52                 accept( replacer );
     51                VarExprReplacer::replace( this, declMap );
    5352        }
    5453}
  • src/SynTree/Label.h

    rf792cb8 r7416d46a  
    3333        std::list< Attribute * >& get_attributes() { return attributes; }
    3434
    35         operator std::string() { return name; }
     35        operator std::string() const { return name; }
    3636        bool empty() { return name.empty(); }
    3737  private:
  • src/SynTree/TypeSubstitution.cc

    rf792cb8 r7416d46a  
    107107
    108108void TypeSubstitution::normalize() {
     109        PassVisitor<Substituter> sub( *this, true );
    109110        do {
    110                 subCount = 0;
    111                 freeOnly = true;
     111                sub.pass.subCount = 0;
     112                sub.pass.freeOnly = true;
    112113                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
    113                         i->second = i->second->acceptMutator( *this );
     114                        i->second = i->second->acceptMutator( sub );
    114115                }
    115         } while ( subCount );
    116 }
    117 
    118 Type * TypeSubstitution::mutate( TypeInstType *inst ) {
    119         BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() );
     116        } while ( sub.pass.subCount );
     117}
     118
     119Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) {
     120        BoundVarsType::const_iterator bound = boundVars.find( inst->name );
    120121        if ( bound != boundVars.end() ) return inst;
    121122
    122         TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );
    123         if ( i == typeEnv.end() ) {
     123        TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() );
     124        if ( i == sub.typeEnv.end() ) {
    124125                return inst;
    125126        } else {
    126 ///         std::cout << "found " << inst->get_name() << ", replacing with ";
    127 ///         i->second->print( std::cout );
    128 ///         std::cout << std::endl;
     127///         std::cerr << "found " << inst->get_name() << ", replacing with ";
     128///         i->second->print( std::cerr );
     129///         std::cerr << std::endl;
    129130                subCount++;
    130                 Type *newtype = i->second->clone();
     131                Type * newtype = i->second->clone();
    131132                newtype->get_qualifiers() |= inst->get_qualifiers();
    132133                delete inst;
     
    135136}
    136137
    137 Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) {
    138         VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() );
    139         if ( i == varEnv.end() ) {
     138Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) {
     139        VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
     140        if ( i == sub.varEnv.end() ) {
    140141                return nameExpr;
    141142        } else {
     
    146147}
    147148
    148 template< typename TypeClass >
    149 Type *TypeSubstitution::handleType( TypeClass *type ) {
    150         ValueGuard<BoundVarsType> oldBoundVars( boundVars );
     149void TypeSubstitution::Substituter::premutate( Type * type ) {
     150        GuardValue( boundVars );
    151151        // bind type variables from forall-qualifiers
    152152        if ( freeOnly ) {
    153                 for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
    154                         boundVars.insert( (*tyvar )->get_name() );
     153                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
     154                        boundVars.insert( (*tyvar)->name );
    155155                } // for
    156156        } // if
    157         Type *ret = Mutator::mutate( type );
    158         return ret;
    159157}
    160158
    161159template< typename TypeClass >
    162 Type *TypeSubstitution::handleAggregateType( TypeClass *type ) {
    163         ValueGuard<BoundVarsType> oldBoundVars( boundVars );
     160void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) {
     161        GuardValue( boundVars );
    164162        // bind type variables from forall-qualifiers
    165163        if ( freeOnly ) {
    166                 for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
    167                         boundVars.insert( (*tyvar )->get_name() );
     164                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
     165                        boundVars.insert( (*tyvar)->name );
    168166                } // for
    169167                // bind type variables from generic type instantiations
    170168                std::list< TypeDecl* > *baseParameters = type->get_baseParameters();
    171                 if ( baseParameters && ! type->get_parameters().empty() ) {
     169                if ( baseParameters && ! type->parameters.empty() ) {
    172170                        for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) {
    173                                 boundVars.insert( (*tyvar)->get_name() );
     171                                boundVars.insert( (*tyvar)->name );
    174172                        } // for
    175173                } // if
    176174        } // if
    177         Type *ret = Mutator::mutate( type );
    178         return ret;
    179 }
    180 
    181 Type * TypeSubstitution::mutate( VoidType *voidType ) {
    182         return handleType( voidType );
    183 }
    184 
    185 Type * TypeSubstitution::mutate( BasicType *basicType ) {
    186         return handleType( basicType );
    187 }
    188 
    189 Type * TypeSubstitution::mutate( PointerType *pointerType ) {
    190         return handleType( pointerType );
    191 }
    192 
    193 Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
    194         return handleType( arrayType );
    195 }
    196 
    197 Type * TypeSubstitution::mutate( FunctionType *functionType ) {
    198         return handleType( functionType );
    199 }
    200 
    201 Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
    202         return handleAggregateType( aggregateUseType );
    203 }
    204 
    205 Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
    206         return handleAggregateType( aggregateUseType );
    207 }
    208 
    209 Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
    210         return handleType( aggregateUseType );
    211 }
    212 
    213 Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
    214         return handleType( aggregateUseType );
    215 }
    216 
    217 Type * TypeSubstitution::mutate( TupleType *tupleType ) {
    218         return handleType( tupleType );
    219 }
    220 
    221 Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
    222         return handleType( varArgsType );
    223 }
    224 
    225 Type * TypeSubstitution::mutate( ZeroType *zeroType ) {
    226         return handleType( zeroType );
    227 }
    228 
    229 Type * TypeSubstitution::mutate( OneType *oneType ) {
    230         return handleType( oneType );
     175}
     176
     177void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) {
     178        handleAggregateType( aggregateUseType );
     179}
     180
     181void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) {
     182        handleAggregateType( aggregateUseType );
    231183}
    232184
  • src/SynTree/TypeSubstitution.h

    rf792cb8 r7416d46a  
    2727#include "SynTree/Declaration.h"   // for TypeDecl, Declaration (ptr only)
    2828#include "SynTree/Expression.h"    // for Expression (ptr only), NameExpr (p...
    29 #include "SynTree/Mutator.h"       // for Mutator
    3029#include "SynTree/Type.h"          // for Type, ArrayType (ptr only), BasicT...
    3130
    32 class TypeSubstitution : public Mutator {
    33         typedef Mutator Parent;
     31class TypeSubstitution {
    3432  public:
    3533        TypeSubstitution();
     
    6462        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
    6563  private:
    66         virtual Type* mutate(TypeInstType *aggregateUseType);
    67         virtual Expression* mutate(NameExpr *nameExpr);
    6864
    69         /// Records type variable bindings from forall-statements
    70         template< typename TypeClass > Type *handleType( TypeClass *type );
    71         /// Records type variable bindings from forall-statements and instantiations of generic types
    72         template< typename TypeClass > Type *handleAggregateType( TypeClass *type );
    73 
    74         virtual Type* mutate(VoidType *basicType);
    75         virtual Type* mutate(BasicType *basicType);
    76         virtual Type* mutate(PointerType *pointerType);
    77         virtual Type* mutate(ArrayType *arrayType);
    78         virtual Type* mutate(FunctionType *functionType);
    79         virtual Type* mutate(StructInstType *aggregateUseType);
    80         virtual Type* mutate(UnionInstType *aggregateUseType);
    81         virtual Type* mutate(EnumInstType *aggregateUseType);
    82         virtual Type* mutate(TraitInstType *aggregateUseType);
    83         virtual Type* mutate(TupleType *tupleType);
    84         virtual Type* mutate(VarArgsType *varArgsType);
    85         virtual Type* mutate(ZeroType *zeroType);
    86         virtual Type* mutate(OneType *oneType);
     65        // Mutator that performs the substitution
     66        struct Substituter;
    8767
    8868        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
     
    9777        typedef std::map< std::string, Type* > TypeEnvType;
    9878        typedef std::map< std::string, Expression* > VarEnvType;
    99         typedef std::set< std::string > BoundVarsType;
    10079        TypeEnvType typeEnv;
    10180        VarEnvType varEnv;
    102         BoundVarsType boundVars;
    103         int subCount;
    104         bool freeOnly;
    10581};
    10682
     
    134110
    135111template< typename FormalIterator, typename ActualIterator >
    136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
    137 {
     112TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
    138113        add( formalBegin, formalEnd, actualBegin );
    139114}
     115
     116// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
     117// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
     118#include "Common/PassVisitor.h"
     119
     120// definitition must happen after PassVisitor is included so that WithGuards can be used
     121struct TypeSubstitution::Substituter : public WithGuards {
     122                Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
     123
     124                Type * postmutate( TypeInstType * aggregateUseType );
     125                Expression * postmutate( NameExpr * nameExpr );
     126
     127                /// Records type variable bindings from forall-statements
     128                void premutate( Type * type );
     129                /// Records type variable bindings from forall-statements and instantiations of generic types
     130                template< typename TypeClass > void handleAggregateType( TypeClass * type );
     131
     132                void premutate( StructInstType * aggregateUseType );
     133                void premutate( UnionInstType * aggregateUseType );
     134
     135                TypeSubstitution & sub;
     136                int subCount = 0;
     137                bool freeOnly;
     138                typedef std::set< std::string > BoundVarsType;
     139                BoundVarsType boundVars;
     140};
    140141
    141142template< typename SynTreeClass >
    142143int TypeSubstitution::apply( SynTreeClass *&input ) {
    143144        assert( input );
    144         subCount = 0;
    145         freeOnly = false;
    146         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
     145        PassVisitor<Substituter> sub( *this, false );
     146        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
    147147        assert( input );
    148 ///     std::cout << "substitution result is: ";
    149 ///     newType->print( std::cout );
    150 ///     std::cout << std::endl;
    151         return subCount;
     148///     std::cerr << "substitution result is: ";
     149///     newType->print( std::cerr );
     150///     std::cerr << std::endl;
     151        return sub.pass.subCount;
    152152}
    153153
     
    155155int TypeSubstitution::applyFree( SynTreeClass *&input ) {
    156156        assert( input );
    157         subCount = 0;
    158         freeOnly = true;
    159         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
     157        PassVisitor<Substituter> sub( *this, true );
     158        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
    160159        assert( input );
    161 ///     std::cout << "substitution result is: ";
    162 ///     newType->print( std::cout );
    163 ///     std::cout << std::endl;
    164         return subCount;
     160///     std::cerr << "substitution result is: ";
     161///     newType->print( std::cerr );
     162///     std::cerr << std::endl;
     163        return sub.pass.subCount;
    165164}
    166165
  • src/SynTree/VarExprReplacer.cc

    rf792cb8 r7416d46a  
    1616#include <iostream>       // for operator<<, basic_ostream, ostream, basic_o...
    1717
     18#include "Common/PassVisitor.h"
    1819#include "Declaration.h"  // for operator<<, DeclarationWithType
    1920#include "Expression.h"   // for VariableExpr
    2021#include "VarExprReplacer.h"
    2122
    22 VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {}
     23namespace 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 );
    2332
    24 // replace variable with new node from decl map
    25 void VarExprReplacer::visit( VariableExpr * varExpr ) {
    26         // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are)
    27         if ( declMap.count( varExpr->get_var() ) ) {
    28                 if ( debug ) {
    29                         std::cerr << "replacing variable reference: " << (void*)varExpr->get_var() << " " << varExpr->get_var() << " with " << (void*)declMap.at( varExpr->get_var() ) << " " << declMap.at( varExpr->get_var() ) << std::endl;
     33                        // replace variable with new node from decl map
     34                        void previsit( VariableExpr * varExpr );
     35                };
     36        }
     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                        }
    3055                }
    31                 varExpr->set_var( declMap.at( varExpr->get_var() ) );
    3256        }
    33 }
     57} // namespace VarExprReplacer
     58
     59
     60
     61
     62
     63
     64
  • src/SynTree/VarExprReplacer.h

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