Changes in / [c532847:76d73fc]


Ignore:
Location:
src
Files:
1 deleted
30 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    rc532847 r76d73fc  
    4747
    4848//================================================================================================
    49 namespace ast {
     49namespace {
    5050
    5151// This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not)
    5252// allow us to use the same stratagy in the new ast.
    53 // xxx - since convert back pass works, this concern seems to be unnecessary.
    54 
    55 // these need to be accessed in new FixInit now
    5653ast::Type * sizeType = nullptr;
    5754ast::FunctionDecl * dereferenceOperator = nullptr;
     
    6663        using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
    6764        Cache cache;
    68 
    69         // Statements can no longer be shared.
    70         // however, since StmtExprResult is now implemented, need to still maintain
    71         // readonly references.
    72         Cache readonlyCache;
    7365
    7466        template<typename T>
     
    162154        }
    163155
    164         const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {       
     156        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
     157                auto&& bfwd = get<Expression>().accept1( node->bitfieldWidth );
     158                auto&& type = get<Type>().accept1( node->type );
     159                auto&& init = get<Initializer>().accept1( node->init );
     160                auto&& attr = get<Attribute>().acceptL( node->attributes );
    165161                if ( inCache( node ) ) {
    166162                        return nullptr;
    167163                }
    168                 auto bfwd = get<Expression>().accept1( node->bitfieldWidth );
    169                 auto type = get<Type>().accept1( node->type );
    170                 auto attr = get<Attribute>().acceptL( node->attributes );
    171 
    172164                auto decl = new ObjectDecl(
    173165                        node->name,
     
    176168                        bfwd,
    177169                        type->clone(),
    178                         nullptr, // prevent infinite loop
     170                        init,
    179171                        attr,
    180172                        Type::FuncSpecifiers( node->funcSpec.val )
    181173                );
    182 
    183                 // handles the case where node->init references itself
    184                 // xxx - does it really happen?
    185                 declWithTypePostamble(decl, node);
    186                 auto init = get<Initializer>().accept1( node->init );
    187                 decl->init = init;
    188                
    189                 this->node = decl;
    190                 return nullptr;
     174                return declWithTypePostamble( decl, node );
    191175        }
    192176
     
    221205                decl->statements = get<CompoundStmt>().accept1( node->stmts );
    222206                decl->withExprs = get<Expression>().acceptL( node->withExprs );
    223                 if ( ast::dereferenceOperator == node ) {
     207                if ( dereferenceOperator == node ) {
    224208                        Validate::dereferenceOperator = decl;
    225209                }
    226                 if ( ast::dtorStructDestroy == node ) {
     210                if ( dtorStructDestroy == node ) {
    227211                        Validate::dtorStructDestroy = decl;
    228212                }
     
    283267                );
    284268
    285                 if ( ast::dtorStruct == node ) {
     269                if ( dtorStruct == node ) {
    286270                        Validate::dtorStruct = decl;
    287271                }
     
    336320
    337321        const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
    338                 // force statements in old tree to be unique.
    339                 // cache.emplace( node, stmt );
    340                 readonlyCache.emplace( node, stmt );
     322                cache.emplace( node, stmt );
    341323                stmt->location = node->location;
    342324                stmt->labels = makeLabelL( stmt, node->labels );
     
    355337                if ( inCache( node ) ) return nullptr;
    356338                auto stmt = new ExprStmt( nullptr );
     339                cache.emplace( node, stmt );
    357340                stmt->expr = get<Expression>().accept1( node->expr );
    358341                return stmtPostamble( stmt, node );
     
    10281011                auto stmts = node->stmts;
    10291012                // disable sharing between multiple StmtExprs explicitly.
    1030                 // this should no longer be true.
    1031 
     1013                if (inCache(stmts)) {
     1014                        stmts = ast::deepCopy(stmts.get());
     1015                }
    10321016                auto rslt = new StmtExpr(
    10331017                        get<CompoundStmt>().accept1(stmts)
     
    10361020                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
    10371021                rslt->dtors       = get<Expression>().acceptL(node->dtors);
    1038                 if (node->resultExpr) {
    1039                         // this MUST be found by children visit
    1040                         rslt->resultExpr  = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr));
    1041                 }
    10421022
    10431023                auto expr = visitBaseExpr( node, rslt );
     
    10561036
    10571037                auto expr = visitBaseExpr( node, rslt );
    1058                 this->node = expr->clone();
     1038                this->node = expr;
    10591039                return nullptr;
    10601040        }
     
    11461126                auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
    11471127                // I believe this should always be a BasicType.
    1148                 if ( ast::sizeType == node ) {
     1128                if ( sizeType == node ) {
    11491129                        Validate::SizeType = type;
    11501130                }
     
    15491529
    15501530                // function type is now derived from parameter decls instead of storing them
    1551 
    1552                 /*
    15531531                auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
    15541532                ftype->params.reserve(paramVars.size());
     
    15621540                }
    15631541                ftype->forall = std::move(forall);
    1564                 */
    1565 
    1566                 // can function type have attributes? seems not to be the case.
    1567                 // visitType(old->type, ftype);
     1542                visitType(old->type, ftype);
    15681543
    15691544                auto decl = new ast::FunctionDecl{
     
    15711546                        old->name,
    15721547                        // GET_ACCEPT_1(type, FunctionType),
    1573                         std::move(forall),
    15741548                        std::move(paramVars),
    15751549                        std::move(returnVars),
     
    15781552                        { old->linkage.val },
    15791553                        GET_ACCEPT_V(attributes, Attribute),
    1580                         { old->get_funcSpec().val },
    1581                         old->type->isVarArgs
     1554                        { old->get_funcSpec().val }
    15821555                };
    15831556
    1584                 // decl->type = ftype;
     1557                decl->type = ftype;
    15851558                cache.emplace( old, decl );
    15861559
     
    15971570
    15981571                if ( Validate::dereferenceOperator == old ) {
    1599                         ast::dereferenceOperator = decl;
     1572                        dereferenceOperator = decl;
    16001573                }
    16011574
    16021575                if ( Validate::dtorStructDestroy == old ) {
    1603                         ast::dtorStructDestroy = decl;
     1576                        dtorStructDestroy = decl;
    16041577                }
    16051578        }
     
    16261599
    16271600                if ( Validate::dtorStruct == old ) {
    1628                         ast::dtorStruct = decl;
     1601                        dtorStruct = decl;
    16291602                }
    16301603        }
     
    25582531                // I believe this should always be a BasicType.
    25592532                if ( Validate::SizeType == old ) {
    2560                         ast::sizeType = type;
     2533                        sizeType = type;
    25612534                }
    25622535                visitType( old, type );
  • src/AST/Decl.cpp

    rc532847 r76d73fc  
    4848
    4949// --- FunctionDecl
    50 
    51 FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name,
    52                 std::vector<ptr<TypeDecl>>&& forall,
    53                 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
    54                 CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
    55                 std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs)
    56         : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
    57           stmts( stmts ) {
    58                   FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs));
    59                   for (auto & param : this->params) {
    60                           ftype->params.emplace_back(param->get_type());
    61                   }
    62                   for (auto & ret : this->returns) {
    63                           ftype->returns.emplace_back(ret->get_type());
    64                   }
    65                   ftype->forall = std::move(forall);
    66                   this->type = ftype;
    67           }
    68 
    6950
    7051const Type * FunctionDecl::get_type() const { return type.get(); }
  • src/AST/Decl.hpp

    rc532847 r76d73fc  
    131131        std::vector< ptr<Expr> > withExprs;
    132132
    133         FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
     133        FunctionDecl( const CodeLocation & loc, const std::string & name,
    134134                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
    135135                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
    136                 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false);
    137         // : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
    138         //  stmts( stmts ) {}
     136                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
     137        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
     138          stmts( stmts ) {}
    139139
    140140        const Type * get_type() const override;
  • src/AST/DeclReplacer.cpp

    rc532847 r76d73fc  
    3838                        const ast::TypeInstType * previsit( const ast::TypeInstType * );
    3939                };
    40 
    41                 struct VarExprReplacer {
    42                 private:
    43                         const ExprMap & exprMap;
    44                        
    45                 public:
    46                         VarExprReplacer(const ExprMap & exprMap): exprMap (exprMap) {}
    47 
    48                         const Expr * postvisit (const VariableExpr *);
    49                 };
    5040        }
    5141
     
    6454                DeclMap declMap;
    6555                return replace( node, declMap, typeMap, debug );
    66         }
    67 
    68         const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap) {
    69                 Pass<VarExprReplacer> replacer = {exprMap};
    70                 return node->accept( replacer );
    7156        }
    7257
     
    10388                        return ninst;
    10489                }
    105 
    106                 const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) {
    107                         if (!exprMap.count(expr->var)) return expr;
    108 
    109                         return exprMap.at(expr->var);
    110                 }
    111 
    11290        }
    11391}
  • src/AST/DeclReplacer.hpp

    rc532847 r76d73fc  
    2323        class DeclWithType;
    2424        class TypeDecl;
    25         class Expr;
    2625
    2726        namespace DeclReplacer {
    2827                using DeclMap = std::unordered_map< const DeclWithType *, const DeclWithType * >;
    2928                using TypeMap = std::unordered_map< const TypeDecl *, const TypeDecl * >;
    30                 using ExprMap = std::unordered_map< const DeclWithType *, const Expr * >;
    3129
    3230                const Node * replace( const Node * node, const DeclMap & declMap, bool debug = false );
    3331                const Node * replace( const Node * node, const TypeMap & typeMap, bool debug = false );
    3432                const Node * replace( const Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false );
    35                 const Node * replace( const Node * node, const ExprMap & exprMap);
    3633        }
    3734}
  • src/AST/Expr.cpp

    rc532847 r76d73fc  
    6767// --- UntypedExpr
    6868
    69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, const Expr * arg ) {
     69UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {
    7070        assert( arg );
    7171
     
    9292}
    9393
    94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ) {
     94UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) {
    9595        assert( lhs && rhs );
    9696
  • src/AST/Expr.hpp

    rc532847 r76d73fc  
    226226
    227227        /// Creates a new dereference expression
    228         static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg );
     228        static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg );
    229229        /// Creates a new assignment expression
    230         static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs );
     230        static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs );
    231231
    232232        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     
    422422                const CodeLocation & loc, const Type * ty, const std::string & r,
    423423                        std::optional<unsigned long long> i )
    424         : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
     424        : Expr( loc, ty ), rep( r ), ival( i ) {}
    425425
    426426        /// Gets the integer value of this constant, if one is appropriate to its type.
     
    617617
    618618        ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
    619         : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
     619        : Expr( loc, call->result ) { assert( call ); }
    620620
    621621        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     
    742742        std::vector<ptr<Expr>> dtors;              ///< destructor(s) for return variable(s)
    743743
    744         readonly<ExprStmt> resultExpr;
    745 
    746744        StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
    747745
  • src/AST/Fwd.hpp

    rc532847 r76d73fc  
    137137typedef unsigned int UniqueId;
    138138
    139 extern Type * sizeType;
    140 extern FunctionDecl * dereferenceOperator;
    141 extern StructDecl   * dtorStruct;
    142 extern FunctionDecl * dtorStructDestroy;
    143 
    144139}
  • src/AST/Node.hpp

    rc532847 r76d73fc  
    4949
    5050        bool unique() const { return strong_count == 1; }
    51         bool isManaged() const {return strong_count > 0; }
    5251
    5352private:
  • src/AST/Pass.hpp

    rc532847 r76d73fc  
    236236        const ast::Expr * call_accept( const ast::Expr * );
    237237
    238         // requests WithStmtsToAdd directly add to this statement, as if it is a compound.
    239 
    240         const ast::Stmt * call_accept_as_compound(const ast::Stmt *);
    241 
    242238        template< typename node_t >
    243239        auto call_accept( const node_t * node ) -> typename std::enable_if<
     
    261257        template<typename node_t, typename parent_t, typename child_t>
    262258        void maybe_accept(const node_t * &, child_t parent_t::* child);
    263 
    264         template<typename node_t, typename parent_t, typename child_t>
    265         void maybe_accept_as_compound(const node_t * &, child_t parent_t::* child);
    266259
    267260private:
  • src/AST/Pass.impl.hpp

    rc532847 r76d73fc  
    167167                __pedantic_pass_assert( stmt );
    168168
    169                 return stmt->accept( *this );
    170         }
    171 
    172         template< typename core_t >
    173         const ast::Stmt * ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) {
    174                 __pedantic_pass_assert( __visit_children() );
    175                 __pedantic_pass_assert( stmt );
    176 
    177169                // add a few useful symbols to the scope
    178170                using __pass::empty;
     
    332324
    333325                auto new_val = call_accept( old_val );
    334 
    335                 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR");
    336 
    337                 if( __pass::differs(old_val, new_val) ) {
    338                         auto new_parent = __pass::mutate<core_t>(parent);
    339                         new_parent->*child = new_val;
    340                         parent = new_parent;
    341                 }
    342         }
    343 
    344         template< typename core_t >
    345         template<typename node_t, typename parent_t, typename child_t>
    346         void ast::Pass< core_t >::maybe_accept_as_compound(
    347                 const node_t * & parent,
    348                 child_t parent_t::*child
    349         ) {
    350                 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" );
    351 
    352                 if(__pass::skip(parent->*child)) return;
    353                 const auto & old_val = __pass::get(parent->*child, 0);
    354 
    355                 static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR");
    356 
    357                 auto new_val = call_accept_as_compound( old_val );
    358326
    359327                static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR");
     
    735703                maybe_accept( node, &IfStmt::inits    );
    736704                maybe_accept( node, &IfStmt::cond     );
    737                 maybe_accept_as_compound( node, &IfStmt::thenPart );
    738                 maybe_accept_as_compound( node, &IfStmt::elsePart );
     705                maybe_accept( node, &IfStmt::thenPart );
     706                maybe_accept( node, &IfStmt::elsePart );
    739707        })
    740708
     
    753721                maybe_accept( node, &WhileStmt::inits );
    754722                maybe_accept( node, &WhileStmt::cond  );
    755                 maybe_accept_as_compound( node, &WhileStmt::body  );
     723                maybe_accept( node, &WhileStmt::body  );
    756724        })
    757725
     
    768736                // for statements introduce a level of scope (for the initialization)
    769737                guard_symtab guard { *this };
    770                 // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later.
    771738                maybe_accept( node, &ForStmt::inits );
    772739                maybe_accept( node, &ForStmt::cond  );
    773740                maybe_accept( node, &ForStmt::inc   );
    774                 maybe_accept_as_compound( node, &ForStmt::body  );
     741                maybe_accept( node, &ForStmt::body  );
    775742        })
    776743
     
    867834                maybe_accept( node, &CatchStmt::decl );
    868835                maybe_accept( node, &CatchStmt::cond );
    869                 maybe_accept_as_compound( node, &CatchStmt::body );
     836                maybe_accept( node, &CatchStmt::body );
    870837        })
    871838
  • src/AST/SymbolTable.cpp

    rc532847 r76d73fc  
    335335}
    336336
    337 
    338 void SymbolTable::addFunction( const FunctionDecl * func ) {
    339         addTypes( func->type->forall );
    340         addIds( func->returns );
    341         addIds( func->params );
    342 }
    343 
     337/*
     338void SymbolTable::addFunctionType( const FunctionType * ftype ) {
     339        addTypes( ftype->forall );
     340        addIds( ftype->returns );
     341        addIds( ftype->params );
     342}
     343*/
    344344
    345345void SymbolTable::lazyInitScope() {
  • src/AST/SymbolTable.hpp

    rc532847 r76d73fc  
    145145
    146146        /// convenience function for adding all of the declarations in a function type to the indexer
    147         void addFunction( const FunctionDecl * );
     147        // void addFunctionType( const FunctionType * ftype );
    148148
    149149private:
  • src/Common/utility.h

    rc532847 r76d73fc  
    360360        reverse_iterate_t( T & ref ) : ref(ref) {}
    361361
    362         // this does NOT work on const T!!!
    363         // typedef typename T::reverse_iterator iterator;
    364         auto begin() { return ref.rbegin(); }
    365         auto end() { return ref.rend(); }
     362        typedef typename T::reverse_iterator iterator;
     363        iterator begin() { return ref.rbegin(); }
     364        iterator end() { return ref.rend(); }
    366365};
    367366
  • src/GenPoly/GenPoly.cc

    rc532847 r76d73fc  
    4646                }
    4747
    48                 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env) {
    49                         for (auto &param : params) {
    50                                 auto paramType = param.strict_as<ast::TypeExpr>();
    51                                 if (isPolyType(paramType->type, env)) return true;
    52                         }
    53                         return false;
    54                 }
    55 
    5648                /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
    5749                bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
     
    6456                }
    6557
    66                 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const TyVarMap & tyVars, const ast::TypeSubstitution * env) {
    67                         for (auto &param : params) {
    68                                 auto paramType = param.strict_as<ast::TypeExpr>();
    69                                 if (isPolyType(paramType->type, tyVars, env)) return true;
    70                         }
    71                         return false;
    72                 }
    73 
    7458                /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present
    7559                bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
     
    10892                        Type *newType = env->lookup( typeInst->get_name() );
    10993                        if ( newType ) return newType;
    110                 }
    111                 return type;
    112         }
    113 
    114         const ast::Type * replaceTypeInst(const ast::Type * type, const ast::TypeSubstitution * env) {
    115                 if (!env) return type;
    116                 if (auto typeInst = dynamic_cast<const ast::TypeInstType*> (type)) {
    117                         auto newType = env->lookup(typeInst->name);
    118                         if (newType) return newType;
    11994                }
    12095                return type;
     
    136111        }
    137112
    138         const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env) {
    139                 type = replaceTypeInst( type, env );
    140 
    141                 if ( dynamic_cast< const ast::TypeInstType * >( type ) ) {
    142                         return type;
    143                 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
    144                         return isPolyType( arrayType->base, env );
    145                 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
    146                         if ( hasPolyParams( structType->params, env ) ) return type;
    147                 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
    148                         if ( hasPolyParams( unionType->params, env ) ) return type;
    149                 }
    150                 return 0;
    151         }
    152 
    153113        Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
    154114                type = replaceTypeInst( type, env );
     
    166126                }
    167127                return 0;
    168         }
    169 
    170         const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env) {
    171                 type = replaceTypeInst( type, env );
    172 
    173                 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
    174                         return tyVars.find(typeInst->name) != tyVars.end() ? type : nullptr;
    175                 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
    176                         return isPolyType( arrayType->base, env );
    177                 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
    178                         if ( hasPolyParams( structType->params, env ) ) return type;
    179                 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
    180                         if ( hasPolyParams( unionType->params, env ) ) return type;
    181                 }
    182                 return nullptr;
    183128        }
    184129
     
    504449        }
    505450
    506         namespace {
    507                 // temporary hack to avoid re-implementing anything related to TyVarMap
    508                 // does this work? these two structs have identical definitions.
    509                 inline TypeDecl::Data convData(const ast::TypeDecl::Data & data) {
    510                         return *reinterpret_cast<const TypeDecl::Data *>(&data);
    511                 }
    512         }
    513 
    514451        bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ) {
    515452                // is parameter is not polymorphic, don't need to box
     
    522459        }
    523460
    524         bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TyVarMap &exprTyVars, const ast::TypeSubstitution * env) {
    525                 // is parameter is not polymorphic, don't need to box
    526                 if ( ! isPolyType( param, exprTyVars ) ) return false;
    527                 ast::ptr<ast::Type> newType = arg;
    528                 if ( env ) env->apply( newType );
    529                 // if the argument's type is polymorphic, we don't need to box again!
    530                 return ! isPolyType( newType );
    531         }
    532 
    533461        bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ) {
    534462                FunctionType * function = getFunctionType( appExpr->function->result );
     
    539467        }
    540468
    541         bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * appExpr, const ast::TypeSubstitution * env) {
    542                 const ast::FunctionType * function = getFunctionType(appExpr->func->result);
    543                 assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->func->result ).c_str() );
    544                 TyVarMap exprTyVars(TypeDecl::Data{});
    545                 makeTyVarMap(function, exprTyVars);
    546                 return needsBoxing(param, arg, exprTyVars, env);
    547 
    548         }
    549 
    550469        void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) {
    551470                tyVarMap.insert( tyVar->name, TypeDecl::Data{ tyVar } );
    552         }
    553 
    554         void addToTyVarMap( const ast::TypeDecl * tyVar, TyVarMap & tyVarMap) {
    555                 tyVarMap.insert(tyVar->name, convData(ast::TypeDecl::Data{tyVar}));
    556471        }
    557472
     
    563478                if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
    564479                        makeTyVarMap( pointer->get_base(), tyVarMap );
    565                 }
    566         }
    567 
    568         void makeTyVarMap(const ast::Type * type, TyVarMap & tyVarMap) {
    569                 if (auto ptype = dynamic_cast<const ast::ParameterizedType *>(type)) {
    570                         for (auto & tyVar : ptype->forall) {
    571                                 assert (tyVar);
    572                                 addToTyVarMap(tyVar, tyVarMap);
    573                         }
    574                 }
    575                 if (auto pointer = dynamic_cast<const ast::PointerType *>(type)) {
    576                         makeTyVarMap(pointer->base, tyVarMap);
    577480                }
    578481        }
  • src/GenPoly/GenPoly.h

    rc532847 r76d73fc  
    2626
    2727namespace GenPoly {
     28        typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap;
    2829
    29         typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap;
    3030        /// Replaces a TypeInstType by its referrent in the environment, if applicable
    3131        Type* replaceTypeInst( Type* type, const TypeSubstitution* env );
     
    3333        /// returns polymorphic type if is polymorphic type, NULL otherwise; will look up substitution in env if provided
    3434        Type *isPolyType( Type *type, const TypeSubstitution *env = 0 );
    35         const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env = nullptr);
    3635
    3736        /// returns polymorphic type if is polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided
    3837        Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );
    39         const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env = nullptr);
    4038
    4139        /// returns dynamic-layout type if is dynamic-layout type in tyVars, NULL otherwise; will look up substitution in env if provided
     
    8684        /// true if arg requires boxing given exprTyVars
    8785        bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env );
    88         bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TyVarMap &exprTyVars, const ast::TypeSubstitution * env);
    8986
    9087        /// true if arg requires boxing in the call to appExpr
    9188        bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env );
    92         bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * appExpr, const ast::TypeSubstitution * env);
    9389
    9490        /// Adds the type variable `tyVar` to `tyVarMap`
     
    9793        /// Adds the declarations in the forall list of type (and its pointed-to type if it's a pointer type) to `tyVarMap`
    9894        void makeTyVarMap( Type *type, TyVarMap &tyVarMap );
    99         void makeTyVarMap(const ast::Type * type, TyVarMap & tyVarMap);
    10095
    10196        /// Prints type variable map
  • src/InitTweak/FixGlobalInit.cc

    rc532847 r76d73fc  
    3434#include "SynTree/Visitor.h"       // for acceptAll, Visitor
    3535
    36 #include "AST/Expr.hpp"
    37 #include "AST/Node.hpp"
    38 #include "AST/Pass.hpp"
    39 
    4036namespace InitTweak {
    4137        class GlobalFixer : public WithShortCircuiting {
     
    5450                FunctionDecl * initFunction;
    5551                FunctionDecl * destroyFunction;
    56         };
    57 
    58         class GlobalFixer_new : public ast::WithShortCircuiting {
    59         public:
    60                 void previsit (const ast::ObjectDecl *);
    61                 void previsit (const ast::FunctionDecl *) { visit_children = false; }
    62                 void previsit (const ast::StructDecl *) { visit_children = false; }
    63                 void previsit (const ast::UnionDecl *) { visit_children = false; }
    64                 void previsit (const ast::EnumDecl *) { visit_children = false; }
    65                 void previsit (const ast::TraitDecl *) { visit_children = false; }
    66                 void previsit (const ast::TypeDecl *) { visit_children = false; }
    67 
    68                 std::list< ast::ptr<ast::Stmt> > initStmts;
    69                 std::list< ast::ptr<ast::Stmt> > destroyStmts;
    7052        };
    7153
     
    10991        }
    11092
    111         void fixGlobalInit(std::list<ast::ptr<ast::Decl>> & translationUnit, bool inLibrary) {
    112                 ast::Pass<GlobalFixer_new> fixer;
    113                 accept_all(translationUnit, fixer);
    114 
    115                 if ( !fixer.core.initStmts.empty() ) {
    116                         std::vector<ast::ptr<ast::Expr>> ctorParams;
    117                         if (inLibrary) ctorParams.emplace_back(ast::ConstantExpr::from_int({}, 200));
    118                         auto initFunction = new ast::FunctionDecl({}, "__global_init__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.initStmts)),
    119                                 ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("constructor", std::move(ctorParams))});
    120 
    121                         translationUnit.emplace_back( initFunction );
    122                 } // if
    123 
    124                 if ( !fixer.core.destroyStmts.empty() ) {
    125                         std::vector<ast::ptr<ast::Expr>> dtorParams;
    126                         if (inLibrary) dtorParams.emplace_back(ast::ConstantExpr::from_int({}, 200));
    127                         auto destroyFunction = new ast::FunctionDecl({}, "__global_destroy__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.destroyStmts)),
    128                                 ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("destructor", std::move(dtorParams))});
    129 
    130                         translationUnit.emplace_back(destroyFunction);
    131                 } // if
    132         }
    133 
    13493        void GlobalFixer::previsit( ObjectDecl *objDecl ) {
    13594                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
     
    168127        }
    169128
    170         void GlobalFixer_new::previsit(const ast::ObjectDecl * objDecl) {
    171                 auto mutDecl = mutate(objDecl);
    172                 assertf(mutDecl == objDecl, "Global object decl must be unique");
    173                 if ( auto ctorInit = objDecl->init.as<ast::ConstructorInit>() ) {
    174                         // a decision should have been made by the resolver, so ctor and init are not both non-NULL
    175                         assert( ! ctorInit->ctor || ! ctorInit->init );
    176 
    177                         const ast::Stmt * dtor = ctorInit->dtor;
    178                         if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
    179                                 // don't need to call intrinsic dtor, because it does nothing, but
    180                                 // non-intrinsic dtors must be called
    181                                 destroyStmts.push_front( dtor );
    182                                 // ctorInit->dtor = nullptr;
    183                         } // if
    184                         if ( const ast::Stmt * ctor = ctorInit->ctor ) {
    185                                 initStmts.push_back( ctor );
    186                                 mutDecl->init = nullptr;
    187                                 // ctorInit->ctor = nullptr;
    188                         } else if ( const ast::Init * init = ctorInit->init ) {
    189                                 mutDecl->init = init;
    190                                 // ctorInit->init = nullptr;
    191                         } else {
    192                                 // no constructor and no initializer, which is okay
    193                                 mutDecl->init = nullptr;
    194                         } // if
    195                         // delete ctorInit;
    196                 } // if
    197         }
    198 
    199129        // only modify global variables
    200130        void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; }
  • src/InitTweak/FixGlobalInit.h

    rc532847 r76d73fc  
    1919#include <string>  // for string
    2020
    21 #include <AST/Fwd.hpp>
    22 
    23 
    2421class Declaration;
    2522
     
    2926        /// function is for library code.
    3027        void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary );
    31         void fixGlobalInit( std::list< ast::ptr<ast::Decl> > & translationUnit, bool inLibrary );
    3228} // namespace
    3329
  • src/InitTweak/FixInit.cc

    rc532847 r76d73fc  
    219219                };
    220220
    221                 struct SplitExpressions : public WithShortCircuiting, /*public WithTypeSubstitution, */public WithStmtsToAdd {
     221                struct SplitExpressions : public WithShortCircuiting, public WithTypeSubstitution, public WithStmtsToAdd {
    222222                        /// add CompoundStmts around top-level expressions so that temporaries are destroyed in the correct places.
    223223                        static void split( std::list< Declaration * > &translationUnit );
  • src/InitTweak/FixInit.h

    rc532847 r76d73fc  
    1919#include <string>  // for string
    2020
    21 #include <AST/Fwd.hpp>
    22 
    2321class Declaration;
    2422
     
    2624        /// replace constructor initializers with expression statements and unwrap basic C-style initializers
    2725        void fix( std::list< Declaration * > & translationUnit, bool inLibrary );
    28 
    29         void fix( std::list<ast::ptr<ast::Decl>> & translationUnit, bool inLibrary);
    3026} // namespace
    3127
  • src/InitTweak/GenInit.cc

    rc532847 r76d73fc  
    283283                assert( stmts.size() <= 1 );
    284284                return stmts.size() == 1 ? strict_dynamic_cast< ImplicitCtorDtorStmt * >( stmts.front() ) : nullptr;
    285 
    286         }
    287 
    288         ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) {
    289                 assertf(objDecl, "genCtorDtor passed null objDecl");
    290                 InitExpander_new srcParam(arg);
    291                 return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl);
    292285        }
    293286
  • src/InitTweak/GenInit.h

    rc532847 r76d73fc  
    3333        /// generates a single ctor/dtor statement using objDecl as the 'this' parameter and arg as the optional argument
    3434        ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg = nullptr );
    35         ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg = nullptr);
    3635
    3736        /// creates an appropriate ConstructorInit node which contains a constructor, destructor, and C-initializer
  • src/InitTweak/InitTweak.cc

    rc532847 r76d73fc  
    498498        }
    499499
    500         const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func) {
    501                 assertf( func, "getParamThis: nullptr ftype" );
    502                 auto & params = func->params;
    503                 assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( func ).c_str());
    504                 return params.front().strict_as<ast::ObjectDecl>();
    505         }
    506 
    507500        bool tryConstruct( DeclarationWithType * dwt ) {
    508501                ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
     
    518511        }
    519512
    520         bool tryConstruct( const ast::DeclWithType * dwt ) {
    521                 auto objDecl = dynamic_cast< const ast::ObjectDecl * >( dwt );
    522                 if ( ! objDecl ) return false;
    523                 return (objDecl->init == nullptr ||
    524                                 ( objDecl->init != nullptr && objDecl->init->maybeConstructed ))
    525                         && ! objDecl->storage.is_extern
    526                         && isConstructable( objDecl->type );
    527         }
    528 
    529         bool isConstructable( const ast::Type * type ) {
    530                 return ! dynamic_cast< const ast::VarArgsType * >( type ) && ! dynamic_cast< const ast::ReferenceType * >( type )
    531                 && ! dynamic_cast< const ast::FunctionType * >( type ) && ! Tuples::isTtype( type );
    532         }
    533 
    534513        struct CallFinder_old {
    535514                CallFinder_old( const std::list< std::string > & names ) : names( names ) {}
     
    557536
    558537        struct CallFinder_new final {
    559                 std::vector< const ast::Expr * > matches;
     538                std::vector< ast::ptr< ast::Expr > > matches;
    560539                const std::vector< std::string > names;
    561540
     
    579558        }
    580559
    581         std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt ) {
     560        std::vector< ast::ptr< ast::Expr > > collectCtorDtorCalls( const ast::Stmt * stmt ) {
    582561                ast::Pass< CallFinder_new > finder{ std::vector< std::string >{ "?{}", "^?{}" } };
    583562                maybe_accept( stmt, finder );
     
    717696                template <typename Predicate>
    718697                bool allofCtorDtor( const ast::Stmt * stmt, const Predicate & pred ) {
    719                         std::vector< const ast::Expr * > callExprs = collectCtorDtorCalls( stmt );
     698                        std::vector< ast::ptr< ast::Expr > > callExprs = collectCtorDtorCalls( stmt );
    720699                        return std::all_of( callExprs.begin(), callExprs.end(), pred );
    721700                }
     
    960939        }
    961940
    962         // looks like some other such codegen uses UntypedExpr and does not create fake function. should revisit afterwards
    963         // following passes may accidentally resolve this expression if returned as untyped...
    964         ast::Expr * createBitwiseAssignment (const ast::Expr * dst, const ast::Expr * src) {
    965                 static ast::ptr<ast::FunctionDecl> assign = nullptr;
    966                 if (!assign) {
    967                         auto td = new ast::TypeDecl({}, "T", {}, nullptr, ast::TypeDecl::Dtype, true);
    968                         assign = new ast::FunctionDecl({}, "?=?", {},
    969                         { new ast::ObjectDecl({}, "_dst", new ast::ReferenceType(new ast::TypeInstType("T", td))),
    970                           new ast::ObjectDecl({}, "_src", new ast::TypeInstType("T", td))},
    971                         { new ast::ObjectDecl({}, "_ret", new ast::TypeInstType("T", td))}, nullptr, {}, ast::Linkage::Intrinsic);
    972                 }
    973                 if (dst->result.as<ast::ReferenceType>()) {
    974                         for (int depth = dst->result->referenceDepth(); depth > 0; depth--) {
    975                                 dst = new ast::AddressExpr(dst);
    976                         }
    977                 }
    978                 else {
    979                         dst = new ast::CastExpr(dst, new ast::ReferenceType(dst->result, {}));
    980                 }
    981                 if (src->result.as<ast::ReferenceType>()) {
    982                         for (int depth = src->result->referenceDepth(); depth > 0; depth--) {
    983                                 src = new ast::AddressExpr(src);
    984                         }
    985                 }
    986                 return new ast::ApplicationExpr(dst->location, ast::VariableExpr::functionPointer(dst->location, assign), {dst, src});
    987         }
    988 
    989941        struct ConstExprChecker : public WithShortCircuiting {
    990942                // most expressions are not const expr
  • src/InitTweak/InitTweak.h

    rc532847 r76d73fc  
    3838        /// returns the first parameter of a constructor/destructor/assignment function
    3939        ObjectDecl * getParamThis( FunctionType * ftype );
    40         const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func);
    4140
    4241        /// generate a bitwise assignment operation.
    4342        ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src );
    44 
    45         ast::Expr * createBitwiseAssignment( const ast::Expr * dst, const ast::Expr * src);
    4643
    4744        /// transform Initializer into an argument list that can be passed to a call expression
     
    5148        /// True if the resolver should try to construct dwt
    5249        bool tryConstruct( DeclarationWithType * dwt );
    53         bool tryConstruct( const ast::DeclWithType * dwt );
    5450
    5551        /// True if the type can have a user-defined constructor
    5652        bool isConstructable( Type * t );
    57         bool isConstructable( const ast::Type * t );
    5853
    5954        /// True if the Initializer contains designations
     
    8479        /// get all Ctor/Dtor call expressions from a Statement
    8580        void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches );
    86         std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt );
     81        std::vector< ast::ptr< ast::Expr > > collectCtorDtorCalls( const ast::Stmt * stmt );
    8782
    8883        /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call
  • src/InitTweak/module.mk

    rc532847 r76d73fc  
    2323        InitTweak/GenInit.h \
    2424        InitTweak/InitTweak.cc \
    25         InitTweak/InitTweak.h \
    26         InitTweak/FixInitNew.cpp
     25        InitTweak/InitTweak.h
    2726
    2827SRCDEMANGLE += \
  • src/ResolvExpr/Resolver.cc

    rc532847 r76d73fc  
    11051105                }
    11061106
    1107                
    1108         } // anonymous namespace
    1109 /// Establish post-resolver invariants for expressions
     1107                /// Establish post-resolver invariants for expressions
    11101108                void finishExpr(
    11111109                        ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env,
     
    11201118                        StripCasts_new::strip( expr );
    11211119                }
     1120        } // anonymous namespace
     1121
    11221122
    11231123        ast::ptr< ast::Expr > resolveInVoidContext(
     
    11391139        }
    11401140
    1141         /// Resolve `untyped` to the expression whose candidate is the best match for a `void`
     1141        namespace {
     1142                /// Resolve `untyped` to the expression whose candidate is the best match for a `void`
    11421143                /// context.
    11431144                ast::ptr< ast::Expr > findVoidExpression(
     
    11501151                        return newExpr;
    11511152                }
    1152 
    1153         namespace {
    1154                
    11551153
    11561154                /// resolve `untyped` to the expression whose candidate satisfies `pred` with the
     
    11641162                        CandidateRef choice =
    11651163                                findUnfinishedKindExpression( untyped, symtab, kind, pred, mode );
    1166                         ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env );
     1164                        finishExpr( choice->expr, choice->env, untyped->env );
    11671165                        return std::move( choice->expr );
    11681166                }
  • src/ResolvExpr/Resolver.h

    rc532847 r76d73fc  
    6666        ast::ptr< ast::Expr > findSingleExpression(
    6767                const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab );
    68         ast::ptr< ast::Expr > findVoidExpression(
    69                 const ast::Expr * untyped, const ast::SymbolTable & symtab);
    7068        /// Resolves a constructor init expression
    7169        ast::ptr< ast::Init > resolveCtorInit(
  • src/SymTab/Autogen.cc

    rc532847 r76d73fc  
    233233        }
    234234
    235         // shallow copy the pointer list for return
    236         std::vector<ast::ptr<ast::TypeDecl>> getGenericParams (const ast::Type * t) {
    237                 if (auto structInst = dynamic_cast<const ast::StructInstType*>(t)) {
    238                         return structInst->base->params;
    239                 }
    240                 if (auto unionInst = dynamic_cast<const ast::UnionInstType*>(t)) {
    241                         return unionInst->base->params;
    242                 }
    243                 return {};
    244         }
    245 
    246235        /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *)
    247236        FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic ) {
     
    255244                ftype->parameters.push_back( dstParam );
    256245                return ftype;
    257         }
    258 
    259         ///
    260         ast::FunctionDecl * genDefaultFunc(const CodeLocation loc, const std::string fname, const ast::Type * paramType, bool maybePolymorphic) {
    261                 std::vector<ast::ptr<ast::TypeDecl>> typeParams;
    262                 if (maybePolymorphic) typeParams = getGenericParams(paramType);
    263                 auto dstParam = new ast::ObjectDecl(loc, "_dst", new ast::ReferenceType(paramType), nullptr, {}, ast::Linkage::Cforall);
    264                 return new ast::FunctionDecl(loc, fname, std::move(typeParams), {dstParam}, {}, new ast::CompoundStmt(loc));
    265246        }
    266247
  • src/SymTab/Autogen.h

    rc532847 r76d73fc  
    5555        /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
    5656        FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true );
    57 
    58         ast::FunctionDecl * genDefaultFunc(const CodeLocation loc, const std::string fname, const ast::Type * paramType, bool maybePolymorphic = true);
    5957
    6058        /// generate the type of a copy constructor for paramType.
  • src/main.cc

    rc532847 r76d73fc  
    343343                        auto transUnit = convert( move( translationUnit ) );
    344344                        PASS( "Resolve", ResolvExpr::resolve( transUnit ) );
    345                         if ( exprp ) {
    346                                 translationUnit = convert( move( transUnit ) );
    347                                 dump( translationUnit );
    348                                 return EXIT_SUCCESS;
    349                         } // if
    350 
    351                         PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary()));
    352345                        translationUnit = convert( move( transUnit ) );
    353346                } else {
    354347                        PASS( "Resolve", ResolvExpr::resolve( translationUnit ) );
    355                         if ( exprp ) {
    356                                 dump( translationUnit );
    357                                 return EXIT_SUCCESS;
    358                         }
    359 
    360                         PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );
    361348                }
    362349
     350                if ( exprp ) {
     351                        dump( translationUnit );
     352                        return EXIT_SUCCESS;
     353                } // if
     354
    363355                // fix ObjectDecl - replaces ConstructorInit nodes
     356                PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );
    364357                if ( ctorinitp ) {
    365358                        dump ( translationUnit );
Note: See TracChangeset for help on using the changeset viewer.