Changeset bfd4974


Ignore:
Timestamp:
Oct 19, 2017, 11:13:12 AM (4 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
21ea170
Parents:
189d800
git-author:
Rob Schluntz <rschlunt@…> (10/12/17 15:50:44)
git-committer:
Rob Schluntz <rschlunt@…> (10/19/17 11:13:12)
Message:

Refactor ManagedTypes? code from CtorDtor?

Location:
src/InitTweak
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    r189d800 rbfd4974  
    8585                // should not have a ConstructorInit generated.
    8686
    87                 bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed
    88                 bool isManaged( Type * type ) const; // determine if type is managed
    89                 void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor
    90                 GenPoly::ScopedSet< std::string > managedTypes;
     87                ManagedTypes managedTypes;
    9188                bool inFunction = false;
    9289        };
     
    204201        }
    205202
    206         bool CtorDtor::isManaged( Type * type ) const {
     203        bool ManagedTypes::isManaged( Type * type ) const {
    207204                // references are never constructed
    208205                if ( dynamic_cast< ReferenceType * >( type ) ) return false;
     
    220217        }
    221218
    222         bool CtorDtor::isManaged( ObjectDecl * objDecl ) const {
     219        bool ManagedTypes::isManaged( ObjectDecl * objDecl ) const {
    223220                Type * type = objDecl->get_type();
    224221                while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
     
    228225        }
    229226
    230         void CtorDtor::handleDWT( DeclarationWithType * dwt ) {
     227        void ManagedTypes::handleDWT( DeclarationWithType * dwt ) {
    231228                // if this function is a user-defined constructor or destructor, mark down the type as "managed"
    232229                if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && CodeGen::isCtorDtor( dwt->get_name() ) ) {
     
    238235                }
    239236        }
     237
     238        void ManagedTypes::handleStruct( StructDecl * aggregateDecl ) {
     239                // don't construct members, but need to take note if there is a managed member,
     240                // because that means that this type is also managed
     241                for ( Declaration * member : aggregateDecl->get_members() ) {
     242                        if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
     243                                if ( isManaged( field ) ) {
     244                                        StructInstType inst( Type::Qualifiers(), aggregateDecl );
     245                                        managedTypes.insert( SymTab::Mangler::mangle( &inst ) );
     246                                        break;
     247                                }
     248                        }
     249                }
     250        }
     251
     252        void ManagedTypes::beginScope() { managedTypes.beginScope(); }
     253        void ManagedTypes::endScope() { managedTypes.endScope(); }
    240254
    241255        ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg ) {
     
    282296
    283297        void CtorDtor::previsit( ObjectDecl * objDecl ) {
    284                 handleDWT( objDecl );
     298                managedTypes.handleDWT( objDecl );
    285299                // hands off if @=, extern, builtin, etc.
    286300                // even if unmanaged, try to construct global or static if initializer is not constexpr, since this is not legal C
    287                 if ( tryConstruct( objDecl ) && ( isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) {
     301                if ( tryConstruct( objDecl ) && ( managedTypes.isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) {
    288302                        // constructed objects cannot be designated
    289303                        if ( isDesignated( objDecl->get_init() ) ) throw SemanticError( "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.\n", objDecl );
     
    300314                inFunction = true;
    301315
    302                 handleDWT( functionDecl );
     316                managedTypes.handleDWT( functionDecl );
    303317
    304318                GuardScope( managedTypes );
     
    306320                for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) {
    307321                        for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) {
    308                                 handleDWT( assertion );
     322                                managedTypes.handleDWT( assertion );
    309323                        }
    310324                }
     
    316330                visit_children = false; // do not try to construct and destruct aggregate members
    317331
    318                 // don't construct members, but need to take note if there is a managed member,
    319                 // because that means that this type is also managed
    320                 for ( Declaration * member : aggregateDecl->get_members() ) {
    321                         if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
    322                                 if ( isManaged( field ) ) {
    323                                         StructInstType inst( Type::Qualifiers(), aggregateDecl );
    324                                         managedTypes.insert( SymTab::Mangler::mangle( &inst ) );
    325                                         break;
    326                                 }
    327                         }
    328                 }
     332                managedTypes.handleStruct( aggregateDecl );
    329333        }
    330334
  • src/InitTweak/GenInit.h

    r189d800 rbfd4974  
    1616#pragma once
    1717
    18 #include <list>               // for list
    19 #include <string>             // for string
     18#include <list>                // for list
     19#include <string>              // for string
    2020
    21 #include "SynTree/SynTree.h"  // for Visitor Nodes
     21#include "SynTree/SynTree.h"   // for Visitor Nodes
     22
     23#include "GenPoly/ScopedSet.h" // for ScopedSet
    2224
    2325namespace InitTweak {
     
    3335        /// creates an appropriate ConstructorInit node which contains a constructor, destructor, and C-initializer
    3436        ConstructorInit * genCtorInit( ObjectDecl * objDecl );
     37
     38        class ManagedTypes {
     39        public:
     40                bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed
     41                bool isManaged( Type * type ) const; // determine if type is managed
     42
     43                void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor
     44                void handleStruct( StructDecl * aggregateDecl ); // add type to managed if child is managed
     45
     46                void beginScope();
     47                void endScope();
     48        private:
     49                GenPoly::ScopedSet< std::string > managedTypes;
     50        };
    3551} // namespace
    3652
Note: See TracChangeset for help on using the changeset viewer.