source: src/Tuples/TupleExpansion.cc @ aefcc3b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since aefcc3b was aefcc3b, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

major change to instantiateFunction to match arguments against the formal parameter's structure rather than the reverse

  • Property mode set to 100644
File size: 6.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TupleAssignment.cc --
8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon May 18 15:02:53 2015
13// Update Count     : 2
14//
15
16#include <iterator>
17#include <iostream>
18#include <cassert>
19#include "Tuples.h"
20#include "GenPoly/DeclMutator.h"
21#include "SynTree/Mutator.h"
22#include "SynTree/Statement.h"
23#include "SynTree/Declaration.h"
24#include "SynTree/Type.h"
25#include "SynTree/Expression.h"
26#include "SynTree/Initializer.h"
27#include "SymTab/Mangler.h"
28#include "Common/ScopedMap.h"
29
30namespace Tuples {
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                };
38
39                class TupleAssignExpander : public Mutator {
40                public:
41                        typedef Mutator Parent;
42                        virtual Expression * mutate( TupleAssignExpr * tupleExpr );
43                };
44
45                class TupleTypeReplacer : public GenPoly::DeclMutator {
46                  public:
47                        typedef GenPoly::DeclMutator Parent;
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        }
73
74        void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
75                UniqueExprExpander unqExpander;
76                unqExpander.mutateDeclarationList( translationUnit );
77        }
78
79        void expandTuples( std::list< Declaration * > & translationUnit ) {
80                TupleAssignExpander assnExpander;
81                mutateAll( translationUnit, assnExpander );
82
83                TupleTypeReplacer replacer;
84                replacer.mutateDeclarationList( translationUnit );
85
86                TupleIndexExpander idxExpander;
87                mutateAll( translationUnit, idxExpander );
88
89                TupleExprExpander exprExpander;
90                mutateAll( translationUnit, exprExpander );
91        }
92
93        Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
94                static UniqueName tempNamer( "_unq_expr_" );
95                unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
96                if ( ! decls.count( unqExpr->get_expr() ) ) {
97                        // 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.
98                        ObjectDecl * decl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, unqExpr->get_result()->clone(), new SingleInit( unqExpr->get_expr()->clone() ) );
99                        decls[unqExpr->get_expr()] = decl;
100                        addDeclaration( decl );
101                }
102                return new VariableExpr( decls[unqExpr->get_expr()] );
103        }
104
105        Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
106                // xxx - Parent::mutate?
107                CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
108                std::list< Statement * > & stmts = compoundStmt->get_kids();
109                for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
110                        stmts.push_back( new DeclStmt( noLabels, obj ) );
111                }
112                TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
113                assert( tupleExpr->get_result() );
114                stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
115                assnExpr->get_tempDecls().clear();
116                assnExpr->get_assigns().clear();
117                delete assnExpr;
118                return new StmtExpr( compoundStmt );
119        }
120
121        Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
122                std::string mangleName = SymTab::Mangler::mangleType( tupleType );
123                TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
124                if ( ! typeMap.count( mangleName ) ) {
125                        // generate struct type to replace tuple type
126                        StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
127                        decl->set_body( true );
128                        int cnt = 0;
129                        for ( Type * t : *newType ) {
130                                decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
131                        }
132                        typeMap[mangleName] = decl;
133                        addDeclaration( decl );
134                }
135                Type::Qualifiers qualifiers = newType->get_qualifiers();
136                delete newType;
137                return new StructInstType( qualifiers, typeMap[mangleName] );
138        }
139
140        Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
141                Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
142                assert( tuple );
143                tupleExpr->set_tuple( nullptr );
144                unsigned int idx = tupleExpr->get_index();
145                delete tupleExpr;
146
147                StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
148                StructDecl * structDecl = type->get_baseStruct();
149                assert( structDecl->get_members().size() > idx );
150                Declaration * member = *std::next(structDecl->get_members().begin(), idx);
151                return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
152        }
153
154        Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
155                assert( tupleExpr->get_result() );
156                std::list< Initializer * > inits;
157                for ( Expression * expr : tupleExpr->get_exprs() ) {
158                        inits.push_back( new SingleInit( expr ) );
159                }
160                return new CompoundLiteralExpr( tupleExpr->get_result(), new ListInit( inits ) );
161        }
162
163        TupleType * makeTupleType( const std::list< Expression * > & exprs ) {
164                TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, false) );
165                Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
166                for ( Expression * expr : exprs ) {
167                        assert( expr->get_result() );
168                        Type * type = expr->get_result()->clone();
169                        tupleType->get_types().push_back( type );
170                        qualifiers &= type->get_qualifiers();
171                } // for
172                return tupleType;
173        }
174} // namespace Tuples
175
176// Local Variables: //
177// tab-width: 4 //
178// mode: c++ //
179// compile-command: "make install" //
180// End: //
181
Note: See TracBrowser for help on using the repository browser.