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 <string> // for string
|
---|
20 |
|
---|
21 | #include "CodeGen/OperatorTable.h"
|
---|
22 | #include "Common/UniqueName.h" // for UniqueName
|
---|
23 | #include "InitTweak/InitTweak.h" // for InitExpander
|
---|
24 | #include "SynTree/Constant.h" // for Constant
|
---|
25 | #include "SynTree/Declaration.h" // for DeclarationWithType, ObjectDecl
|
---|
26 | #include "SynTree/Expression.h" // for NameExpr, ConstantExpr, UntypedExpr...
|
---|
27 | #include "SynTree/Type.h" // for Type, ArrayType, Type::Qualifiers
|
---|
28 |
|
---|
29 | class CompoundStmt;
|
---|
30 | class Statement;
|
---|
31 |
|
---|
32 | namespace SymTab {
|
---|
33 | /// Generates assignment operators, constructors, and destructor for aggregate types as required
|
---|
34 | void autogenerateRoutines( std::list< Declaration * > &translationUnit );
|
---|
35 |
|
---|
36 | /// returns true if obj's name is the empty string and it has a bitfield width
|
---|
37 | bool isUnnamedBitfield( ObjectDecl * obj );
|
---|
38 |
|
---|
39 | /// size_t type - set when size_t typedef is seen. Useful in a few places,
|
---|
40 | /// such as in determining array dimension type
|
---|
41 | extern Type * SizeType;
|
---|
42 |
|
---|
43 | /// intrinsic dereference operator for unqualified types - set when *? function is seen in FindSpecialDeclarations.
|
---|
44 | /// Useful for creating dereference ApplicationExprs without a full resolver pass.
|
---|
45 | extern FunctionDecl * dereferenceOperator;
|
---|
46 |
|
---|
47 | /// generate the type of an assignment function for paramType.
|
---|
48 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
49 | FunctionType * genAssignType( Type * paramType, bool maybePolymorphic = true );
|
---|
50 |
|
---|
51 | /// generate the type of a default constructor or destructor for paramType.
|
---|
52 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
53 | FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true );
|
---|
54 |
|
---|
55 | /// generate the type of a copy constructor for paramType.
|
---|
56 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
57 | FunctionType * genCopyType( Type * paramType, bool maybePolymorphic = true );
|
---|
58 |
|
---|
59 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
|
---|
60 | template< typename OutputIterator >
|
---|
61 | Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
|
---|
62 |
|
---|
63 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
|
---|
64 | /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
|
---|
65 | template< typename OutputIterator >
|
---|
66 | Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, bool addCast = false ) {
|
---|
67 | bool isReferenceCtorDtor = false;
|
---|
68 | if ( dynamic_cast< ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) {
|
---|
69 | // reference constructors are essentially application of the rebind operator.
|
---|
70 | // apply & to both arguments, do not need a cast
|
---|
71 | fname = "?=?";
|
---|
72 | dstParam = new AddressExpr( dstParam );
|
---|
73 | addCast = false;
|
---|
74 | isReferenceCtorDtor = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // want to be able to generate assignment, ctor, and dtor generically,
|
---|
78 | // so fname is either ?=?, ?{}, or ^?{}
|
---|
79 | UntypedExpr * fExpr = new UntypedExpr( new NameExpr( fname ) );
|
---|
80 |
|
---|
81 | if ( addCast ) {
|
---|
82 | // cast to T& with qualifiers removed, so that qualified objects can be constructed
|
---|
83 | // and destructed with the same functions as non-qualified objects.
|
---|
84 | // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
|
---|
85 | // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
|
---|
86 | // remove lvalue as a qualifier, this can change to
|
---|
87 | // type->get_qualifiers() = Type::Qualifiers();
|
---|
88 | assert( type );
|
---|
89 | Type * castType = type->clone();
|
---|
90 | castType->get_qualifiers() -= Type::Qualifiers( Type::Lvalue | Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
|
---|
91 | // castType->set_lvalue( true ); // xxx - might not need this
|
---|
92 | dstParam = new CastExpr( dstParam, new ReferenceType( Type::Qualifiers(), castType ) );
|
---|
93 | }
|
---|
94 | fExpr->args.push_back( dstParam );
|
---|
95 |
|
---|
96 | Statement * listInit = srcParam.buildListInit( fExpr );
|
---|
97 |
|
---|
98 | // fetch next set of arguments
|
---|
99 | ++srcParam;
|
---|
100 |
|
---|
101 | // return if adding reference fails - will happen on default constructor and destructor
|
---|
102 | if ( isReferenceCtorDtor && ! srcParam.addReference() ) {
|
---|
103 | delete fExpr;
|
---|
104 | return listInit;
|
---|
105 | }
|
---|
106 |
|
---|
107 | std::list< Expression * > args = *srcParam;
|
---|
108 | fExpr->args.splice( fExpr->args.end(), args );
|
---|
109 |
|
---|
110 | *out++ = new ExprStmt( noLabels, fExpr );
|
---|
111 |
|
---|
112 | srcParam.clearArrayIndices();
|
---|
113 |
|
---|
114 | return listInit;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
|
---|
118 | /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
|
---|
119 | template< typename OutputIterator >
|
---|
120 | void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
|
---|
121 | static UniqueName indexName( "_index" );
|
---|
122 |
|
---|
123 | // for a flexible array member nothing is done -- user must define own assignment
|
---|
124 | if ( ! array->get_dimension() ) return ;
|
---|
125 |
|
---|
126 | Expression * begin, * end, * update, * cmp;
|
---|
127 | if ( forward ) {
|
---|
128 | // generate: for ( int i = 0; i < N; ++i )
|
---|
129 | begin = new ConstantExpr( Constant::from_int( 0 ) );
|
---|
130 | end = array->dimension->clone();
|
---|
131 | cmp = new NameExpr( "?<?" );
|
---|
132 | update = new NameExpr( "++?" );
|
---|
133 | } else {
|
---|
134 | // generate: for ( int i = N-1; i >= 0; --i )
|
---|
135 | begin = new UntypedExpr( new NameExpr( "?-?" ) );
|
---|
136 | ((UntypedExpr*)begin)->args.push_back( array->dimension->clone() );
|
---|
137 | ((UntypedExpr*)begin)->args.push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
|
---|
138 | end = new ConstantExpr( Constant::from_int( 0 ) );
|
---|
139 | cmp = new NameExpr( "?>=?" );
|
---|
140 | update = new NameExpr( "--?" );
|
---|
141 | }
|
---|
142 |
|
---|
143 | ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
|
---|
144 |
|
---|
145 | UntypedExpr *cond = new UntypedExpr( cmp );
|
---|
146 | cond->args.push_back( new VariableExpr( index ) );
|
---|
147 | cond->args.push_back( end );
|
---|
148 |
|
---|
149 | UntypedExpr *inc = new UntypedExpr( update );
|
---|
150 | inc->args.push_back( new VariableExpr( index ) );
|
---|
151 |
|
---|
152 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
|
---|
153 | dstIndex->args.push_back( dstParam );
|
---|
154 | dstIndex->args.push_back( new VariableExpr( index ) );
|
---|
155 | dstParam = dstIndex;
|
---|
156 |
|
---|
157 | // srcParam must keep track of the array indices to build the
|
---|
158 | // source parameter and/or array list initializer
|
---|
159 | srcParam.addArrayIndex( new VariableExpr( index ), array->dimension->clone() );
|
---|
160 |
|
---|
161 | // for stmt's body, eventually containing call
|
---|
162 | CompoundStmt * body = new CompoundStmt( noLabels );
|
---|
163 | Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->kids ), array->base, addCast, forward );
|
---|
164 |
|
---|
165 | // block containing for stmt and index variable
|
---|
166 | std::list<Statement *> initList;
|
---|
167 | CompoundStmt * block = new CompoundStmt( noLabels );
|
---|
168 | block->push_back( new DeclStmt( noLabels, index ) );
|
---|
169 | if ( listInit ) block->get_kids().push_back( listInit );
|
---|
170 | block->push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
|
---|
171 |
|
---|
172 | *out++ = block;
|
---|
173 | }
|
---|
174 |
|
---|
175 | template< typename OutputIterator >
|
---|
176 | Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
|
---|
177 | if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
178 | genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
|
---|
179 | return 0;
|
---|
180 | } else {
|
---|
181 | return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /// inserts into out a generated call expression to function fname with arguments dstParam
|
---|
186 | /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
|
---|
187 | /// object being constructed. The function wraps constructor and destructor calls in an
|
---|
188 | /// ImplicitCtorDtorStmt node.
|
---|
189 | template< typename OutputIterator >
|
---|
190 | void genImplicitCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
|
---|
191 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
|
---|
192 | assert( obj );
|
---|
193 | // unnamed bit fields are not copied as they cannot be accessed
|
---|
194 | if ( isUnnamedBitfield( obj ) ) return;
|
---|
195 |
|
---|
196 | bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) );
|
---|
197 | std::list< Statement * > stmts;
|
---|
198 | genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->type, addCast, forward );
|
---|
199 |
|
---|
200 | // 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
|
---|
201 | assert( stmts.size() <= 1 );
|
---|
202 | if ( stmts.size() == 1 ) {
|
---|
203 | Statement * callStmt = stmts.front();
|
---|
204 | if ( addCast ) {
|
---|
205 | // implicitly generated ctor/dtor calls should be wrapped
|
---|
206 | // so that later passes are aware they were generated.
|
---|
207 | // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
|
---|
208 | // because this causes the address to be taken at codegen, which is illegal in C.
|
---|
209 | callStmt = new ImplicitCtorDtorStmt( callStmt );
|
---|
210 | }
|
---|
211 | *out++ = callStmt;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | } // namespace SymTab
|
---|
215 |
|
---|
216 | // Local Variables: //
|
---|
217 | // tab-width: 4 //
|
---|
218 | // mode: c++ //
|
---|
219 | // compile-command: "make install" //
|
---|
220 | // End: //
|
---|
221 |
|
---|