source: src/SymTab/Autogen.h @ bff227f

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 bff227f was 4fbdfae0, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Find intrinsic dereference declaration so that dereference ApplicationExprs? can be built without the resolver

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