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