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