source: src/SymTab/Autogen.h @ d1969a6

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

insert implicit ctor/dtors if field is unhandled in a struct ctor/dtor

  • 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 : 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"
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                // do something special for unnamed members
50                dstParam = new AddressExpr( dstParam );
51                if ( addCast ) {
52                        // cast to T* with qualifiers removed, so that qualified objects can be constructed
53                        // and destructed with the same functions as non-qualified objects.
54                        // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
55                        // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
56                        // remove lvalue as a qualifier, this can change to
57                        //   type->get_qualifiers() = Type::Qualifiers();
58                        assert( type );
59                        Type * castType = type->clone();
60                        castType->get_qualifiers() -= Type::Qualifiers(true, true, true, false, true, true);
61                        castType->set_isLvalue( true ); // xxx - might not need this
62                        dstParam = new CastExpr( dstParam, new PointerType( Type::Qualifiers(), castType ) );
63                }
64                fExpr->get_args().push_back( dstParam );
65
66                Statement * listInit = srcParam.buildListInit( fExpr );
67
68                std::list< Expression * > args = *++srcParam;
69                fExpr->get_args().splice( fExpr->get_args().end(), args );
70
71                *out++ = new ExprStmt( noLabels, fExpr );
72
73                srcParam.clearArrayIndices();
74
75                return listInit;
76        }
77
78        /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
79        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
80        template< typename OutputIterator >
81        void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
82                static UniqueName indexName( "_index" );
83
84                // for a flexible array member nothing is done -- user must define own assignment
85                if ( ! array->get_dimension() ) return ;
86
87                Expression * begin, * end, * update, * cmp;
88                if ( forward ) {
89                        // generate: for ( int i = 0; i < 0; ++i )
90                        begin = new NameExpr( "0" );
91                        end = array->get_dimension()->clone();
92                        cmp = new NameExpr( "?<?" );
93                        update = new NameExpr( "++?" );
94                } else {
95                        // generate: for ( int i = N-1; i >= 0; --i )
96                        begin = new UntypedExpr( new NameExpr( "?-?" ) );
97                        ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
98                        ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) );
99                        end = new NameExpr( "0" );
100                        cmp = new NameExpr( "?>=?" );
101                        update = new NameExpr( "--?" );
102                }
103
104                ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin, std::list<Expression*>() ) );
105
106                UntypedExpr *cond = new UntypedExpr( cmp );
107                cond->get_args().push_back( new VariableExpr( index ) );
108                cond->get_args().push_back( end );
109
110                UntypedExpr *inc = new UntypedExpr( update );
111                inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
112
113                UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
114                dstIndex->get_args().push_back( dstParam );
115                dstIndex->get_args().push_back( new VariableExpr( index ) );
116                dstParam = dstIndex;
117
118                // srcParam must keep track of the array indices to build the
119                // source parameter and/or array list initializer
120                srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
121
122                // for stmt's body, eventually containing call
123                CompoundStmt * body = new CompoundStmt( noLabels );
124                Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), addCast, forward );
125
126                // block containing for stmt and index variable
127                std::list<Statement *> initList;
128                CompoundStmt * block = new CompoundStmt( noLabels );
129                block->get_kids().push_back( new DeclStmt( noLabels, index ) );
130                if ( listInit ) block->get_kids().push_back( listInit );
131                block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
132
133                *out++ = block;
134        }
135
136        template< typename OutputIterator >
137        Statement * genCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
138                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
139                        genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
140                        return 0;
141                } else {
142                        return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
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 >
151        void genImplicitCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
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                bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) );
158                std::list< Statement * > stmts;
159                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), addCast, forward );
160
161                // 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
162                assert( stmts.size() <= 1 );
163                if ( stmts.size() == 1 ) {
164                        Statement * callStmt = stmts.front();
165                        if ( addCast ) {
166                                // implicitly generated ctor/dtor calls should be wrapped
167                                // so that later passes are aware they were generated.
168                                // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
169                                // because this causes the address to be taken at codegen, which is illegal in C.
170                                callStmt = new ImplicitCtorDtorStmt( callStmt );
171                        }
172                        *out++ = callStmt;
173                }
174        }
175} // namespace SymTab
176#endif // AUTOGEN_H
177
Note: See TracBrowser for help on using the repository browser.