source: src/SymTab/Autogen.h @ 49148d5

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 49148d5 was 49148d5, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Update autogen to generate ctor/dtor/assign with references

  • Property mode set to 100644
File size: 7.9 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 : Wed Jun 21 17:25:26 2017
13// Update Count     : 14
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"
24#include "InitTweak/InitTweak.h"
25
26namespace SymTab {
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
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
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 >
39        Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
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        /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
43        template< typename OutputIterator >
44        Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false ) {
45                // want to be able to generate assignment, ctor, and dtor generically,
46                // so fname is either ?=?, ?{}, or ^?{}
47                UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
48
49                if ( addCast ) {
50                        // cast to T& with qualifiers removed, so that qualified objects can be constructed
51                        // and destructed with the same functions as non-qualified objects.
52                        // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
53                        // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
54                        // remove lvalue as a qualifier, this can change to
55                        //   type->get_qualifiers() = Type::Qualifiers();
56                        assert( type );
57                        Type * castType = type->clone();
58                        castType->get_qualifiers() -= Type::Qualifiers( Type::Lvalue | Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
59                        // castType->set_lvalue( true ); // xxx - might not need this
60                        dstParam = new CastExpr( dstParam, new ReferenceType( Type::Qualifiers(), castType ) );
61                }
62                fExpr->get_args().push_back( dstParam );
63
64                Statement * listInit = srcParam.buildListInit( fExpr );
65
66                std::list< Expression * > args = *++srcParam;
67                fExpr->get_args().splice( fExpr->get_args().end(), args );
68
69                *out++ = new ExprStmt( noLabels, fExpr );
70
71                srcParam.clearArrayIndices();
72
73                return listInit;
74        }
75
76        /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
77        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
78        template< typename OutputIterator >
79        void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
80                static UniqueName indexName( "_index" );
81
82                // for a flexible array member nothing is done -- user must define own assignment
83                if ( ! array->get_dimension() ) return ;
84
85                Expression * begin, * end, * update, * cmp;
86                if ( forward ) {
87                        // generate: for ( int i = 0; i < N; ++i )
88                        begin = new ConstantExpr( Constant::from_int( 0 ) );
89                        end = array->get_dimension()->clone();
90                        cmp = new NameExpr( "?<?" );
91                        update = new NameExpr( "++?" );
92                } else {
93                        // generate: for ( int i = N-1; i >= 0; --i )
94                        begin = new UntypedExpr( new NameExpr( "?-?" ) );
95                        ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
96                        ((UntypedExpr*)begin)->get_args().push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
97                        end = new ConstantExpr( Constant::from_int( 0 ) );
98                        cmp = new NameExpr( "?>=?" );
99                        update = new NameExpr( "--?" );
100                }
101
102                ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
103
104                UntypedExpr *cond = new UntypedExpr( cmp );
105                cond->get_args().push_back( new VariableExpr( index ) );
106                cond->get_args().push_back( end );
107
108                UntypedExpr *inc = new UntypedExpr( update );
109                inc->get_args().push_back( new VariableExpr( index ) );
110
111                UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
112                dstIndex->get_args().push_back( dstParam );
113                dstIndex->get_args().push_back( new VariableExpr( index ) );
114                dstParam = dstIndex;
115
116                // srcParam must keep track of the array indices to build the
117                // source parameter and/or array list initializer
118                srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
119
120                // for stmt's body, eventually containing call
121                CompoundStmt * body = new CompoundStmt( noLabels );
122                Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), addCast, forward );
123
124                // block containing for stmt and index variable
125                std::list<Statement *> initList;
126                CompoundStmt * block = new CompoundStmt( noLabels );
127                block->get_kids().push_back( new DeclStmt( noLabels, index ) );
128                if ( listInit ) block->get_kids().push_back( listInit );
129                block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
130
131                *out++ = block;
132        }
133
134        template< typename OutputIterator >
135        Statement * genCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
136                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
137                        genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
138                        return 0;
139                } else {
140                        return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
141                }
142        }
143
144        /// inserts into out a generated call expression to function fname with arguments dstParam
145        /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
146        /// object being constructed. The function wraps constructor and destructor calls in an
147        /// ImplicitCtorDtorStmt node.
148        template< typename OutputIterator >
149        void genImplicitCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
150                ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
151                assert( obj );
152                // unnamed bit fields are not copied as they cannot be accessed
153                if ( isUnnamedBitfield( obj ) ) return;
154
155                bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) );
156                std::list< Statement * > stmts;
157                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), addCast, forward );
158
159                // 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
160                assert( stmts.size() <= 1 );
161                if ( stmts.size() == 1 ) {
162                        Statement * callStmt = stmts.front();
163                        if ( addCast ) {
164                                // implicitly generated ctor/dtor calls should be wrapped
165                                // so that later passes are aware they were generated.
166                                // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
167                                // because this causes the address to be taken at codegen, which is illegal in C.
168                                callStmt = new ImplicitCtorDtorStmt( callStmt );
169                        }
170                        *out++ = callStmt;
171                }
172        }
173} // namespace SymTab
174#endif // AUTOGEN_H
Note: See TracBrowser for help on using the repository browser.