Changeset 62423350 for src/SynTree


Ignore:
Timestamp:
Jun 29, 2017, 5:06:24 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
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, with_gc
Children:
a12d5aa
Parents:
bb1cd95
Message:

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType?.

Location:
src/SynTree
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Constant.cc

    rbb1cd95 r62423350  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // Constant.cc -- 
     7// Constant.cc --
    88//
    99// Author           : Richard C. Bilson
     
    4646}
    4747
     48unsigned long long Constant::get_ival() const {
     49        assertf( safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve ival from non-integer constant." );
     50        return val.ival;
     51}
     52
     53double Constant::get_dval() const {
     54        assertf( ! safe_dynamic_cast<BasicType*>(type)->isInteger(), "Attempt to retrieve dval from integer constant." );
     55        return val.dval;
     56}
     57
    4858void Constant::print( std::ostream &os ) const {
    4959        os << "(" << rep << " " << val.ival;
  • src/SynTree/Constant.h

    rbb1cd95 r62423350  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // Constant.h -- 
     7// Constant.h --
    88//
    99// Author           : Richard C. Bilson
     
    3232        std::string & get_value() { return rep; }
    3333        void set_value( std::string newValue ) { rep = newValue; }
     34        unsigned long long get_ival() const;
     35        double get_dval() const;
    3436
    3537        /// generates a boolean constant of the given bool
  • src/SynTree/Expression.cc

    rbb1cd95 r62423350  
    2121#include <iterator>
    2222
     23#include "Declaration.h"
     24#include "Expression.h"
     25#include "Initializer.h"
     26#include "Statement.h"
    2327#include "Type.h"
    24 #include "Initializer.h"
    25 #include "Expression.h"
    26 #include "Declaration.h"
    27 #include "Statement.h"
    2828#include "TypeSubstitution.h"
     29#include "VarExprReplacer.h"
     30
    2931#include "Common/utility.h"
     32#include "Common/PassVisitor.h"
     33
    3034#include "InitTweak/InitTweak.h"
    3135
     
    681685}
    682686
    683 InitExpr::InitExpr( Expression * expr, Type * type, Designation * designation ) : expr( expr ), designation( designation ) {
    684         set_result( type );
     687InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
     688        set_result( expr->get_result()->clone() );
    685689}
    686690InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
  • src/SynTree/Expression.h

    rbb1cd95 r62423350  
    776776class InitExpr : public Expression {
    777777public:
    778         InitExpr( Expression * expr, Type * type, Designation * designation );
     778        InitExpr( Expression * expr, Designation * designation );
    779779        InitExpr( const InitExpr & other );
    780780        ~InitExpr();
  • src/SynTree/Initializer.cc

    rbb1cd95 r62423350  
    6565
    6666
    67 ListInit::ListInit( const std::list<Initializer*> &initializers, const std::list<Designation *> &designations, bool maybeConstructed )
    68         : Initializer( maybeConstructed ), initializers( initializers ), designations( designations ) {
     67ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
     68        : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
     69                // handle the common case where a ListInit is created without designations by making a list of empty designations with the same length as the initializer
     70                if ( designations.empty() ) {
     71                        for ( auto & i : initializers ) {
     72                                (void)i;
     73                                designations.push_back( new Designation( {} ) );
     74                        }
     75                }
     76                assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%d) and designations (%d)", initializers.size(), designations.size() );
    6977}
    7078
  • src/SynTree/Mutator.cc

    rbb1cd95 r62423350  
    515515        mutateAll( tupleType->get_forall(), *this );
    516516        mutateAll( tupleType->get_types(), *this );
     517        mutateAll( tupleType->get_members(), *this );
    517518        return tupleType;
    518519}
  • src/SynTree/TupleType.cc

    rbb1cd95 r62423350  
    1414//
    1515
     16#include "Declaration.h"
     17#include "Initializer.h"
    1618#include "Type.h"
    1719#include "Common/utility.h"
     20#include "Parser/LinkageSpec.h"
    1821
    1922TupleType::TupleType( const Type::Qualifiers &tq, const std::list< Type * > & types, const std::list< Attribute * > & attributes ) : Type( tq, attributes ), types( types ) {
     23        for ( Type * t : *this ) {
     24                // xxx - this is very awkward. TupleTypes should contain objects so that members can be named, but if they don't have an initializer node then
     25                // they end up getting constructors, which end up being inserted causing problems. This happens because the object decls have to be visited so that
     26                // their types are kept in sync with the types list here. Ultimately, the types list here should be eliminated and perhaps replaced with a list-view
     27                // of the object types list, but I digress. The temporary solution here is to make a ListInit with maybeConstructed = false, that way even when the
     28                // object is visited, it is never constructed. Ultimately, a better solution might be either:
     29                // a) to separate TupleType from its declarations, into TupleDecl and Tuple{Inst?}Type, ala StructDecl and StructInstType
     30                // b) separate initializer nodes better, e.g. add a MaybeConstructed node that is replaced by genInit, rather than what currently exists in a bool
     31                members.push_back( new ObjectDecl( "" , Type::StorageClasses(), LinkageSpec::Cforall, nullptr, t->clone(), new ListInit( {}, {}, false ) ) );
     32        }
    2033}
    2134
    2235TupleType::TupleType( const TupleType& other ) : Type( other ) {
    2336        cloneAll( other.types, types );
     37        cloneAll( other.members, members );
    2438}
    2539
    2640TupleType::~TupleType() {
    2741        deleteAll( types );
     42        deleteAll( members );
    2843}
    2944
  • src/SynTree/Type.h

    rbb1cd95 r62423350  
    481481class TupleType : public Type {
    482482  public:
    483         TupleType( const Type::Qualifiers & tq, const std::list< Type * > & types = std::list< Type * >(), const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
     483        TupleType( const Type::Qualifiers & tq, const std::list< Type * > & types, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
    484484        TupleType( const TupleType& );
    485485        virtual ~TupleType();
     
    488488        typedef value_type::iterator iterator;
    489489
    490         std::list<Type*>& get_types() { return types; }
     490        std::list<Type *> & get_types() { return types; }
    491491        virtual unsigned size() const { return types.size(); };
     492
     493        // For now, this is entirely synthetic -- tuple types always have unnamed members.
     494        // Eventually, we may allow named tuples, in which case members should subsume types
     495        std::list<Declaration *> & get_members() { return members; }
    492496
    493497        iterator begin() { return types.begin(); }
     
    506510        virtual void print( std::ostream & os, int indent = 0 ) const;
    507511  private:
    508         std::list<Type*> types;
     512        std::list<Type *> types;
     513        std::list<Declaration *> members;
    509514};
    510515
  • src/SynTree/VarExprReplacer.cc

    rbb1cd95 r62423350  
    1414//
    1515
     16#include "Declaration.h"
    1617#include "Expression.h"
    1718#include "VarExprReplacer.h"
    1819
    19 VarExprReplacer::VarExprReplacer( const DeclMap & declMap ) : declMap( declMap ) {}
     20VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {}
    2021
    2122// replace variable with new node from decl map
    2223void VarExprReplacer::visit( VariableExpr * varExpr ) {
    23   // 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)
    24   if ( declMap.count( varExpr->get_var() ) ) {
    25     varExpr->set_var( declMap.at( varExpr->get_var() ) );
    26   }
     24        // 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)
     25        if ( declMap.count( varExpr->get_var() ) ) {
     26                if ( debug ) {
     27                        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;
     28                }
     29                varExpr->set_var( declMap.at( varExpr->get_var() ) );
     30        }
    2731}
  • src/SynTree/VarExprReplacer.h

    rbb1cd95 r62423350  
    2727private:
    2828        const DeclMap & declMap;
     29  bool debug;
    2930public:
    30         VarExprReplacer( const DeclMap & declMap );
     31        VarExprReplacer( const DeclMap & declMap, bool debug = false );
    3132
    3233        // replace variable with new node from decl map
  • src/SynTree/Visitor.cc

    rbb1cd95 r62423350  
    407407        acceptAll( tupleType->get_forall(), *this );
    408408        acceptAll( tupleType->get_types(), *this );
     409        acceptAll( tupleType->get_members(), *this );
    409410}
    410411
Note: See TracChangeset for help on using the changeset viewer.