source: src/Tuples/TupleExpansion.cc@ 12bc63a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 12bc63a was aa8f9df, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'replace-results-list' into tuples

Conflicts:

src/ResolvExpr/AlternativeFinder.cc
src/SymTab/Indexer.cc
src/SynTree/Mutator.cc
src/SynTree/Visitor.cc
src/Tuples/TupleAssignment.cc
src/Tuples/TupleAssignment.h

  • Property mode set to 100644
File size: 2.9 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 "SymTab/Mangler.h"
26#include "Common/ScopedMap.h"
27
28namespace 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
Note: See TracBrowser for help on using the repository browser.