source: src/SymTab/Autogen.h @ daf4c89

Last change on this file since daf4c89 was 8913de4, checked in by Andrew Beach <ajbeach@…>, 18 months ago

Update in autogen that should help with some resolver issues and invariant checks. Some related headers were cleaned up to reduce new code files including old ones.

  • Property mode set to 100644
File size: 9.7 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
[fb4dc28]11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Apr 14 15:06:00 2023
13// Update Count     : 17
[972e6f7]14//
15
[6b0b624]16#pragma once
[972e6f7]17
[30f9072]18#include <cassert>                // for assert
[b8524ca]19#include <iterator>               // for back_inserter
[d180746]20#include <string>                 // for string
[30f9072]21
[1a5ad8c]22#include "CodeGen/OperatorTable.h"
[30f9072]23#include "Common/UniqueName.h"    // for UniqueName
[b8524ca]24#include "Common/utility.h"       // for splice
[30f9072]25#include "InitTweak/InitTweak.h"  // for InitExpander
26#include "SynTree/Constant.h"     // for Constant
[d180746]27#include "SynTree/Declaration.h"  // for DeclarationWithType, ObjectDecl
28#include "SynTree/Expression.h"   // for NameExpr, ConstantExpr, UntypedExpr...
[30f9072]29#include "SynTree/Type.h"         // for Type, ArrayType, Type::Qualifiers
[07de76b]30#include "SynTree/Statement.h"    // for CompoundStmt, DeclStmt, ExprStmt
[972e6f7]31
[d180746]32class CompoundStmt;
33class Statement;
34
[972e6f7]35namespace SymTab {
[2be1023]36        /// Generates assignment operators, constructors, and destructor for aggregate types as required
37        void autogenerateRoutines( std::list< Declaration * > &translationUnit );
38
39        /// returns true if obj's name is the empty string and it has a bitfield width
40        bool isUnnamedBitfield( ObjectDecl * obj );
41
[837ce06]42        /// generate the type of an assignment function for paramType.
43        /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
44        FunctionType * genAssignType( Type * paramType, bool maybePolymorphic = true );
[8a6cf7e]45
[837ce06]46        /// generate the type of a default constructor or destructor for paramType.
47        /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
48        FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true );
[8404321]49
[837ce06]50        /// generate the type of a copy constructor for paramType.
51        /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
52        FunctionType * genCopyType( Type * paramType, bool maybePolymorphic = true );
[8404321]53
[2be1023]54        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
55        template< typename OutputIterator >
[b8524ca]56        Statement * genCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast = nullptr, bool forward = true );
57
[2be1023]58        /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
[f9cebb5]59        /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
[2be1023]60        template< typename OutputIterator >
[b8524ca]61        Statement * genScalarCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, Type * addCast = nullptr ) {
[1a5ad8c]62                bool isReferenceCtorDtor = false;
63                if ( dynamic_cast< ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) {
64                        // reference constructors are essentially application of the rebind operator.
65                        // apply & to both arguments, do not need a cast
66                        fname = "?=?";
67                        dstParam = new AddressExpr( dstParam );
[b95fe40]68                        addCast = nullptr;
[1a5ad8c]69                        isReferenceCtorDtor = true;
70                }
71
[49148d5]72                // want to be able to generate assignment, ctor, and dtor generically,
73                // so fname is either ?=?, ?{}, or ^?{}
[1a5ad8c]74                UntypedExpr * fExpr = new UntypedExpr( new NameExpr( fname ) );
[49148d5]75
76                if ( addCast ) {
77                        // cast to T& with qualifiers removed, so that qualified objects can be constructed
78                        // and destructed with the same functions as non-qualified objects.
79                        // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
80                        // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
81                        // remove lvalue as a qualifier, this can change to
82                        //   type->get_qualifiers() = Type::Qualifiers();
[b95fe40]83                        Type * castType = addCast->clone();
[b4f8808]84                        castType->get_qualifiers() -= Type::Qualifiers( Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
[49148d5]85                        // castType->set_lvalue( true ); // xxx - might not need this
86                        dstParam = new CastExpr( dstParam, new ReferenceType( Type::Qualifiers(), castType ) );
87                }
[1a5ad8c]88                fExpr->args.push_back( dstParam );
[2be1023]89
[49148d5]90                Statement * listInit = srcParam.buildListInit( fExpr );
[39f84a4]91
[1a5ad8c]92                // fetch next set of arguments
93                ++srcParam;
94
95                // return if adding reference fails - will happen on default constructor and destructor
96                if ( isReferenceCtorDtor && ! srcParam.addReference() ) {
97                        delete fExpr;
98                        return listInit;
99                }
100
101                std::list< Expression * > args = *srcParam;
102                fExpr->args.splice( fExpr->args.end(), args );
[4d2434a]103
[ba3706f]104                *out++ = new ExprStmt( fExpr );
[4d2434a]105
[49148d5]106                srcParam.clearArrayIndices();
[f9cebb5]107
[49148d5]108                return listInit;
[2be1023]109        }
110
111        /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
112        /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
113        template< typename OutputIterator >
[b8524ca]114        void genArrayCall( InitTweak::InitExpander_old & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, Type * addCast = nullptr, bool forward = true ) {
[2be1023]115                static UniqueName indexName( "_index" );
116
117                // for a flexible array member nothing is done -- user must define own assignment
[b95fe40]118                if ( ! array->get_dimension() ) return;
119
120                if ( addCast ) {
121                        // peel off array layer from cast
122                        ArrayType * at = strict_dynamic_cast< ArrayType * >( addCast );
123                        addCast = at->base;
124                }
[2be1023]125
126                Expression * begin, * end, * update, * cmp;
127                if ( forward ) {
[579263a]128                        // generate: for ( int i = 0; i < N; ++i )
129                        begin = new ConstantExpr( Constant::from_int( 0 ) );
[1a5ad8c]130                        end = array->dimension->clone();
[2be1023]131                        cmp = new NameExpr( "?<?" );
132                        update = new NameExpr( "++?" );
133                } else {
134                        // generate: for ( int i = N-1; i >= 0; --i )
135                        begin = new UntypedExpr( new NameExpr( "?-?" ) );
[1a5ad8c]136                        ((UntypedExpr*)begin)->args.push_back( array->dimension->clone() );
137                        ((UntypedExpr*)begin)->args.push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
[579263a]138                        end = new ConstantExpr( Constant::from_int( 0 ) );
[2be1023]139                        cmp = new NameExpr( "?>=?" );
140                        update = new NameExpr( "--?" );
141                }
142
[e4d829b]143                ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
[2be1023]144
145                UntypedExpr *cond = new UntypedExpr( cmp );
[1a5ad8c]146                cond->args.push_back( new VariableExpr( index ) );
147                cond->args.push_back( end );
[2be1023]148
149                UntypedExpr *inc = new UntypedExpr( update );
[1a5ad8c]150                inc->args.push_back( new VariableExpr( index ) );
[2be1023]151
152                UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
[1a5ad8c]153                dstIndex->args.push_back( dstParam );
154                dstIndex->args.push_back( new VariableExpr( index ) );
[2be1023]155                dstParam = dstIndex;
156
[39f84a4]157                // srcParam must keep track of the array indices to build the
158                // source parameter and/or array list initializer
[1a5ad8c]159                srcParam.addArrayIndex( new VariableExpr( index ), array->dimension->clone() );
[39f84a4]160
[2be1023]161                // for stmt's body, eventually containing call
[ba3706f]162                CompoundStmt * body = new CompoundStmt();
[1a5ad8c]163                Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->kids ), array->base, addCast, forward );
[2be1023]164
165                // block containing for stmt and index variable
166                std::list<Statement *> initList;
[ba3706f]167                CompoundStmt * block = new CompoundStmt();
168                block->push_back( new DeclStmt( index ) );
[f9cebb5]169                if ( listInit ) block->get_kids().push_back( listInit );
[ba3706f]170                block->push_back( new ForStmt( initList, cond, inc, body ) );
[2be1023]171
172                *out++ = block;
173        }
174
175        template< typename OutputIterator >
[b8524ca]176        Statement * genCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast, bool forward ) {
[2be1023]177                if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
[4d2434a]178                        genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
[f9cebb5]179                        return 0;
[2be1023]180                } else {
[f9cebb5]181                        return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
[2be1023]182                }
183        }
184
185        /// inserts into out a generated call expression to function fname with arguments dstParam
186        /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
187        /// object being constructed. The function wraps constructor and destructor calls in an
188        /// ImplicitCtorDtorStmt node.
189        template< typename OutputIterator >
[b8524ca]190        void genImplicitCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
[2be1023]191                ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
192                assert( obj );
193                // unnamed bit fields are not copied as they cannot be accessed
194                if ( isUnnamedBitfield( obj ) ) return;
195
[b95fe40]196                Type * addCast = nullptr;
197                if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) ) ) {
198                        assert( dstParam->result );
199                        addCast = dstParam->result;
200                }
[2be1023]201                std::list< Statement * > stmts;
[1a5ad8c]202                genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->type, addCast, forward );
[2be1023]203
[6cf27a07]204                // 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
205                assert( stmts.size() <= 1 );
[f9cebb5]206                if ( stmts.size() == 1 ) {
207                        Statement * callStmt = stmts.front();
208                        if ( addCast ) {
209                                // implicitly generated ctor/dtor calls should be wrapped
210                                // so that later passes are aware they were generated.
211                                // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
212                                // because this causes the address to be taken at codegen, which is illegal in C.
213                                callStmt = new ImplicitCtorDtorStmt( callStmt );
214                        }
215                        *out++ = callStmt;
216                }
[2be1023]217        }
[b8524ca]218
[972e6f7]219} // namespace SymTab
[6b0b624]220
221// Local Variables: //
222// tab-width: 4 //
223// mode: c++ //
224// compile-command: "make install" //
225// End: //
Note: See TracBrowser for help on using the repository browser.