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