[0dd3a2f] | 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 | //
|
---|
[3be261a] | 7 | // Expression.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[a5f0529] | 12 | // Last Modified On : Tue Jul 25 14:15:47 2017
|
---|
| 13 | // Update Count : 54
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[ea6332d] | 16 | #include "SynTree/Expression.h"
|
---|
| 17 |
|
---|
| 18 | #include <cassert> // for assert, assertf
|
---|
| 19 | #include <iostream> // for ostream, operator<<, basic_ostream
|
---|
| 20 | #include <list> // for list, _List_iterator, list<>::co...
|
---|
| 21 |
|
---|
| 22 | #include "Common/utility.h" // for maybeClone, cloneAll, deleteAll
|
---|
| 23 | #include "Declaration.h" // for ObjectDecl, DeclarationWithType
|
---|
| 24 | #include "Expression.h" // for Expression, ImplicitCopyCtorExpr
|
---|
| 25 | #include "InitTweak/InitTweak.h" // for getCallArg, getPointerBase
|
---|
| 26 | #include "Initializer.h" // for Designation, Initializer
|
---|
| 27 | #include "Statement.h" // for CompoundStmt, ExprStmt, Statement
|
---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
|
---|
| 29 | #include "SynTree/Constant.h" // for Constant
|
---|
| 30 | #include "Type.h" // for Type, BasicType, Type::Qualifiers
|
---|
| 31 | #include "TypeSubstitution.h" // for TypeSubstitution
|
---|
[51b73452] | 32 |
|
---|
[b0440b7] | 33 | #include "GenPoly/Lvalue.h"
|
---|
[51b73452] | 34 |
|
---|
[68195a6] | 35 | void printInferParams( const InferredParams & inferParams, std::ostream &os, Indenter indent, int level ) {
|
---|
| 36 | if ( ! inferParams.empty() ) {
|
---|
| 37 | os << indent << "with inferred parameters " << level << ":" << std::endl;
|
---|
| 38 | for ( InferredParams::const_iterator i = inferParams.begin(); i != inferParams.end(); ++i ) {
|
---|
| 39 | os << indent+1;
|
---|
| 40 | Declaration::declFromId( i->second.decl )->printShort( os, indent+1 );
|
---|
| 41 | os << std::endl;
|
---|
| 42 | printInferParams( *i->second.inferParams, os, indent+1, level+1 );
|
---|
| 43 | } // for
|
---|
| 44 | } // if
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[bf4b4cf] | 47 | Expression::Expression() : result( 0 ), env( 0 ) {}
|
---|
[0dd3a2f] | 48 |
|
---|
[df626eb] | 49 | Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ) {
|
---|
[0dd3a2f] | 50 | }
|
---|
| 51 |
|
---|
| 52 | Expression::~Expression() {
|
---|
| 53 | delete env;
|
---|
[906e24d] | 54 | delete result;
|
---|
[51b73452] | 55 | }
|
---|
| 56 |
|
---|
[50377a4] | 57 | void Expression::print( std::ostream &os, Indenter indent ) const {
|
---|
[68195a6] | 58 | printInferParams( inferParams, os, indent+1, 0 );
|
---|
| 59 |
|
---|
[0dd3a2f] | 60 | if ( env ) {
|
---|
[50377a4] | 61 | os << std::endl << indent << "... with environment:" << std::endl;
|
---|
| 62 | env->print( os, indent+1 );
|
---|
[0dd3a2f] | 63 | } // if
|
---|
[e04ef3a] | 64 |
|
---|
| 65 | if ( extension ) {
|
---|
[50377a4] | 66 | os << std::endl << indent << "... with extension:";
|
---|
[e04ef3a] | 67 | } // if
|
---|
[51b73452] | 68 | }
|
---|
| 69 |
|
---|
[bf4b4cf] | 70 | ConstantExpr::ConstantExpr( Constant _c ) : Expression(), constant( _c ) {
|
---|
[906e24d] | 71 | set_result( constant.get_type()->clone() );
|
---|
[51b73452] | 72 | }
|
---|
| 73 |
|
---|
[0dd3a2f] | 74 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
|
---|
| 75 | }
|
---|
[51b73452] | 76 |
|
---|
| 77 | ConstantExpr::~ConstantExpr() {}
|
---|
| 78 |
|
---|
[50377a4] | 79 | void ConstantExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[60089f4] | 80 | os << "constant expression " ;
|
---|
[cf0941d] | 81 | constant.print( os );
|
---|
[0dd3a2f] | 82 | Expression::print( os, indent );
|
---|
[51b73452] | 83 | }
|
---|
| 84 |
|
---|
[ddb80bd] | 85 | long long int ConstantExpr::intValue() const {
|
---|
| 86 | if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) {
|
---|
| 87 | if ( basicType->isInteger() ) {
|
---|
| 88 | return get_constant()->get_ival();
|
---|
| 89 | }
|
---|
| 90 | } else if ( dynamic_cast< OneType * >( result ) ) {
|
---|
| 91 | return 1;
|
---|
| 92 | } else if ( dynamic_cast< ZeroType * >( result ) ) {
|
---|
| 93 | return 0;
|
---|
| 94 | }
|
---|
| 95 | throw SemanticError( "Constant expression of non-integral type ", this );
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[bf4b4cf] | 98 | VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) {
|
---|
[db4ecc5] | 99 | assert( var );
|
---|
| 100 | assert( var->get_type() );
|
---|
[906e24d] | 101 | Type * type = var->get_type()->clone();
|
---|
[615a096] | 102 | type->set_lvalue( true );
|
---|
[3de176d] | 103 |
|
---|
| 104 | // xxx - doesn't quite work yet - get different alternatives with the same cost
|
---|
| 105 |
|
---|
| 106 | // // enumerators are not lvalues
|
---|
| 107 | // if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( var->get_type() ) ) {
|
---|
| 108 | // assert( inst->baseEnum );
|
---|
| 109 | // EnumDecl * decl = inst->baseEnum;
|
---|
[0690350] | 110 | // long long int value;
|
---|
| 111 | // if ( decl->valueOf( var, value ) ) {
|
---|
| 112 | // type->set_lvalue( false );
|
---|
[3de176d] | 113 | // }
|
---|
| 114 | // }
|
---|
| 115 |
|
---|
[906e24d] | 116 | set_result( type );
|
---|
[51b73452] | 117 | }
|
---|
| 118 |
|
---|
[0dd3a2f] | 119 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
|
---|
[51b73452] | 120 | }
|
---|
| 121 |
|
---|
[0dd3a2f] | 122 | VariableExpr::~VariableExpr() {
|
---|
| 123 | // don't delete the declaration, since it points somewhere else in the tree
|
---|
[51b73452] | 124 | }
|
---|
| 125 |
|
---|
[8a6cf7e] | 126 | VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) {
|
---|
| 127 | VariableExpr * funcExpr = new VariableExpr( func );
|
---|
| 128 | funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) );
|
---|
| 129 | return funcExpr;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[50377a4] | 132 | void VariableExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[89231bc] | 133 | os << "Variable Expression: ";
|
---|
[50377a4] | 134 | var->printShort(os, indent);
|
---|
[0dd3a2f] | 135 | Expression::print( os, indent );
|
---|
[51b73452] | 136 | }
|
---|
| 137 |
|
---|
[bf4b4cf] | 138 | SizeofExpr::SizeofExpr( Expression *expr_ ) :
|
---|
| 139 | Expression(), expr(expr_), type(0), isType(false) {
|
---|
[906e24d] | 140 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[51b73452] | 141 | }
|
---|
| 142 |
|
---|
[bf4b4cf] | 143 | SizeofExpr::SizeofExpr( Type *type_ ) :
|
---|
| 144 | Expression(), expr(0), type(type_), isType(true) {
|
---|
[906e24d] | 145 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[51b73452] | 146 | }
|
---|
| 147 |
|
---|
[0dd3a2f] | 148 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
|
---|
| 149 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
|
---|
[51b73452] | 150 | }
|
---|
| 151 |
|
---|
[0dd3a2f] | 152 | SizeofExpr::~SizeofExpr() {
|
---|
| 153 | delete expr;
|
---|
| 154 | delete type;
|
---|
[51b73452] | 155 | }
|
---|
| 156 |
|
---|
[50377a4] | 157 | void SizeofExpr::print( std::ostream &os, Indenter indent) const {
|
---|
[89231bc] | 158 | os << "Sizeof Expression on: ";
|
---|
[50377a4] | 159 | if (isType) type->print(os, indent+1);
|
---|
| 160 | else expr->print(os, indent+1);
|
---|
[47534159] | 161 | Expression::print( os, indent );
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[bf4b4cf] | 164 | AlignofExpr::AlignofExpr( Expression *expr_ ) :
|
---|
| 165 | Expression(), expr(expr_), type(0), isType(false) {
|
---|
[906e24d] | 166 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[47534159] | 167 | }
|
---|
| 168 |
|
---|
[bf4b4cf] | 169 | AlignofExpr::AlignofExpr( Type *type_ ) :
|
---|
| 170 | Expression(), expr(0), type(type_), isType(true) {
|
---|
[906e24d] | 171 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[47534159] | 172 | }
|
---|
| 173 |
|
---|
| 174 | AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
|
---|
| 175 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | AlignofExpr::~AlignofExpr() {
|
---|
| 179 | delete expr;
|
---|
| 180 | delete type;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[50377a4] | 183 | void AlignofExpr::print( std::ostream &os, Indenter indent) const {
|
---|
[f19339e] | 184 | os << "Alignof Expression on: ";
|
---|
[50377a4] | 185 | if (isType) type->print(os, indent+1);
|
---|
| 186 | else expr->print(os, indent+1);
|
---|
[0dd3a2f] | 187 | Expression::print( os, indent );
|
---|
[51b73452] | 188 | }
|
---|
| 189 |
|
---|
[bf4b4cf] | 190 | UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type, const std::string &member ) :
|
---|
| 191 | Expression(), type(type), member(member) {
|
---|
[50377a4] | 192 | assert( type );
|
---|
[906e24d] | 193 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[2a4b088] | 194 | }
|
---|
| 195 |
|
---|
| 196 | UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
|
---|
| 197 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
|
---|
| 198 |
|
---|
| 199 | UntypedOffsetofExpr::~UntypedOffsetofExpr() {
|
---|
| 200 | delete type;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[50377a4] | 203 | void UntypedOffsetofExpr::print( std::ostream &os, Indenter indent) const {
|
---|
| 204 | os << "Untyped Offsetof Expression on member " << member << " of ";
|
---|
| 205 | type->print(os, indent+1);
|
---|
[2a4b088] | 206 | Expression::print( os, indent );
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[bf4b4cf] | 209 | OffsetofExpr::OffsetofExpr( Type *type, DeclarationWithType *member ) :
|
---|
| 210 | Expression(), type(type), member(member) {
|
---|
[50377a4] | 211 | assert( member );
|
---|
| 212 | assert( type );
|
---|
[906e24d] | 213 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
|
---|
[25a054f] | 214 | }
|
---|
| 215 |
|
---|
| 216 | OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
|
---|
[79970ed] | 217 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
|
---|
[25a054f] | 218 |
|
---|
| 219 | OffsetofExpr::~OffsetofExpr() {
|
---|
| 220 | delete type;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[50377a4] | 223 | void OffsetofExpr::print( std::ostream &os, Indenter indent) const {
|
---|
| 224 | os << "Offsetof Expression on member " << member->name << " of ";
|
---|
| 225 | type->print(os, indent+1);
|
---|
[25a054f] | 226 | Expression::print( os, indent );
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[bf4b4cf] | 229 | OffsetPackExpr::OffsetPackExpr( StructInstType *type ) : Expression(), type( type ) {
|
---|
[50377a4] | 230 | assert( type );
|
---|
[906e24d] | 231 | set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
|
---|
[afc1045] | 232 | }
|
---|
| 233 |
|
---|
| 234 | OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
|
---|
| 235 |
|
---|
| 236 | OffsetPackExpr::~OffsetPackExpr() { delete type; }
|
---|
| 237 |
|
---|
[50377a4] | 238 | void OffsetPackExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 239 | os << "Offset pack expression on ";
|
---|
| 240 | type->print(os, indent+1);
|
---|
[25a054f] | 241 | Expression::print( os, indent );
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[bf4b4cf] | 244 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_ ) :
|
---|
| 245 | Expression(), attr( attr ), expr(expr_), type(0), isType(false) {
|
---|
[51b73452] | 246 | }
|
---|
| 247 |
|
---|
[bf4b4cf] | 248 | AttrExpr::AttrExpr( Expression *attr, Type *type_ ) :
|
---|
| 249 | Expression(), attr( attr ), expr(0), type(type_), isType(true) {
|
---|
[51b73452] | 250 | }
|
---|
| 251 |
|
---|
[0dd3a2f] | 252 | AttrExpr::AttrExpr( const AttrExpr &other ) :
|
---|
| 253 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
|
---|
[51b73452] | 254 | }
|
---|
| 255 |
|
---|
[0dd3a2f] | 256 | AttrExpr::~AttrExpr() {
|
---|
| 257 | delete attr;
|
---|
| 258 | delete expr;
|
---|
| 259 | delete type;
|
---|
[51b73452] | 260 | }
|
---|
| 261 |
|
---|
[50377a4] | 262 | void AttrExpr::print( std::ostream &os, Indenter indent) const {
|
---|
[f19339e] | 263 | os << "Attr ";
|
---|
[50377a4] | 264 | attr->print( os, indent+1);
|
---|
[0dd3a2f] | 265 | if ( isType || expr ) {
|
---|
| 266 | os << "applied to: ";
|
---|
[50377a4] | 267 | if (isType) type->print(os, indent+1);
|
---|
| 268 | else expr->print(os, indent+1);
|
---|
[0dd3a2f] | 269 | } // if
|
---|
| 270 | Expression::print( os, indent );
|
---|
[51b73452] | 271 | }
|
---|
| 272 |
|
---|
[bf4b4cf] | 273 | CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
|
---|
[906e24d] | 274 | set_result(toType);
|
---|
[51b73452] | 275 | }
|
---|
| 276 |
|
---|
[bf4b4cf] | 277 | CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) {
|
---|
[906e24d] | 278 | set_result( new VoidType( Type::Qualifiers() ) );
|
---|
[51b73452] | 279 | }
|
---|
| 280 |
|
---|
[0dd3a2f] | 281 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
|
---|
[51b73452] | 282 | }
|
---|
| 283 |
|
---|
| 284 | CastExpr::~CastExpr() {
|
---|
[0dd3a2f] | 285 | delete arg;
|
---|
[51b73452] | 286 | }
|
---|
| 287 |
|
---|
[50377a4] | 288 | void CastExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 289 | os << "Cast of:" << std::endl << indent+1;
|
---|
| 290 | arg->print(os, indent+1);
|
---|
| 291 | os << std::endl << indent << "... to:";
|
---|
[906e24d] | 292 | if ( result->isVoid() ) {
|
---|
[50377a4] | 293 | os << " nothing";
|
---|
[0dd3a2f] | 294 | } else {
|
---|
[50377a4] | 295 | os << std::endl << indent+1;
|
---|
| 296 | result->print( os, indent+1 );
|
---|
[0dd3a2f] | 297 | } // if
|
---|
| 298 | Expression::print( os, indent );
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[a5f0529] | 301 | VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
|
---|
| 302 | set_result(toType);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | VirtualCastExpr::~VirtualCastExpr() {
|
---|
| 309 | delete arg;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[50377a4] | 312 | void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 313 | os << "Virtual Cast of:" << std::endl << indent+1;
|
---|
| 314 | arg->print(os, indent+1);
|
---|
| 315 | os << std::endl << indent << "... to:";
|
---|
[a5f0529] | 316 | if ( ! result ) {
|
---|
[50377a4] | 317 | os << " unknown";
|
---|
[a5f0529] | 318 | } else {
|
---|
[50377a4] | 319 | os << std::endl << indent+1;
|
---|
| 320 | result->print( os, indent+1 );
|
---|
[a5f0529] | 321 | } // if
|
---|
| 322 | Expression::print( os, indent );
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[bf4b4cf] | 325 | UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) :
|
---|
| 326 | Expression(), member(member), aggregate(aggregate) {
|
---|
[50377a4] | 327 | assert( aggregate );
|
---|
| 328 | }
|
---|
[51b73452] | 329 |
|
---|
[0dd3a2f] | 330 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
|
---|
[3b58d91] | 331 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
|
---|
[51b73452] | 332 | }
|
---|
| 333 |
|
---|
| 334 | UntypedMemberExpr::~UntypedMemberExpr() {
|
---|
[0dd3a2f] | 335 | delete aggregate;
|
---|
[3b58d91] | 336 | delete member;
|
---|
[51b73452] | 337 | }
|
---|
| 338 |
|
---|
[50377a4] | 339 | void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 340 | os << "Untyped Member Expression, with field: " << std::endl << indent+1;
|
---|
| 341 | member->print(os, indent+1 );
|
---|
| 342 | os << indent << "... from aggregate: " << std::endl << indent+1;
|
---|
| 343 | aggregate->print(os, indent+1);
|
---|
[0dd3a2f] | 344 | Expression::print( os, indent );
|
---|
[51b73452] | 345 | }
|
---|
| 346 |
|
---|
[d9fa60a] | 347 | namespace {
|
---|
| 348 | TypeSubstitution makeSub( Type * t ) {
|
---|
[0698aa1] | 349 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {
|
---|
| 350 | return makeSub( refType->get_base() );
|
---|
| 351 | } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
|
---|
[98a249fb] | 352 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );
|
---|
[d9fa60a] | 353 | } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {
|
---|
[98a249fb] | 354 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );
|
---|
[d9fa60a] | 355 | } else {
|
---|
[4b0f997] | 356 | assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() );
|
---|
[d9fa60a] | 357 | }
|
---|
| 358 | }
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[51b73452] | 361 |
|
---|
[bf4b4cf] | 362 | MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) :
|
---|
| 363 | Expression(), member(member), aggregate(aggregate) {
|
---|
[50377a4] | 364 | assert( member );
|
---|
| 365 | assert( aggregate );
|
---|
[d9fa60a] | 366 |
|
---|
| 367 | TypeSubstitution sub( makeSub( aggregate->get_result() ) );
|
---|
| 368 | Type * res = member->get_type()->clone();
|
---|
| 369 | sub.apply( res );
|
---|
[487845d7] | 370 | result = res;
|
---|
| 371 | result->set_lvalue( true );
|
---|
| 372 | result->get_qualifiers() |= aggregate->result->get_qualifiers();
|
---|
[51b73452] | 373 | }
|
---|
| 374 |
|
---|
[0dd3a2f] | 375 | MemberExpr::MemberExpr( const MemberExpr &other ) :
|
---|
[39f84a4] | 376 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
|
---|
[51b73452] | 377 | }
|
---|
| 378 |
|
---|
| 379 | MemberExpr::~MemberExpr() {
|
---|
[4551a6e] | 380 | // don't delete the member declaration, since it points somewhere else in the tree
|
---|
[0dd3a2f] | 381 | delete aggregate;
|
---|
[51b73452] | 382 | }
|
---|
| 383 |
|
---|
[50377a4] | 384 | void MemberExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[89231bc] | 385 | os << "Member Expression, with field: " << std::endl;
|
---|
[50377a4] | 386 | os << indent+1;
|
---|
| 387 | member->print( os, indent+1 );
|
---|
| 388 | os << std::endl << indent << "... from aggregate: " << std::endl << indent+1;
|
---|
| 389 | aggregate->print(os, indent + 1);
|
---|
[0dd3a2f] | 390 | Expression::print( os, indent );
|
---|
[51b73452] | 391 | }
|
---|
| 392 |
|
---|
[bf4b4cf] | 393 | UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) :
|
---|
| 394 | Expression(), function(function), args(args) {}
|
---|
[51b73452] | 395 |
|
---|
[0dd3a2f] | 396 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
|
---|
| 397 | Expression( other ), function( maybeClone( other.function ) ) {
|
---|
| 398 | cloneAll( other.args, args );
|
---|
[51b73452] | 399 | }
|
---|
| 400 |
|
---|
[ac71a86] | 401 | UntypedExpr::~UntypedExpr() {
|
---|
| 402 | delete function;
|
---|
[03b812d2] | 403 | deleteAll( args );
|
---|
[ac71a86] | 404 | }
|
---|
[51b73452] | 405 |
|
---|
[b3b2077] | 406 | UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
|
---|
| 407 | UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
|
---|
| 408 | if ( Type * type = expr->get_result() ) {
|
---|
| 409 | Type * base = InitTweak::getPointerBase( type );
|
---|
[0698aa1] | 410 | assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
|
---|
[b0440b7] | 411 | ret->set_result( base->clone() );
|
---|
| 412 | if ( GenPoly::referencesPermissable() ) {
|
---|
| 413 | // if references are still allowed in the AST, dereference returns a reference
|
---|
| 414 | ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
|
---|
| 415 | } else {
|
---|
| 416 | // references have been removed, in which case dereference returns an lvalue of the base type.
|
---|
[0690350] | 417 | ret->result->set_lvalue( true );
|
---|
[b0440b7] | 418 | }
|
---|
[b3b2077] | 419 | }
|
---|
| 420 | return ret;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
|
---|
| 424 | assert( arg1 && arg2 );
|
---|
| 425 | UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
|
---|
| 426 | if ( arg1->get_result() && arg2->get_result() ) {
|
---|
| 427 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
|
---|
| 428 | // so the result is the type of the RHS
|
---|
| 429 | ret->set_result( arg2->get_result()->clone() );
|
---|
| 430 | }
|
---|
| 431 | return ret;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 |
|
---|
[50377a4] | 435 | void UntypedExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[89231bc] | 436 | os << "Applying untyped: " << std::endl;
|
---|
[50377a4] | 437 | os << indent+1;
|
---|
| 438 | function->print(os, indent+1);
|
---|
| 439 | os << std::endl << indent << "...to: " << std::endl;
|
---|
| 440 | printAll(args, os, indent+1);
|
---|
[0dd3a2f] | 441 | Expression::print( os, indent );
|
---|
[51b73452] | 442 | }
|
---|
| 443 |
|
---|
[bf4b4cf] | 444 | NameExpr::NameExpr( std::string name ) : Expression(), name(name) {
|
---|
[50377a4] | 445 | assertf(name != "0", "Zero is not a valid name");
|
---|
| 446 | assertf(name != "1", "One is not a valid name");
|
---|
[4cb935e] | 447 | }
|
---|
[51b73452] | 448 |
|
---|
[0dd3a2f] | 449 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
|
---|
[51b73452] | 450 | }
|
---|
| 451 |
|
---|
| 452 | NameExpr::~NameExpr() {}
|
---|
| 453 |
|
---|
[50377a4] | 454 | void NameExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 455 | os << "Name: " << get_name();
|
---|
[0dd3a2f] | 456 | Expression::print( os, indent );
|
---|
[51b73452] | 457 | }
|
---|
| 458 |
|
---|
[bf4b4cf] | 459 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) :
|
---|
| 460 | Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) {
|
---|
[906e24d] | 461 | set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
|
---|
[51b73452] | 462 | }
|
---|
| 463 |
|
---|
[0dd3a2f] | 464 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
|
---|
| 465 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
|
---|
[51b73452] | 466 | }
|
---|
| 467 |
|
---|
[a08ba92] | 468 | LogicalExpr::~LogicalExpr() {
|
---|
[0dd3a2f] | 469 | delete arg1;
|
---|
| 470 | delete arg2;
|
---|
[51b73452] | 471 | }
|
---|
| 472 |
|
---|
[50377a4] | 473 | void LogicalExpr::print( std::ostream &os, Indenter indent )const {
|
---|
| 474 | os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: ";
|
---|
[0dd3a2f] | 475 | arg1->print(os);
|
---|
| 476 | os << " and ";
|
---|
| 477 | arg2->print(os);
|
---|
| 478 | Expression::print( os, indent );
|
---|
[51b73452] | 479 | }
|
---|
| 480 |
|
---|
[bf4b4cf] | 481 | ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) :
|
---|
| 482 | Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {}
|
---|
[51b73452] | 483 |
|
---|
[0dd3a2f] | 484 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
|
---|
| 485 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
|
---|
| 486 | }
|
---|
[51b73452] | 487 |
|
---|
| 488 | ConditionalExpr::~ConditionalExpr() {
|
---|
[0dd3a2f] | 489 | delete arg1;
|
---|
| 490 | delete arg2;
|
---|
| 491 | delete arg3;
|
---|
[51b73452] | 492 | }
|
---|
| 493 |
|
---|
[50377a4] | 494 | void ConditionalExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 495 | os << "Conditional expression on: " << std::endl << indent+1;
|
---|
| 496 | arg1->print( os, indent+1 );
|
---|
| 497 | os << indent << "First alternative:" << std::endl << indent+1;
|
---|
| 498 | arg2->print( os, indent+1 );
|
---|
| 499 | os << indent << "Second alternative:" << std::endl << indent+1;
|
---|
| 500 | arg3->print( os, indent+1 );
|
---|
[0dd3a2f] | 501 | Expression::print( os, indent );
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[e6cee92] | 504 | AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
|
---|
[3be261a] | 505 |
|
---|
| 506 |
|
---|
[50377a4] | 507 | void AsmExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[7f5566b] | 508 | os << "Asm Expression: " << std::endl;
|
---|
[50377a4] | 509 | if ( inout ) inout->print( os, indent+1 );
|
---|
| 510 | if ( constraint ) constraint->print( os, indent+1 );
|
---|
| 511 | if ( operand ) operand->print( os, indent+1 );
|
---|
[7f5566b] | 512 | }
|
---|
| 513 |
|
---|
[db4ecc5] | 514 |
|
---|
| 515 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
|
---|
| 516 | assert( callExpr );
|
---|
[50377a4] | 517 | assert( callExpr->result );
|
---|
[906e24d] | 518 | set_result( callExpr->get_result()->clone() );
|
---|
[db4ecc5] | 519 | }
|
---|
| 520 |
|
---|
| 521 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
|
---|
| 522 | cloneAll( other.tempDecls, tempDecls );
|
---|
| 523 | cloneAll( other.returnDecls, returnDecls );
|
---|
| 524 | cloneAll( other.dtors, dtors );
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
|
---|
[d5556a3] | 528 | set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
|
---|
[db4ecc5] | 529 | delete callExpr;
|
---|
| 530 | deleteAll( tempDecls );
|
---|
| 531 | deleteAll( returnDecls );
|
---|
| 532 | deleteAll( dtors );
|
---|
| 533 | }
|
---|
| 534 |
|
---|
[50377a4] | 535 | void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 536 | os << "Implicit Copy Constructor Expression: " << std::endl << indent+1;
|
---|
| 537 | callExpr->print( os, indent+1 );
|
---|
| 538 | os << std::endl << indent << "... with temporaries:" << std::endl;
|
---|
| 539 | printAll( tempDecls, os, indent+1 );
|
---|
| 540 | os << std::endl << indent << "... with return temporaries:" << std::endl;
|
---|
| 541 | printAll( returnDecls, os, indent+1 );
|
---|
[db4ecc5] | 542 | Expression::print( os, indent );
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[3be261a] | 545 |
|
---|
[b6fe7e6] | 546 | ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
|
---|
| 547 | // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
|
---|
| 548 | assert( callExpr );
|
---|
| 549 | Expression * arg = InitTweak::getCallArg( callExpr, 0 );
|
---|
| 550 | assert( arg );
|
---|
[50377a4] | 551 | set_result( maybeClone( arg->result ) );
|
---|
[b6fe7e6] | 552 | }
|
---|
[3be261a] | 553 |
|
---|
[b6fe7e6] | 554 | ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | ConstructorExpr::~ConstructorExpr() {
|
---|
| 558 | delete callExpr;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[50377a4] | 561 | void ConstructorExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 562 | os << "Constructor Expression: " << std::endl << indent+1;
|
---|
[b6fe7e6] | 563 | callExpr->print( os, indent + 2 );
|
---|
| 564 | Expression::print( os, indent );
|
---|
[0dd3a2f] | 565 | }
|
---|
| 566 |
|
---|
[3be261a] | 567 |
|
---|
[fbcde64] | 568 | CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
|
---|
[3c13c03] | 569 | assert( type && initializer );
|
---|
[5ccb10d] | 570 | type->set_lvalue( true );
|
---|
[fbcde64] | 571 | set_result( type );
|
---|
[630a82a] | 572 | }
|
---|
| 573 |
|
---|
[fbcde64] | 574 | CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {}
|
---|
[630a82a] | 575 |
|
---|
| 576 | CompoundLiteralExpr::~CompoundLiteralExpr() {
|
---|
| 577 | delete initializer;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[50377a4] | 580 | void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 581 | os << "Compound Literal Expression: " << std::endl << indent+1;
|
---|
| 582 | result->print( os, indent+1 );
|
---|
| 583 | os << indent+1;
|
---|
| 584 | initializer->print( os, indent+1 );
|
---|
[d5556a3] | 585 | Expression::print( os, indent );
|
---|
[630a82a] | 586 | }
|
---|
| 587 |
|
---|
[d9e2280] | 588 | RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
|
---|
[3c13c03] | 589 | RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
|
---|
[50377a4] | 590 | void RangeExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
[3c13c03] | 591 | os << "Range Expression: ";
|
---|
[8688ce1] | 592 | low->print( os, indent );
|
---|
| 593 | os << " ... ";
|
---|
| 594 | high->print( os, indent );
|
---|
[d5556a3] | 595 | Expression::print( os, indent );
|
---|
[8688ce1] | 596 | }
|
---|
[3be261a] | 597 |
|
---|
[6eb8948] | 598 | StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
|
---|
[5e2c348] | 599 | computeResult();
|
---|
| 600 | }
|
---|
| 601 | StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
|
---|
| 602 | cloneAll( other.returnDecls, returnDecls );
|
---|
| 603 | cloneAll( other.dtors, dtors );
|
---|
| 604 | }
|
---|
| 605 | StmtExpr::~StmtExpr() {
|
---|
| 606 | delete statements;
|
---|
| 607 | deleteAll( dtors );
|
---|
| 608 | deleteAll( returnDecls );
|
---|
| 609 | }
|
---|
| 610 | void StmtExpr::computeResult() {
|
---|
[6eb8948] | 611 | assert( statements );
|
---|
[5e2c348] | 612 | std::list< Statement * > & body = statements->kids;
|
---|
| 613 | delete result;
|
---|
| 614 | result = nullptr;
|
---|
[0992849] | 615 | if ( ! returnDecls.empty() ) {
|
---|
| 616 | // prioritize return decl for result type, since if a return decl exists, then
|
---|
| 617 | // the StmtExpr is currently in an intermediate state where the body will always
|
---|
| 618 | // give a void result type.
|
---|
| 619 | result = returnDecls.front()->get_type()->clone();
|
---|
| 620 | } else if ( ! body.empty() ) {
|
---|
[6eb8948] | 621 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
|
---|
[9fe33947] | 622 | result = maybeClone( exprStmt->expr->result );
|
---|
[6eb8948] | 623 | }
|
---|
| 624 | }
|
---|
[07516b56] | 625 | // ensure that StmtExpr has a result type
|
---|
| 626 | if ( ! result ) {
|
---|
[9fe33947] | 627 | result = new VoidType( Type::Qualifiers() );
|
---|
[07516b56] | 628 | }
|
---|
[6eb8948] | 629 | }
|
---|
[50377a4] | 630 | void StmtExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 631 | os << "Statement Expression: " << std::endl << indent+1;
|
---|
| 632 | statements->print( os, indent+1 );
|
---|
[d5556a3] | 633 | if ( ! returnDecls.empty() ) {
|
---|
[50377a4] | 634 | os << indent+1 << "... with returnDecls: ";
|
---|
| 635 | printAll( returnDecls, os, indent+1 );
|
---|
[d5556a3] | 636 | }
|
---|
| 637 | if ( ! dtors.empty() ) {
|
---|
[50377a4] | 638 | os << indent+1 << "... with dtors: ";
|
---|
| 639 | printAll( dtors, os, indent+1 );
|
---|
[d5556a3] | 640 | }
|
---|
| 641 | Expression::print( os, indent );
|
---|
[6eb8948] | 642 | }
|
---|
| 643 |
|
---|
[3c13c03] | 644 |
|
---|
[bf32bb8] | 645 | long long UniqueExpr::count = 0;
|
---|
[141b786] | 646 | UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
|
---|
| 647 | assert( expr );
|
---|
[bf32bb8] | 648 | assert( count != -1 );
|
---|
| 649 | if ( id == -1 ) id = count++;
|
---|
| 650 | if ( expr->get_result() ) {
|
---|
| 651 | set_result( expr->get_result()->clone() );
|
---|
| 652 | }
|
---|
[3c13c03] | 653 | }
|
---|
[141b786] | 654 | UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
|
---|
[3c13c03] | 655 | }
|
---|
| 656 | UniqueExpr::~UniqueExpr() {
|
---|
[141b786] | 657 | delete expr;
|
---|
| 658 | delete object;
|
---|
| 659 | delete var;
|
---|
[3c13c03] | 660 | }
|
---|
[50377a4] | 661 | void UniqueExpr::print( std::ostream &os, Indenter indent ) const {
|
---|
| 662 | os << "Unique Expression with id:" << id << std::endl << indent+1;
|
---|
| 663 | expr->print( os, indent+1 );
|
---|
| 664 | if ( object ) {
|
---|
| 665 | os << indent << "... with decl: ";
|
---|
| 666 | get_object()->printShort( os, indent+1 );
|
---|
[77971f6] | 667 | }
|
---|
[d5556a3] | 668 | Expression::print( os, indent );
|
---|
[3c13c03] | 669 | }
|
---|
| 670 |
|
---|
[e4d829b] | 671 | InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
|
---|
| 672 | InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
|
---|
| 673 | InitAlternative::~InitAlternative() {
|
---|
| 674 | delete type;
|
---|
| 675 | delete designation;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
|
---|
| 679 | UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
|
---|
| 680 | UntypedInitExpr::~UntypedInitExpr() {
|
---|
| 681 | delete expr;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
[50377a4] | 684 | void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const {
|
---|
| 685 | os << "Untyped Init Expression" << std::endl << indent+1;
|
---|
| 686 | expr->print( os, indent+1 );
|
---|
[e4d829b] | 687 | if ( ! initAlts.empty() ) {
|
---|
| 688 | for ( const InitAlternative & alt : initAlts ) {
|
---|
[50377a4] | 689 | os << indent+1 << "InitAlternative: ";
|
---|
| 690 | alt.type->print( os, indent+1 );
|
---|
| 691 | alt.designation->print( os, indent+1 );
|
---|
[e4d829b] | 692 | }
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[62423350] | 696 | InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
|
---|
| 697 | set_result( expr->get_result()->clone() );
|
---|
[e4d829b] | 698 | }
|
---|
| 699 | InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
|
---|
| 700 | InitExpr::~InitExpr() {
|
---|
| 701 | delete expr;
|
---|
| 702 | delete designation;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[50377a4] | 705 | void InitExpr::print( std::ostream & os, Indenter indent ) const {
|
---|
| 706 | os << "Init Expression" << std::endl << indent+1;
|
---|
| 707 | expr->print( os, indent+1 );
|
---|
| 708 | os << indent+1 << "... with designation: ";
|
---|
| 709 | designation->print( os, indent+1 );
|
---|
[e4d829b] | 710 | }
|
---|
| 711 |
|
---|
[0dd3a2f] | 712 | // Local Variables: //
|
---|
| 713 | // tab-width: 4 //
|
---|
| 714 | // mode: c++ //
|
---|
| 715 | // compile-command: "make install" //
|
---|
| 716 | // End: //
|
---|