source: src/SymTab/Autogen.h @ c8e4d2f8

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c8e4d2f8 was 2bfc6b2, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Refactor FindSpecialDeclarations? and associated special declarations

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