source: src/SymTab/Autogen.h @ 39f84a4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 39f84a4 was 39f84a4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

part-way through reorganizing ctor call generation so that it is more general

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[972e6f7]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 : Rob Schluntz
12// Last Modified On : Tue May 19 16:49:43 2015
13// Update Count     : 1
14//
15
16#ifndef AUTOGEN_H
17#define AUTOGEN_H
18
19#include <string>
20#include "SynTree/Statement.h"
21#include "SynTree/Expression.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Initializer.h"
[39f84a4]24#include "InitTweak/InitTweak.h"
[972e6f7]25
26namespace SymTab {
[2be1023]27        /// Generates assignment operators, constructors, and destructor for aggregate types as required
28        void autogenerateRoutines( std::list< Declaration * > &translationUnit );
29
30        /// returns true if obj's name is the empty string and it has a bitfield width
31        bool isUnnamedBitfield( ObjectDecl * obj );
32
[5f98ce5]33        /// size_t type - set when size_t typedef is seen. Useful in a few places,
34        /// such as in determining array dimension type
35        extern Type * SizeType;
36
[2be1023]37        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
38        template< typename OutputIterator >
[39f84a4]39        void genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward = true );
[2be1023]40
41        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
42        template< typename OutputIterator >
[39f84a4]43        void genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out ) {
[2be1023]44                // want to be able to generate assignment, ctor, and dtor generically,
45                // so fname is either ?=?, ?{}, or ^?{}
46                UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
47
48                // do something special for unnamed members
49                fExpr->get_args().push_back( new AddressExpr( dstParam ) );
50
[39f84a4]51    Statement * listInit = srcParam.buildListInit( fExpr );
52    if ( listInit ) {
53      *out++ = listInit;
54    }
55
56    std::list< Expression * > args = *++srcParam;
57    fExpr->get_args().splice( fExpr->get_args().end(), args );
58/*              if ( srcParam ) {
59                        // xxx -
60                        // make srcParam more complicated
61                        // if srcParam contains
[2be1023]62                        fExpr->get_args().push_back( srcParam );
63                }
[39f84a4]64*/
[2be1023]65                *out++ = new ExprStmt( noLabels, fExpr );
66        }
67
68        /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
69        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
70        template< typename OutputIterator >
[39f84a4]71        void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool forward = true ) {
[2be1023]72                static UniqueName indexName( "_index" );
73
74                // for a flexible array member nothing is done -- user must define own assignment
75                if ( ! array->get_dimension() ) return ;
76
77                Expression * begin, * end, * update, * cmp;
78                if ( forward ) {
79                        // generate: for ( int i = 0; i < 0; ++i )
80                        begin = new NameExpr( "0" );
81                        end = array->get_dimension()->clone();
82                        cmp = new NameExpr( "?<?" );
83                        update = new NameExpr( "++?" );
84                } else {
85                        // generate: for ( int i = N-1; i >= 0; --i )
86                        begin = new UntypedExpr( new NameExpr( "?-?" ) );
87                        ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
88                        ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) );
89                        end = new NameExpr( "0" );
90                        cmp = new NameExpr( "?>=?" );
91                        update = new NameExpr( "--?" );
92                }
93
94                ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL );
95
96                UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
97                init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
98                init->get_args().push_back( begin );
99                index->set_init( new SingleInit( init, std::list<Expression*>() ) );
100
101                UntypedExpr *cond = new UntypedExpr( cmp );
102                cond->get_args().push_back( new VariableExpr( index ) );
103                cond->get_args().push_back( end );
104
105                UntypedExpr *inc = new UntypedExpr( update );
106                inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
107
108                UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
109                dstIndex->get_args().push_back( dstParam );
110                dstIndex->get_args().push_back( new VariableExpr( index ) );
111                dstParam = dstIndex;
112
[39f84a4]113                // srcParam must keep track of the array indices to build the
114                // source parameter and/or array list initializer
115                srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
116
117                // if ( srcParam ) {
118                //      UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
119                //      srcIndex->get_args().push_back( srcParam );
120                //      srcIndex->get_args().push_back( new VariableExpr( index ) );
121                //      srcParam = srcIndex;
122                // }
[2be1023]123
124                // for stmt's body, eventually containing call
125                CompoundStmt * body = new CompoundStmt( noLabels );
126                genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), forward );
127
128                // block containing for stmt and index variable
129                std::list<Statement *> initList;
130                CompoundStmt * block = new CompoundStmt( noLabels );
131                block->get_kids().push_back( new DeclStmt( noLabels, index ) );
132                block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
133
134                *out++ = block;
135        }
136
137        template< typename OutputIterator >
[39f84a4]138        void genCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward ) {
[2be1023]139                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
140                        genArrayCall( srcParam, dstParam, fname, out, at, forward );
141                } else {
142                        genScalarCall( srcParam, dstParam, fname, out );
143                }
144        }
145
146        /// inserts into out a generated call expression to function fname with arguments dstParam
147        /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
148        /// object being constructed. The function wraps constructor and destructor calls in an
149        /// ImplicitCtorDtorStmt node.
150        template< typename OutputIterator >
[39f84a4]151        void genImplicitCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
[2be1023]152                ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
153                assert( obj );
154                // unnamed bit fields are not copied as they cannot be accessed
155                if ( isUnnamedBitfield( obj ) ) return;
156
157                std::list< Statement * > stmts;
158                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), forward );
159
[6cf27a07]160                // 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
161                assert( stmts.size() <= 1 );
162    if ( stmts.size() == 1 ) {
163                Statement * callStmt = stmts.front();
164                if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) ) ) {
165                        // implicitly generated ctor/dtor calls should be wrapped
166                        // so that later passes are aware they were generated.
167                        // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
168                        // because this causes the address to be taken at codegen, which is illegal in C.
169                        callStmt = new ImplicitCtorDtorStmt( callStmt );
170                }
171                *out++ = callStmt;
172    }
[2be1023]173        }
[972e6f7]174} // namespace SymTab
175#endif // AUTOGEN_H
176
Note: See TracBrowser for help on using the repository browser.