Changeset 7f6a7c9 for src/AST


Ignore:
Timestamp:
Sep 21, 2022, 11:02:15 AM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, stuck-waitfor-destruct
Children:
95dab9e
Parents:
428adbc (diff), 0bd46fd (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' into pthread-emulation

Location:
src/AST
Files:
2 added
15 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r428adbc r7f6a7c9  
    310310                        node->name,
    311311                        get<Attribute>().acceptL( node->attributes ),
     312                        false, // Temporary
    312313                        LinkageSpec::Spec( node->linkage.val ),
    313314                        get<Type>().accept1(node->base)
     
    731732        }
    732733
     734        const ast::Expr * visit( const ast::QualifiedNameExpr * node ) override final {
     735                auto temp = new QualifiedNameExpr(
     736                                get<Declaration>().accept1(node->type_decl),
     737                                node->name
     738                );
     739                temp->var = get<DeclarationWithType>().accept1(node->var);
     740                auto expr = visitBaseExpr( node,
     741                        temp
     742                );
     743                this->node = expr;
     744                return nullptr;
     745        }
     746
    733747        const ast::Expr * visit( const ast::AddressExpr * node ) override final {
    734748                auto expr = visitBaseExpr( node,
     
    17401754                        old->location,
    17411755                        old->name,
     1756                        old->isTyped,
    17421757                        GET_ACCEPT_V(attributes, Attribute),
    17431758                        { old->linkage.val },
     
    22662281        }
    22672282
     2283        /// xxx - type_decl should be DeclWithType in the final design
     2284        /// type_decl is set to EnumDecl as a temporary fix
     2285        virtual void visit( const QualifiedNameExpr * old ) override final {
     2286                this->node = visitBaseExpr( old,
     2287                        new ast::QualifiedNameExpr (
     2288                                old->location,
     2289                                GET_ACCEPT_1(type_decl, Decl),
     2290                                GET_ACCEPT_1(var, DeclWithType),
     2291                                old->name
     2292                        )
     2293                );
     2294        }
     2295
    22682296        virtual void visit( const CastExpr * old ) override final {
    22692297                this->node = visitBaseExpr( old,
  • src/AST/Decl.hpp

    r428adbc r7f6a7c9  
    217217
    218218        /// convenience accessor to match Type::isComplete()
    219         bool isComplete() { return sized; }
     219        bool isComplete() const { return sized; }
    220220
    221221        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     
    312312class EnumDecl final : public AggregateDecl {
    313313public:
     314        bool isTyped;
    314315        ptr<Type> base;
    315316
    316         EnumDecl( const CodeLocation& loc, const std::string& name,
    317                 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, Type const * base = nullptr,
     317        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
     318                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
     319                Type const * base = nullptr,
    318320                std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
    319         : AggregateDecl( loc, name, std::move(attrs), linkage ), base(base), enumValues(enumValues) {}
     321        : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), enumValues(enumValues) {}
    320322
    321323        /// gets the integer value for this enumerator, returning true iff value found
     
    327329        const char * typeString() const override { return aggrString( Enum ); }
    328330
    329         bool isTyped() {return base && base.get();}
    330331
    331332private:
  • src/AST/DeclReplacer.cpp

    r428adbc r7f6a7c9  
    99// Author           : Aaron B. Moss
    1010// Created On       : Wed May 8 13:00:00 2019
    11 // Last Modified By : Aaron B. Moss
    12 // Last Modified On : Wed May 8 13:00:00 2019
    13 // Update Count     : 1
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Thr Sep 15 11:55:00 2022
     13// Update Count     : 2
    1414//
    1515
    1616#include "DeclReplacer.hpp"
     17
    1718#include "Expr.hpp"
     19#include "Pass.hpp"
    1820#include "Type.hpp"
    19 
    20 #include "Pass.hpp"
    2121
    2222namespace ast {
    2323
    2424namespace DeclReplacer {
    25         namespace {
    26                 struct DeclReplacer {
    27                 private:
    28                         const DeclMap & declMap;
    29                         const TypeMap & typeMap;
    30                         bool debug;
    3125
    32                 public:
    33                         DeclReplacer(const DeclMap & declMap, const TypeMap & typeMap, bool debug)
    34                                 : declMap( declMap ), typeMap( typeMap ), debug( debug )
    35                         {}
     26namespace {
     27        struct DeclReplacer {
     28        private:
     29                const DeclMap & declMap;
     30                const TypeMap & typeMap;
     31                bool debug;
    3632
    37                         const ast::VariableExpr * previsit( const ast::VariableExpr * );
    38                         const ast::TypeInstType * previsit( const ast::TypeInstType * );
    39                 };
     33        public:
     34                DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug )
     35                        : declMap( declMap ), typeMap( typeMap ), debug( debug )
     36                {}
    4037
    41                 struct VarExprReplacer {
    42                 private:
    43                         const ExprMap & exprMap;
    44                        
    45                 public:
    46                         VarExprReplacer(const ExprMap & exprMap): exprMap (exprMap) {}
     38                const ast::VariableExpr * previsit( const ast::VariableExpr * );
     39                const ast::TypeInstType * previsit( const ast::TypeInstType * );
     40        };
    4741
    48                         const Expr * postvisit (const VariableExpr *);
    49                 };
     42        struct VarExprReplacer {
     43        private:
     44                const ExprMap & exprMap;
     45
     46        public:
     47                VarExprReplacer( const ExprMap & exprMap ) : exprMap( exprMap ) {}
     48
     49                const Expr * postvisit( const VariableExpr * );
     50        };
     51} // namespace
     52
     53const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
     54        if(!node) return nullptr;
     55        Pass<DeclReplacer> replacer = { declMap, typeMap, debug };
     56        return node->accept( replacer );
     57}
     58
     59const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) {
     60        TypeMap typeMap;
     61        return replace( node, declMap, typeMap, debug );
     62}
     63
     64const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) {
     65        DeclMap declMap;
     66        return replace( node, declMap, typeMap, debug );
     67}
     68
     69const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap ) {
     70        Pass<VarExprReplacer> replacer = {exprMap};
     71        return node->accept( replacer );
     72}
     73
     74namespace {
     75        // replace variable with new node from decl map
     76        const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) {
     77                // 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)
     78                if ( !declMap.count( varExpr->var ) ) return varExpr;
     79
     80                auto replacement = declMap.at( varExpr->var );
     81                if ( debug ) {
     82                        std::cerr << "replacing variable reference: "
     83                                << (void*)varExpr->var.get() << " " << varExpr->var
     84                                << " with " << (void*)replacement << " " << replacement
     85                                << std::endl;
     86                }
     87                auto nexpr = mutate(varExpr);
     88                nexpr->var = replacement;
     89                return nexpr;
    5090        }
    5191
    52         const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
    53                 if(!node) return nullptr;
    54                 Pass<DeclReplacer> replacer = { declMap, typeMap, debug };
    55                 return node->accept( replacer );
     92        const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) {
     93                if ( !typeMap.count( inst->base ) ) return inst;
     94
     95                auto replacement = typeMap.at( inst->base );
     96                if ( debug ) {
     97                        std::cerr << "replacing type reference: "
     98                                << (void*)inst->base.get() << " " << inst->base
     99                                << " with " << (void*)replacement << " " << replacement
     100                                << std::endl;
     101                }
     102                auto ninst = mutate(inst);
     103                ninst->base = replacement;
     104                return ninst;
    56105        }
    57106
    58         const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) {
    59                 TypeMap typeMap;
    60                 return replace( node, declMap, typeMap, debug );
     107        const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) {
     108                if ( !exprMap.count( expr->var ) ) return expr;
     109                return exprMap.at( expr->var );
    61110        }
     111} // namespace
    62112
    63         const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) {
    64                 DeclMap declMap;
    65                 return replace( node, declMap, typeMap, debug );
    66         }
     113} // namespace DeclReplacer
    67114
    68         const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap) {
    69                 Pass<VarExprReplacer> replacer = {exprMap};
    70                 return node->accept( replacer );
    71         }
    72 
    73         namespace {
    74                 // replace variable with new node from decl map
    75                 const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) {
    76                         // 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)
    77                         if ( !declMap.count( varExpr->var ) ) return varExpr;
    78 
    79                         auto replacement = declMap.at( varExpr->var );
    80                         if ( debug ) {
    81                                 std::cerr << "replacing variable reference: "
    82                                         << (void*)varExpr->var.get() << " " << varExpr->var
    83                                         << " with " << (void*)replacement << " " << replacement
    84                                         << std::endl;
    85                         }
    86                         auto nexpr = mutate(varExpr);
    87                         nexpr->var = replacement;
    88                         return nexpr;
    89                 }
    90 
    91                 const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) {
    92                         if ( !typeMap.count( inst->base ) ) return inst;
    93 
    94                         auto replacement = typeMap.at( inst->base );
    95                         if ( debug ) {
    96                                 std::cerr << "replacing type reference: "
    97                                         << (void*)inst->base.get() << " " << inst->base
    98                                         << " with " << (void*)replacement << " " << replacement
    99                                         << std::endl;
    100                         }
    101                         auto ninst = mutate(inst);
    102                         ninst->base = replacement;
    103                         return ninst;
    104                 }
    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 
    112         }
    113 }
    114 
    115 }
     115} // namespace ast
    116116
    117117// Local Variables: //
  • src/AST/Expr.hpp

    r428adbc r7f6a7c9  
    254254};
    255255
     256class QualifiedNameExpr final : public Expr {
     257public:
     258        ptr<Decl> type_decl;
     259        ptr<DeclWithType> var;
     260        std::string name;
     261
     262        QualifiedNameExpr( const CodeLocation & loc, const Decl * d, const DeclWithType * r, const std::string & n )
     263        : Expr( loc ), type_decl( d ), var(r), name( n ) {}
     264
     265        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     266private:
     267        QualifiedNameExpr * clone() const override { return new QualifiedNameExpr{ *this }; }
     268        MUTATE_FRIEND
     269};
     270
    256271/// A reference to a named variable.
    257272class VariableExpr final : public Expr {
  • src/AST/Fwd.hpp

    r428adbc r7f6a7c9  
    6767class UntypedExpr;
    6868class NameExpr;
     69class QualifiedNameExpr;
    6970class AddressExpr;
    7071class LabelAddressExpr;
  • src/AST/Pass.hpp

    r428adbc r7f6a7c9  
    167167        const ast::Expr *             visit( const ast::UntypedExpr          * ) override final;
    168168        const ast::Expr *             visit( const ast::NameExpr             * ) override final;
     169        const ast::Expr *                         visit( const ast::QualifiedNameExpr    * ) override final;
    169170        const ast::Expr *             visit( const ast::AddressExpr          * ) override final;
    170171        const ast::Expr *             visit( const ast::LabelAddressExpr     * ) override final;
     
    327328struct PureVisitor {};
    328329
     330/// Keep track of the nearest parent node's location field.
     331struct WithCodeLocation {
     332        const CodeLocation * location = nullptr;
     333};
     334
    329335/// Keep track of the polymorphic const TypeSubstitution * typeSubs for the current expression.
    330336struct WithConstTypeSubstitution {
  • src/AST/Pass.impl.hpp

    r428adbc r7f6a7c9  
    2525#define VISIT_START( node ) \
    2626        using namespace ast; \
     27        /* back-up the last known code location */ \
     28        __attribute__((unused)) auto loc_guard = ast::__pass::make_location_guard( core, node, 0 ); \
    2729        /* back-up the visit children */ \
    2830        __attribute__((unused)) ast::__pass::visit_children_guard guard1( ast::__pass::visit_children(core, 0) ); \
     
    11971199
    11981200//--------------------------------------------------------------------------
     1201// QualifiedNameExpr
     1202template< typename core_t >
     1203const ast::Expr * ast::Pass< core_t >::visit( const ast::QualifiedNameExpr * node ) {
     1204        VISIT_START( node );
     1205        if ( __visit_children() ) {
     1206                guard_symtab guard { *this };
     1207                maybe_accept( node, &QualifiedNameExpr::var );
     1208                maybe_accept( node, &QualifiedNameExpr::type_decl );
     1209        }
     1210        VISIT_END( Expr, node );
     1211}
     1212
     1213//--------------------------------------------------------------------------
    11991214// CastExpr
    12001215template< typename core_t >
  • src/AST/Pass.proto.hpp

    r428adbc r7f6a7c9  
    326326        }
    327327
     328        template< typename core_t, typename node_t >
     329        static auto make_location_guard( core_t & core, node_t * node, int )
     330                        -> decltype( node->location, ValueGuardPtr<const CodeLocation *>( &core.location ) ) {
     331                ValueGuardPtr<const CodeLocation *> guard( &core.location );
     332                core.location = &node->location;
     333                return guard;
     334        }
     335
     336        template< typename core_t, typename node_t >
     337        static auto make_location_guard( core_t &, node_t *, long ) -> int {
     338                return 0;
     339        }
     340
    328341        // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
    329342        // All passes which have such functions are assumed desire this behaviour
  • src/AST/Print.cpp

    r428adbc r7f6a7c9  
    3333{
    3434        return array<C,sizeof...(T)>{
    35                 forward<T>(values)...
     35                std::forward<T>(values)...
    3636        };
    3737}
     
    8686
    8787                static constexpr auto StorageClasses = make_array<const char*>(
    88                         "extern", "static", "auto", "register", "_Thread_local"
     88                        "extern", "static", "auto", "register", "__thread", "_Thread_local"
    8989                );
    9090
    9191                static constexpr auto Qualifiers = make_array<const char*>(
    92                         "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic"
     92                        "const", "restrict", "volatile", "mutex", "_Atomic"
    9393                );
    9494        };
     
    215215                        ++indent;
    216216                        ptrToEnum->base->accept( *this );
    217                         --indent; 
     217                        --indent;
    218218                }
    219219
     
    899899                postprint( node );
    900900
     901                return node;
     902        }
     903
     904        virtual const ast::Expr * visit( const ast::QualifiedNameExpr * node ) override final {
     905                os << "QualifiedNameExpr: " << std::endl;
     906                os << ++indent << "Type: ";
     907                safe_print( node->type_decl );
     908                os << std::endl;
     909                os <<  indent << "Name: " << node->name  << std::endl;
     910                --indent;
     911                postprint( node );
    901912                return node;
    902913        }
     
    16231634// if the wrong size is specified
    16241635constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
    1625 constexpr array<const char*, 5> Printer::Names::StorageClasses;
    1626 constexpr array<const char*, 6> Printer::Names::Qualifiers;
     1636constexpr array<const char*, 6> Printer::Names::StorageClasses;
     1637constexpr array<const char*, 5> Printer::Names::Qualifiers;
    16271638}
  • src/AST/StorageClasses.hpp

    r428adbc r7f6a7c9  
    2424        /// Bitflags for storage classes
    2525        enum {
    26                 Extern      = 1 << 0,
    27                 Static      = 1 << 1,
    28                 Auto        = 1 << 2,
    29                 Register    = 1 << 3,
    30                 ThreadLocal = 1 << 4,
    31                 NumClasses       = 5
     26                Extern         = 1 << 0,
     27                Static         = 1 << 1,
     28                Auto           = 1 << 2,
     29                Register       = 1 << 3,
     30                ThreadLocalGcc = 1 << 4,
     31                ThreadLocalC11 = 1 << 5,
     32                NumClasses          = 6
    3233        };
    3334
     
    3738                        unsigned int val;
    3839                        struct {
    39                                 bool is_extern      : 1;
    40                                 bool is_static      : 1;
    41                                 bool is_auto        : 1;
    42                                 bool is_register    : 1;
    43                                 bool is_threadlocal : 1;
     40                                bool is_extern         : 1;
     41                                bool is_static         : 1;
     42                                bool is_auto           : 1;
     43                                bool is_register       : 1;
     44                                bool is_threadlocalGcc : 1;
     45                                bool is_threadlocalC11 : 1;
    4446                        };
    4547
     
    4850
    4951                constexpr class_flags( unsigned int val = 0 ) : val(val) {}
     52
     53                bool is_threadlocal_any() { return this->is_threadlocalC11 || this->is_threadlocalGcc; }
    5054        };
    5155
  • src/AST/Type.cpp

    r428adbc r7f6a7c9  
    143143TraitInstType::TraitInstType(
    144144        const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
    145 : BaseInstType( b->name, q, move(as) ), base( b ) {}
     145: BaseInstType( b->name, q, std::move(as) ), base( b ) {}
    146146
    147147// --- TypeInstType
     
    149149TypeInstType::TypeInstType( const TypeDecl * b,
    150150        CV::Qualifiers q, std::vector<ptr<Attribute>> && as )
    151 : BaseInstType( b->name, q, move(as) ), base( b ), kind( b->kind ) {}
     151: BaseInstType( b->name, q, std::move(as) ), base( b ), kind( b->kind ) {}
    152152
    153153void TypeInstType::set_base( const TypeDecl * b ) {
     
    161161
    162162TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
    163 : Type( q ), types( move(ts) ), members() {
     163: Type( q ), types( std::move(ts) ), members() {
    164164        // This constructor is awkward. `TupleType` needs to contain objects so that members can be
    165165        // named, but members without initializer nodes end up getting constructors, which breaks
  • src/AST/Type.hpp

    r428adbc r7f6a7c9  
    8383template< enum Node::ref_type ref_t >
    8484void reset_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q = {} ) {
    85         if ( p->qualifiers.val != q.val ) p.get_and_mutate()->qualifiers = q;
     85        if ( p->qualifiers != q ) p.get_and_mutate()->qualifiers = q;
    8686}
    8787
     
    8989template< enum Node::ref_type ref_t >
    9090void add_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q ) {
    91         if ( ( p->qualifiers.val & q.val ) != q.val ) p.get_and_mutate()->qualifiers |= q;
     91        if ( ( p->qualifiers & q ) != q ) p.get_and_mutate()->qualifiers |= q;
    9292}
    9393
     
    9595template< enum Node::ref_type ref_t >
    9696void remove_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q ) {
    97         if ( ( p->qualifiers.val & q.val ) != 0 ) p.get_and_mutate()->qualifiers -= q;
     97        if ( ( p->qualifiers & q ) != 0 ) p.get_and_mutate()->qualifiers -= q;
    9898}
    9999
     
    412412                std::string typeString() const { return std::string("_") + std::to_string(formal_usage) + "_" + std::to_string(expr_id) + "_" + base->name; }
    413413                bool operator==(const TypeEnvKey & other) const { return base == other.base && formal_usage == other.formal_usage && expr_id == other.expr_id; }
    414 
    415414        };
    416415
  • src/AST/TypeEnvironment.hpp

    r428adbc r7f6a7c9  
    5656struct AssertCompare {
    5757        bool operator()( const VariableExpr * d1, const VariableExpr * d2 ) const {
     58                auto kind1 = ast::SymbolTable::getSpecialFunctionKind(d1->var->name);
     59                auto kind2 = ast::SymbolTable::getSpecialFunctionKind(d2->var->name);
     60                // heuristics optimization: force special functions to go last
     61                if (kind1 > kind2) return true;
     62                else if (kind1 < kind2) return false;
     63
    5864                int cmp = d1->var->name.compare( d2->var->name );
    5965                return cmp < 0 || ( cmp == 0 && d1->result < d2->result );
  • src/AST/Visitor.hpp

    r428adbc r7f6a7c9  
    5959    virtual const ast::Expr *             visit( const ast::UntypedExpr          * ) = 0;
    6060    virtual const ast::Expr *             visit( const ast::NameExpr             * ) = 0;
     61    virtual const ast::Expr *             visit( const ast::QualifiedNameExpr    * ) = 0;
    6162    virtual const ast::Expr *             visit( const ast::AddressExpr          * ) = 0;
    6263    virtual const ast::Expr *             visit( const ast::LabelAddressExpr     * ) = 0;
  • src/AST/module.mk

    r428adbc r7f6a7c9  
    2424        AST/Copy.cpp \
    2525        AST/Copy.hpp \
     26        AST/Create.cpp \
     27        AST/Create.hpp \
    2628        AST/CVQualifiers.hpp \
    2729        AST/Decl.cpp \
Note: See TracChangeset for help on using the changeset viewer.