Changeset 70f89d00


Ignore:
Timestamp:
May 30, 2016, 12:51:22 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
f1b1e4c
Parents:
677c1be
Message:

function scoped const objects can be constructed, add missing copy constructors to Initializer.cc

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    r677c1be r70f89d00  
    143143                if ( tryConstruct( objDecl ) ) {
    144144                        if ( inFunction ) {
     145                                // remove qualifiers so that const objects can be initialized, and attach the
     146                                // qualifiers to ConstructorInit so that they can be replaced after resolving
     147                                Type * type = objDecl->get_type();
     148                                Type::Qualifiers qualifiers = type->get_qualifiers();
     149                                type->get_qualifiers() = Type::Qualifiers();
     150
    145151                                if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
    146152                                        // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array
     
    165171                                                assert( dtor.size() == 1 );
    166172
    167                                                 objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
     173                                                objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init(), objDecl, qualifiers ) );
    168174                                        } else {
    169175                                                // array came with an initializer list: initialize each element
     
    185191                                        ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
    186192                                        ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
    187                                         objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init() ) );
     193                                        objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init(), objDecl, qualifiers ) );
    188194                                }
    189195                        }
  • src/InitTweak/InitTweak.cc

    r677c1be r70f89d00  
    8383    }
    8484  }
     85
     86  namespace {
     87    template<typename CallExpr>
     88    std::string funcName( CallExpr * expr ) {
     89      Expression * func = expr->get_function();
     90      if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
     91        return nameExpr->get_name();
     92      } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
     93        return varExpr->get_var()->get_name();
     94      } else {
     95        assert( false && "Unexpected expression type being called as a function in call expression" );
     96      }
     97    }
     98  }
     99
     100  std::string getFunctionName( Expression * expr ) {
     101    if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
     102      return funcName( appExpr );
     103    } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
     104      return funcName( untypedExpr );
     105    } else {
     106      assert( false && "Unexpected expression type passed to getFunctionName" );
     107    }
     108  }
    85109}
  • src/InitTweak/InitTweak.h

    r677c1be r70f89d00  
    3939  /// Currently has assertions that make it less than fully general.
    4040  bool isInstrinsicSingleArgCallStmt( Statement * expr );
     41
     42  /// returns the name of the function being called
     43  std::string getFunctionName(Expression * expr);
    4144} // namespace
    4245
  • src/ResolvExpr/AlternativeFinder.cc

    r677c1be r70f89d00  
    3939#include "Tuples/NameMatcher.h"
    4040#include "Common/utility.h"
     41#include "InitTweak/InitTweak.h"
    4142
    4243extern bool resolvep;
     
    546547
    547548                {
    548                         NameExpr *fname = 0;;
    549                         if ( ( fname = dynamic_cast<NameExpr *>( untypedExpr->get_function()))
    550                                  && ( fname->get_name() == std::string("&&")) ) {
     549                        std::string fname = InitTweak::getFunctionName( untypedExpr );
     550                        if ( fname == "&&" ) {
    551551                                VoidType v = Type::Qualifiers();                // resolve to type void *
    552552                                PointerType pt( Type::Qualifiers(), v.clone() );
  • src/ResolvExpr/Resolver.cc

    r677c1be r70f89d00  
    492492                } catch ( SemanticError ) {
    493493                        // no alternatives for the constructor initializer - fallback on C-style initializer
    494                         // xxx- not sure if this makes a ton of sense - should maybe never be able to have this situation?
     494                        // xxx - not sure if this makes a ton of sense - should maybe never be able to have this situation?
     495
     496                        // reset type qualifiers
     497                        ctorInit->get_object()->get_type()->get_qualifiers() = ctorInit->get_qualifiers();
    495498                        fallbackInit( ctorInit );
    496499                        return;
    497500                }
     501                // reset type qualifiers
     502                ctorInit->get_object()->get_type()->get_qualifiers() = ctorInit->get_qualifiers();
    498503
    499504                // found a constructor - can get rid of C-style initializer
  • src/SynTree/Initializer.cc

    r677c1be r70f89d00  
    2020
    2121Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {}
     22Initializer::Initializer( const Initializer & other ) : maybeConstructed( other.maybeConstructed ) {
     23}
     24
    2225
    2326Initializer::~Initializer() {}
     
    3942}
    4043
    41 SingleInit::~SingleInit() {}
    42 
    43 SingleInit *SingleInit::clone() const { return new SingleInit( *this); }
     44SingleInit::~SingleInit() {
     45        deleteAll(designators);
     46}
    4447
    4548void SingleInit::print( std::ostream &os, int indent ) {
     
    5861
    5962ListInit::ListInit( const std::list<Initializer*> &_initializers, const std::list<Expression *> &_designators, bool maybeConstructed )
    60         : Initializer( maybeConstructed), initializers( _initializers ), designators( _designators ) {
     63        : Initializer( maybeConstructed ), initializers( _initializers ), designators( _designators ) {
    6164}
    6265
    63 ListInit::~ListInit() {}
    64 
    65 ListInit *ListInit::clone() const {
    66         return new ListInit( *this );
     66ListInit::~ListInit() {
     67        deleteAll( initializers );
     68        deleteAll( designators );
    6769}
    6870
     
    8486
    8587
    86 ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {}
     88ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init, ObjectDecl * object, Type::Qualifiers qualifiers ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ), object( object ), qualifiers( qualifiers ) {}
     89ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ), object( other.object ), qualifiers( other.qualifiers ) {
     90}
     91
    8792ConstructorInit::~ConstructorInit() {
    8893        delete ctor;
     94        delete dtor;
    8995        delete init;
    90 }
    91 
    92 ConstructorInit *ConstructorInit::clone() const {
    93         return new ConstructorInit( *this );
    9496}
    9597
  • src/SynTree/Initializer.h

    r677c1be r70f89d00  
    2020#include "Visitor.h"
    2121#include "Mutator.h"
     22#include "Type.h"
    2223
    2324#include <cassert>
     
    2829        //      Initializer( std::string _name = std::string(""), int _pos = 0 );
    2930        Initializer( bool maybeConstructed );
     31        Initializer( const Initializer & other );
    3032        virtual ~Initializer();
    3133
     
    6870        std::list<Expression *> &get_designators() { return designators; }
    6971
    70         virtual SingleInit *clone() const;
     72        virtual SingleInit *clone() const { return new SingleInit( *this); }
    7173        virtual void accept( Visitor &v ) { v.visit( this ); }
    7274        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
     
    9496        std::list<Initializer*>::iterator end_initializers() { return initializers.end(); }
    9597
    96         virtual ListInit *clone() const;
     98        virtual ListInit *clone() const { return new ListInit( *this ); }
    9799        virtual void accept( Visitor &v ) { v.visit( this ); }
    98100        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
     
    107109class ConstructorInit : public Initializer {
    108110  public:
    109         ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
     111        ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init, ObjectDecl * objectDecl, Type::Qualifiers qualifiers );
     112        ConstructorInit( const ConstructorInit &other );
    110113        virtual ~ConstructorInit();
    111114
     
    116119        void set_init( Initializer * newValue ) { init = newValue; }
    117120        Initializer * get_init() const { return init; }
     121        void set_object( ObjectDecl * newValue ) { object = newValue; }
     122        ObjectDecl * get_object() const { return object; }
     123        void set_qualifiers( Type::Qualifiers newValue ) { qualifiers = newValue; }
     124        Type::Qualifiers get_qualifiers() { return qualifiers; }
    118125
    119         virtual ConstructorInit *clone() const;
     126        ConstructorInit *clone() const { return new ConstructorInit( *this ); }
    120127        virtual void accept( Visitor &v ) { v.visit( this ); }
    121128        virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
     
    128135        // if an appropriate constructor definition is not found by the resolver
    129136        Initializer * init;
     137        // Non-owned pointer back to the object being constructed
     138        ObjectDecl * object;
     139        // to construct const objects, need to first remove type qualifiers, then resolve
     140        // then add qualifiers back onto object
     141        Type::Qualifiers qualifiers;
    130142};
    131143
Note: See TracChangeset for help on using the changeset viewer.