source: src/SymTab/Autogen.h@ ea5023c

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 with_gc
Last change on this file since ea5023c was 33218c6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 8.7 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// Autogen.h --
8//
9// Author : Rob Schluntz
10// Created On : Sun May 17 21:53:34 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:50:25 2017
13// Update Count : 15
14//
15
16#pragma once
17
18#include <cassert> // for assert
19#include <iterator> // for back_insert_iterator, back_inserter
20#include <list> // for list
21#include <string> // for string, operator==
22
23#include "Common/UniqueName.h" // for UniqueName
24#include "InitTweak/InitTweak.h" // for InitExpander
25#include "Parser/LinkageSpec.h" // for C
26#include "SynTree/Constant.h" // for Constant
27#include "SynTree/Declaration.h" // for ObjectDecl, Declaration (ptr only)
28#include "SynTree/Expression.h" // for UntypedExpr, NameExpr, VariableExpr
29#include "SynTree/Initializer.h" // for SingleInit
30#include "SynTree/Label.h" // for Label, noLabels
31#include "SynTree/Statement.h" // for Statement (ptr only), CompoundStmt
32#include "SynTree/Type.h" // for Type, ArrayType, Type::Qualifiers
33
34namespace SymTab {
35 /// Generates assignment operators, constructors, and destructor for aggregate types as required
36 void autogenerateRoutines( std::list< Declaration * > &translationUnit );
37
38 /// returns true if obj's name is the empty string and it has a bitfield width
39 bool isUnnamedBitfield( ObjectDecl * obj );
40
41 /// size_t type - set when size_t typedef is seen. Useful in a few places,
42 /// such as in determining array dimension type
43 extern Type * SizeType;
44
45 /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
46 template< typename OutputIterator >
47 Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
48
49 /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
50 /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
51 template< typename OutputIterator >
52 Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false ) {
53 // want to be able to generate assignment, ctor, and dtor generically,
54 // so fname is either ?=?, ?{}, or ^?{}
55 UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
56
57 // do something special for unnamed members
58 dstParam = new AddressExpr( dstParam );
59 if ( addCast ) {
60 // cast to T* with qualifiers removed, so that qualified objects can be constructed
61 // and destructed with the same functions as non-qualified objects.
62 // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
63 // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
64 // remove lvalue as a qualifier, this can change to
65 // type->get_qualifiers() = Type::Qualifiers();
66 assert( type );
67 Type * castType = type->clone();
68 castType->get_qualifiers() -= Type::Qualifiers( Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
69 castType->set_lvalue( true ); // xxx - might not need this
70 dstParam = new CastExpr( dstParam, new PointerType( Type::Qualifiers(), castType ) );
71 }
72 fExpr->get_args().push_back( dstParam );
73
74 Statement * listInit = srcParam.buildListInit( fExpr );
75
76 std::list< Expression * > args = *++srcParam;
77 fExpr->get_args().splice( fExpr->get_args().end(), args );
78
79 *out++ = new ExprStmt( noLabels, fExpr );
80
81 srcParam.clearArrayIndices();
82
83 return listInit;
84 }
85
86 /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
87 /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
88 template< typename OutputIterator >
89 void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
90 static UniqueName indexName( "_index" );
91
92 // for a flexible array member nothing is done -- user must define own assignment
93 if ( ! array->get_dimension() ) return ;
94
95 Expression * begin, * end, * update, * cmp;
96 if ( forward ) {
97 // generate: for ( int i = 0; i < N; ++i )
98 begin = new ConstantExpr( Constant::from_int( 0 ) );
99 end = array->get_dimension()->clone();
100 cmp = new NameExpr( "?<?" );
101 update = new NameExpr( "++?" );
102 } else {
103 // generate: for ( int i = N-1; i >= 0; --i )
104 begin = new UntypedExpr( new NameExpr( "?-?" ) );
105 ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
106 ((UntypedExpr*)begin)->get_args().push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
107 end = new ConstantExpr( Constant::from_int( 0 ) );
108 cmp = new NameExpr( "?>=?" );
109 update = new NameExpr( "--?" );
110 }
111
112 ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
113
114 UntypedExpr *cond = new UntypedExpr( cmp );
115 cond->get_args().push_back( new VariableExpr( index ) );
116 cond->get_args().push_back( end );
117
118 UntypedExpr *inc = new UntypedExpr( update );
119 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
120
121 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
122 dstIndex->get_args().push_back( dstParam );
123 dstIndex->get_args().push_back( new VariableExpr( index ) );
124 dstParam = dstIndex;
125
126 // srcParam must keep track of the array indices to build the
127 // source parameter and/or array list initializer
128 srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
129
130 // for stmt's body, eventually containing call
131 CompoundStmt * body = new CompoundStmt( noLabels );
132 Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), addCast, forward );
133
134 // block containing for stmt and index variable
135 std::list<Statement *> initList;
136 CompoundStmt * block = new CompoundStmt( noLabels );
137 block->get_kids().push_back( new DeclStmt( noLabels, index ) );
138 if ( listInit ) block->get_kids().push_back( listInit );
139 block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
140
141 *out++ = block;
142 }
143
144 template< typename OutputIterator >
145 Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
146 if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
147 genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
148 return 0;
149 } else {
150 return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
151 }
152 }
153
154 /// inserts into out a generated call expression to function fname with arguments dstParam
155 /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
156 /// object being constructed. The function wraps constructor and destructor calls in an
157 /// ImplicitCtorDtorStmt node.
158 template< typename OutputIterator >
159 void genImplicitCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
160 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
161 assert( obj );
162 // unnamed bit fields are not copied as they cannot be accessed
163 if ( isUnnamedBitfield( obj ) ) return;
164
165 bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) );
166 std::list< Statement * > stmts;
167 genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), addCast, forward );
168
169 // currently genCall should produce at most one element, but if that changes then the next line needs to be updated to grab the statement which contains the call
170 assert( stmts.size() <= 1 );
171 if ( stmts.size() == 1 ) {
172 Statement * callStmt = stmts.front();
173 if ( addCast ) {
174 // implicitly generated ctor/dtor calls should be wrapped
175 // so that later passes are aware they were generated.
176 // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
177 // because this causes the address to be taken at codegen, which is illegal in C.
178 callStmt = new ImplicitCtorDtorStmt( callStmt );
179 }
180 *out++ = callStmt;
181 }
182 }
183} // namespace SymTab
184
185// Local Variables: //
186// tab-width: 4 //
187// mode: c++ //
188// compile-command: "make install" //
189// End: //
190
Note: See TracBrowser for help on using the repository browser.