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