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 "SymTab/Mangler.h" |
---|
26 | #include "Common/ScopedMap.h" |
---|
27 | |
---|
28 | namespace Tuples { |
---|
29 | class TupleAssignExpander : public Mutator { |
---|
30 | public: |
---|
31 | virtual Expression * mutate( TupleAssignExpr * tupleExpr ); |
---|
32 | }; |
---|
33 | |
---|
34 | class TupleTypeReplacer : public GenPoly::DeclMutator { |
---|
35 | public: |
---|
36 | typedef GenPoly::DeclMutator Parent; |
---|
37 | |
---|
38 | virtual Type * mutate( TupleType * tupleType ); |
---|
39 | |
---|
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 | |
---|
50 | void expandTuples( std::list< Declaration * > & translationUnit ) { |
---|
51 | TupleAssignExpander expander; |
---|
52 | mutateAll( translationUnit, expander ); |
---|
53 | |
---|
54 | TupleTypeReplacer replacer; |
---|
55 | replacer.mutateDeclarationList( translationUnit ); |
---|
56 | } |
---|
57 | |
---|
58 | Expression * TupleAssignExpander::mutate( TupleAssignExpr * tupleExpr ) { |
---|
59 | CompoundStmt * compoundStmt = new CompoundStmt( noLabels ); |
---|
60 | std::list< Statement * > & stmts = compoundStmt->get_kids(); |
---|
61 | for ( ObjectDecl * obj : tupleExpr->get_tempDecls() ) { |
---|
62 | stmts.push_back( new DeclStmt( noLabels, obj ) ); |
---|
63 | } |
---|
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; |
---|
70 | return new StmtExpr( compoundStmt ); |
---|
71 | } |
---|
72 | |
---|
73 | Type * TupleTypeReplacer::mutate( TupleType * tupleType ) { |
---|
74 | std::string mangleName = SymTab::Mangler::mangleType( tupleType ); |
---|
75 | TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) ); |
---|
76 | if ( ! typeMap.count( mangleName ) ) { |
---|
77 | // generate struct type to replace tuple type |
---|
78 | StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName ); |
---|
79 | decl->set_body( true ); |
---|
80 | int cnt = 0; |
---|
81 | for ( Type * t : *newType ) { |
---|
82 | decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) ); |
---|
83 | } |
---|
84 | typeMap[mangleName] = decl; |
---|
85 | addDeclaration( decl ); |
---|
86 | } |
---|
87 | Type::Qualifiers qualifiers = newType->get_qualifiers(); |
---|
88 | delete newType; |
---|
89 | return new StructInstType( qualifiers, typeMap[mangleName] ); |
---|
90 | } |
---|
91 | |
---|
92 | } // namespace Tuples |
---|
93 | |
---|
94 | // Local Variables: // |
---|
95 | // tab-width: 4 // |
---|
96 | // mode: c++ // |
---|
97 | // compile-command: "make install" // |
---|
98 | // End: // |
---|
99 | |
---|