Ignore:
Timestamp:
Sep 17, 2016, 8:27:51 AM (8 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:
8c49c0e
Parents:
12bc63a
Message:

expand TupleExpr? and TupleIndexExpr?, add UniqueExpr?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Tuples/TupleExpansion.cc

    r12bc63a r3c13c03  
    2323#include "SynTree/Declaration.h"
    2424#include "SynTree/Type.h"
     25#include "SynTree/Expression.h"
     26#include "SynTree/Initializer.h"
    2527#include "SymTab/Mangler.h"
    2628#include "Common/ScopedMap.h"
    2729
    2830namespace Tuples {
    29         class TupleAssignExpander : public Mutator {
    30         public:
    31                 virtual Expression * mutate( TupleAssignExpr * tupleExpr );
    32         };
     31        namespace {
     32                class UniqueExprExpander : public GenPoly::DeclMutator {
     33                public:
     34                        typedef GenPoly::DeclMutator Parent;
     35                        virtual Expression * mutate( UniqueExpr * unqExpr );
     36                        std::map< Expression *, ObjectDecl * > decls;
     37                };
    3338
    34         class TupleTypeReplacer : public GenPoly::DeclMutator {
    35           public:
    36                 typedef GenPoly::DeclMutator Parent;
     39                class TupleAssignExpander : public Mutator {
     40                public:
     41                        typedef Mutator Parent;
     42                        virtual Expression * mutate( TupleAssignExpr * tupleExpr );
     43                };
    3744
    38                 virtual Type * mutate( TupleType * tupleType );
     45                class TupleTypeReplacer : public GenPoly::DeclMutator {
     46                  public:
     47                        typedef GenPoly::DeclMutator Parent;
    3948
    40                 virtual CompoundStmt * mutate( CompoundStmt * stmt ) {
    41                         typeMap.beginScope();
    42                         stmt = Parent::mutate( stmt );
    43                         typeMap.endScope();
    44                         return stmt;
    45                 }
    46           private:
    47                 ScopedMap< std::string, StructDecl * > typeMap;
    48         };
     49                        virtual Type * mutate( TupleType * tupleType );
     50
     51                        virtual CompoundStmt * mutate( CompoundStmt * stmt ) {
     52                                typeMap.beginScope();
     53                                stmt = Parent::mutate( stmt );
     54                                typeMap.endScope();
     55                                return stmt;
     56                        }
     57                  private:
     58                        ScopedMap< std::string, StructDecl * > typeMap;
     59                };
     60
     61                class TupleIndexExpander : public Mutator {
     62                public:
     63                        typedef Mutator Parent;
     64                        virtual Expression * mutate( TupleIndexExpr * tupleExpr );
     65                };
     66
     67                class TupleExprExpander : public Mutator {
     68                public:
     69                        typedef Mutator Parent;
     70                        virtual Expression * mutate( TupleExpr * tupleExpr );
     71                };
     72        }
    4973
    5074        void expandTuples( std::list< Declaration * > & translationUnit ) {
    51                 TupleAssignExpander expander;
    52                 mutateAll( translationUnit, expander );
     75                UniqueExprExpander unqExpander;
     76                unqExpander.mutateDeclarationList( translationUnit );
     77
     78                TupleAssignExpander assnExpander;
     79                mutateAll( translationUnit, assnExpander );
    5380
    5481                TupleTypeReplacer replacer;
    5582                replacer.mutateDeclarationList( translationUnit );
     83
     84                TupleIndexExpander idxExpander;
     85                mutateAll( translationUnit, idxExpander );
     86
     87                TupleExprExpander exprExpander;
     88                mutateAll( translationUnit, exprExpander );
    5689        }
    5790
    58         Expression * TupleAssignExpander::mutate( TupleAssignExpr * tupleExpr ) {
     91        Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
     92                static UniqueName tempNamer( "_unq_expr_" );
     93                unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
     94                if ( ! decls.count( unqExpr->get_expr() ) ) {
     95                        // xxx - it's possible (likely?) that expressions can appear in the wrong order because of this. Need to ensure they're placed in the correct location.
     96                        ObjectDecl * decl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, unqExpr->get_result()->clone(), new SingleInit( unqExpr->get_expr()->clone() ) );
     97                        decls[unqExpr->get_expr()] = decl;
     98                        addDeclaration( decl );
     99                }
     100                return new VariableExpr( decls[unqExpr->get_expr()] );
     101        }
     102
     103        Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
     104                // xxx - Parent::mutate?
    59105                CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
    60106                std::list< Statement * > & stmts = compoundStmt->get_kids();
    61                 for ( ObjectDecl * obj : tupleExpr->get_tempDecls() ) {
     107                for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
    62108                        stmts.push_back( new DeclStmt( noLabels, obj ) );
    63109                }
    64                 for ( Expression * assign : tupleExpr->get_assigns() ) {
    65                         stmts.push_back( new ExprStmt( noLabels, assign ) );
    66                 }
    67                 tupleExpr->get_tempDecls().clear();
    68                 tupleExpr->get_assigns().clear();
    69                 delete tupleExpr;
     110                TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
     111                assert( tupleExpr->get_result() );
     112                stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
     113                assnExpr->get_tempDecls().clear();
     114                assnExpr->get_assigns().clear();
     115                delete assnExpr;
    70116                return new StmtExpr( compoundStmt );
    71117        }
     
    90136        }
    91137
     138        Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
     139                Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
     140                assert( tuple );
     141                tupleExpr->set_tuple( nullptr );
     142                unsigned int idx = tupleExpr->get_index();
     143                delete tupleExpr;
     144
     145                StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
     146                StructDecl * structDecl = type->get_baseStruct();
     147                assert( structDecl->get_members().size() > idx );
     148                Declaration * member = *std::next(structDecl->get_members().begin(), idx);
     149                return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
     150        }
     151
     152        Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
     153                assert( tupleExpr->get_result() );
     154                std::list< Initializer * > inits;
     155                for ( Expression * expr : tupleExpr->get_exprs() ) {
     156                        inits.push_back( new SingleInit( expr ) );
     157                }
     158                return new CompoundLiteralExpr( tupleExpr->get_result(), new ListInit( inits ) );
     159        }
     160
     161        TupleType * makeTupleType( const std::list< Expression * > & exprs ) {
     162                TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, false) );
     163                Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
     164                for ( Expression * expr : exprs ) {
     165                        assert( expr->get_result() );
     166                        Type * type = expr->get_result()->clone();
     167                        tupleType->get_types().push_back( type );
     168                        qualifiers &= type->get_qualifiers();
     169                } // for
     170                return tupleType;
     171        }
    92172} // namespace Tuples
    93173
Note: See TracChangeset for help on using the changeset viewer.