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