source: src/SymTab/Autogen.h @ 6b0b624

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 6b0b624 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 8.0 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 <string>
19#include "SynTree/Statement.h"
20#include "SynTree/Expression.h"
21#include "SynTree/Declaration.h"
22#include "SynTree/Initializer.h"
23#include "InitTweak/InitTweak.h"
24
25namespace SymTab {
26        /// Generates assignment operators, constructors, and destructor for aggregate types as required
27        void autogenerateRoutines( std::list< Declaration * > &translationUnit );
28
29        /// returns true if obj's name is the empty string and it has a bitfield width
30        bool isUnnamedBitfield( ObjectDecl * obj );
31
32        /// size_t type - set when size_t typedef is seen. Useful in a few places,
33        /// such as in determining array dimension type
34        extern Type * SizeType;
35
36        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
37        template< typename OutputIterator >
38        Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
39
40        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
41        /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
42        template< typename OutputIterator >
43        Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false ) {
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        dstParam = new AddressExpr( dstParam );
50        if ( addCast ) {
51                // cast to T* with qualifiers removed, so that qualified objects can be constructed
52                // and destructed with the same functions as non-qualified objects.
53                // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
54                // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
55                // remove lvalue as a qualifier, this can change to
56                //   type->get_qualifiers() = Type::Qualifiers();
57                assert( type );
58                Type * castType = type->clone();
59                castType->get_qualifiers() -= Type::Qualifiers( Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
60                castType->set_lvalue( true ); // xxx - might not need this
61                dstParam = new CastExpr( dstParam, new PointerType( Type::Qualifiers(), castType ) );
62        }
63        fExpr->get_args().push_back( dstParam );
64
65        Statement * listInit = srcParam.buildListInit( fExpr );
66
67        std::list< Expression * > args = *++srcParam;
68        fExpr->get_args().splice( fExpr->get_args().end(), args );
69
70        *out++ = new ExprStmt( noLabels, fExpr );
71
72        srcParam.clearArrayIndices();
73
74        return listInit;
75        }
76
77        /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
78        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
79        template< typename OutputIterator >
80        void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
81                static UniqueName indexName( "_index" );
82
83                // for a flexible array member nothing is done -- user must define own assignment
84                if ( ! array->get_dimension() ) return ;
85
86                Expression * begin, * end, * update, * cmp;
87                if ( forward ) {
88                        // generate: for ( int i = 0; i < N; ++i )
89                        begin = new ConstantExpr( Constant::from_int( 0 ) );
90                        end = array->get_dimension()->clone();
91                        cmp = new NameExpr( "?<?" );
92                        update = new NameExpr( "++?" );
93                } else {
94                        // generate: for ( int i = N-1; i >= 0; --i )
95                        begin = new UntypedExpr( new NameExpr( "?-?" ) );
96                        ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
97                        ((UntypedExpr*)begin)->get_args().push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
98                        end = new ConstantExpr( Constant::from_int( 0 ) );
99                        cmp = new NameExpr( "?>=?" );
100                        update = new NameExpr( "--?" );
101                }
102
103                ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
104
105                UntypedExpr *cond = new UntypedExpr( cmp );
106                cond->get_args().push_back( new VariableExpr( index ) );
107                cond->get_args().push_back( end );
108
109                UntypedExpr *inc = new UntypedExpr( update );
110                inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
111
112                UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
113                dstIndex->get_args().push_back( dstParam );
114                dstIndex->get_args().push_back( new VariableExpr( index ) );
115                dstParam = dstIndex;
116
117                // srcParam must keep track of the array indices to build the
118                // source parameter and/or array list initializer
119                srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
120
121                // for stmt's body, eventually containing call
122                CompoundStmt * body = new CompoundStmt( noLabels );
123                Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), addCast, forward );
124
125                // block containing for stmt and index variable
126                std::list<Statement *> initList;
127                CompoundStmt * block = new CompoundStmt( noLabels );
128                block->get_kids().push_back( new DeclStmt( noLabels, index ) );
129                if ( listInit ) block->get_kids().push_back( listInit );
130                block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
131
132                *out++ = block;
133        }
134
135        template< typename OutputIterator >
136        Statement * genCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
137                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
138                        genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
139                        return 0;
140                } else {
141                        return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
142                }
143        }
144
145        /// inserts into out a generated call expression to function fname with arguments dstParam
146        /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
147        /// object being constructed. The function wraps constructor and destructor calls in an
148        /// ImplicitCtorDtorStmt node.
149        template< typename OutputIterator >
150        void genImplicitCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
151                ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
152                assert( obj );
153                // unnamed bit fields are not copied as they cannot be accessed
154                if ( isUnnamedBitfield( obj ) ) return;
155
156                bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) );
157                std::list< Statement * > stmts;
158                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), addCast, forward );
159
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 ( addCast ) {
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                }
173        }
174} // namespace SymTab
175
176// Local Variables: //
177// tab-width: 4 //
178// mode: c++ //
179// compile-command: "make install" //
180// End: //
181
Note: See TracBrowser for help on using the repository browser.