source: src/SymTab/Autogen.h @ 2c04369

arm-ehcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 2c04369 was 2c04369, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Fixed some problems in convert. One of which was better solved by removing the FindSpecialDeclarations? hack.

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