source: src/AST/Expr.hpp@ c6c682cf

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c6c682cf was e7d6968, checked in by Fangren Yu <f37yu@…>, 5 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc into master

  • Property mode set to 100644
File size: 26.8 KB
RevLine 
[79f7875]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// Expr.hpp --
8//
9// Author : Aaron B. Moss
10// Created On : Fri May 10 10:30:00 2019
[312029a]11// Last Modified By : Peter A. Buhr
[79f7875]12// Created On : Fri May 10 10:30:00 2019
[312029a]13// Update Count : 7
[79f7875]14//
15
16#pragma once
17
18#include <cassert>
[60aaa51d]19#include <deque>
[79f7875]20#include <map>
[54e41b3]21#include <string>
[79f7875]22#include <utility> // for move
23#include <vector>
[c36298d]24#include <optional>
[79f7875]25
26#include "Fwd.hpp" // for UniqueId
[54e41b3]27#include "Label.hpp"
[312029a]28#include "Decl.hpp"
[79f7875]29#include "ParseNode.hpp"
[264e691]30#include "Visitor.hpp"
[79f7875]31
[f3cc5b6]32// Must be included in *all* AST classes; should be #undef'd at the end of the file
[99da267]33#define MUTATE_FRIEND \
34 template<typename node_t> friend node_t * mutate(const node_t * node); \
35 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
36
[f3cc5b6]37
[20de6fb]38class ConverterOldToNew;
[c36298d]39class ConverterNewToOld;
[20de6fb]40
[79f7875]41namespace ast {
42
[6d51bd7]43/// Contains the ID of a declaration and a type that is derived from that declaration,
[79f7875]44/// but subject to decay-to-pointer and type parameter renaming
45struct ParamEntry {
46 UniqueId decl;
[07d867b]47 readonly<Decl> declptr;
[79f7875]48 ptr<Type> actualType;
49 ptr<Type> formalType;
50 ptr<Expr> expr;
51
[aaeacf4]52 ParamEntry() : decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {}
[7870799]53 ParamEntry(
54 UniqueId id, const Decl * declptr, const Type * actual, const Type * formal,
[b69233ac]55 const Expr * e )
[aaeacf4]56 : decl( id ), declptr( declptr ), actualType( actual ), formalType( formal ), expr( e ) {}
[79f7875]57};
58
59/// Pre-resolution list of parameters to infer
60using ResnSlots = std::vector<UniqueId>;
61/// Post-resolution map of inferred parameters
62using InferredParams = std::map< UniqueId, ParamEntry >;
63
64/// Base node for expressions
65class Expr : public ParseNode {
66public:
[07d867b]67 /*
68 * NOTE: the union approach is incorrect until the case of
69 * partial resolution in InferMatcher is eliminated.
70 * it is reverted to allow unresolved and resolved parameters
71 * to coexist in an expression node.
72 */
[79f7875]73 struct InferUnion {
[07d867b]74 // mode is now unused
[79f7875]75 enum { Empty, Slots, Params } mode;
[07d867b]76 struct data_t {
77 // char def;
78 ResnSlots * resnSlots;
79 InferredParams * inferParams;
80
81 data_t(): resnSlots(nullptr), inferParams(nullptr) {}
82 data_t(const data_t &other) = delete;
83 ~data_t() {
84 delete resnSlots;
85 delete inferParams;
86 }
[79f7875]87 } data;
88
89 /// initializes from other InferUnion
90 void init_from( const InferUnion& o ) {
[07d867b]91 if (o.data.resnSlots) {
92 data.resnSlots = new ResnSlots(*o.data.resnSlots);
93 }
94 if (o.data.inferParams) {
95 data.inferParams = new InferredParams(*o.data.inferParams);
[79f7875]96 }
97 }
98
99 /// initializes from other InferUnion (move semantics)
100 void init_from( InferUnion&& o ) {
[07d867b]101 data.resnSlots = o.data.resnSlots;
102 data.inferParams = o.data.inferParams;
103 o.data.resnSlots = nullptr;
104 o.data.inferParams = nullptr;
[79f7875]105 }
106
107 InferUnion() : mode(Empty), data() {}
108 InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); }
109 InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); }
110 InferUnion& operator= ( const InferUnion& ) = delete;
111 InferUnion& operator= ( InferUnion&& ) = delete;
[07d867b]112
113 bool hasSlots() const { return data.resnSlots; }
[79f7875]114
115 ResnSlots& resnSlots() {
[07d867b]116 if (!data.resnSlots) {
117 data.resnSlots = new ResnSlots();
[79f7875]118 }
[07d867b]119 return *data.resnSlots;
[19e567dd]120 }
121
[60aaa51d]122 const ResnSlots& resnSlots() const {
[07d867b]123 if (data.resnSlots) {
124 return *data.resnSlots;
[19e567dd]125 }
[7870799]126 assertf(false, "Mode was not already resnSlots");
127 abort();
[79f7875]128 }
129
130 InferredParams& inferParams() {
[07d867b]131 if (!data.inferParams) {
132 data.inferParams = new InferredParams();
[79f7875]133 }
[07d867b]134 return *data.inferParams;
[19e567dd]135 }
136
[60aaa51d]137 const InferredParams& inferParams() const {
[07d867b]138 if (data.inferParams) {
139 return *data.inferParams;
[19e567dd]140 }
[7870799]141 assertf(false, "Mode was not already Params");
142 abort();
[79f7875]143 }
[60aaa51d]144
[07d867b]145 void set_inferParams( InferredParams * ps ) {
146 delete data.resnSlots;
147 data.resnSlots = nullptr;
148 delete data.inferParams;
149 data.inferParams = ps;
[b69233ac]150 }
151
[aaeacf4]152 /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode
[60aaa51d]153 /// and the other is in `Params`.
154 void splice( InferUnion && o ) {
[07d867b]155 if (o.data.resnSlots) {
156 if (data.resnSlots) {
157 data.resnSlots->insert(
158 data.resnSlots->end(), o.data.resnSlots->begin(), o.data.resnSlots->end() );
159 delete o.data.resnSlots;
160 }
161 else {
162 data.resnSlots = o.data.resnSlots;
[60aaa51d]163 }
[07d867b]164 o.data.resnSlots = nullptr;
165 }
166
167 if (o.data.inferParams) {
168 if (data.inferParams) {
169 for ( const auto & p : *o.data.inferParams ) {
170 (*data.inferParams)[p.first] = std::move(p.second);
171 }
172 delete o.data.inferParams;
173 }
174 else {
175 data.inferParams = o.data.inferParams;
176 }
177 o.data.inferParams = nullptr;
178 }
[60aaa51d]179 }
[79f7875]180 };
181
182 ptr<Type> result;
183 ptr<TypeSubstitution> env;
184 InferUnion inferred;
185 bool extension = false;
186
[54e41b3]187 Expr( const CodeLocation & loc, const Type * res = nullptr )
188 : ParseNode( loc ), result( res ), env(), inferred() {}
[79f7875]189
[9e1d485]190 Expr * set_extension( bool ex ) { extension = ex; return this; }
[cf32116]191 virtual bool get_lvalue() const;
[79f7875]192
[69bafd2]193 virtual const Expr * accept( Visitor & v ) const override = 0;
[79f7875]194private:
[23f99e1]195 Expr * clone() const override = 0;
[f3cc5b6]196 MUTATE_FRIEND
[264e691]197};
198
[87701b6]199/// The application of a function to a set of parameters.
[54e41b3]200/// Post-resolver form of `UntypedExpr`
201class ApplicationExpr final : public Expr {
202public:
203 ptr<Expr> func;
204 std::vector<ptr<Expr>> args;
205
206 ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} );
207
[cf32116]208 bool get_lvalue() const final;
209
[54e41b3]210 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
211private:
212 ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; }
[f3cc5b6]213 MUTATE_FRIEND
[54e41b3]214};
215
216/// The application of a function to a set of parameters, pre-overload resolution.
217class UntypedExpr final : public Expr {
218public:
219 ptr<Expr> func;
220 std::vector<ptr<Expr>> args;
221
222 UntypedExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} )
223 : Expr( loc ), func( f ), args( std::move(as) ) {}
224
[cf32116]225 bool get_lvalue() const final;
226
[54e41b3]227 /// Creates a new dereference expression
[490fb92e]228 static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg );
[54e41b3]229 /// Creates a new assignment expression
[490fb92e]230 static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs );
[54e41b3]231
232 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
233private:
234 UntypedExpr * clone() const override { return new UntypedExpr{ *this }; }
[f3cc5b6]235 MUTATE_FRIEND
[54e41b3]236};
237
238/// A name whose name is as-yet undetermined.
239/// May also be used to avoid name mangling in codegen phase.
240class NameExpr final : public Expr {
241public:
242 std::string name;
243
244 NameExpr( const CodeLocation & loc, const std::string & n ) : Expr( loc ), name( n ) {}
245
246 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
247private:
248 NameExpr * clone() const override { return new NameExpr{ *this }; }
[f3cc5b6]249 MUTATE_FRIEND
[54e41b3]250};
251
[d5631b3]252/// A reference to a named variable.
253class VariableExpr final : public Expr {
254public:
255 readonly<DeclWithType> var;
256
257 VariableExpr( const CodeLocation & loc );
258 VariableExpr( const CodeLocation & loc, const DeclWithType * v );
259
260 bool get_lvalue() const final;
261
262 /// generates a function pointer for a given function
263 static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl );
264
265 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
266private:
267 VariableExpr * clone() const override { return new VariableExpr{ *this }; }
268 MUTATE_FRIEND
269};
270
[54e41b3]271/// Address-of expression `&e`
272class AddressExpr final : public Expr {
273public:
274 ptr<Expr> arg;
275
276 AddressExpr( const CodeLocation & loc, const Expr * a );
277
[b8524ca]278 /// Generate AddressExpr wrapping given expression at same location
279 AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {}
280
[54e41b3]281 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
282private:
283 AddressExpr * clone() const override { return new AddressExpr{ *this }; }
[f3cc5b6]284 MUTATE_FRIEND
[54e41b3]285};
286
287/// GCC &&label
288/// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html
289class LabelAddressExpr final : public Expr {
290public:
291 Label arg;
292
293 LabelAddressExpr( const CodeLocation & loc, Label && a );
294
295 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
296private:
297 LabelAddressExpr * clone() const override { return new LabelAddressExpr{ *this }; }
[f3cc5b6]298 MUTATE_FRIEND
[54e41b3]299};
300
301/// Whether a cast existed in the program source or not
302enum GeneratedFlag { ExplicitCast, GeneratedCast };
303
304/// A type cast, e.g. `(int)e`
305class CastExpr final : public Expr {
306public:
307 ptr<Expr> arg;
308 GeneratedFlag isGenerated;
309
[87701b6]310 CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
[54e41b3]311 GeneratedFlag g = GeneratedCast ) : Expr( loc, to ), arg( a ), isGenerated( g ) {}
312 /// Cast-to-void
313 CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast );
314
[b8524ca]315 /// Wrap a cast expression around an existing expression (always generated)
316 CastExpr( const Expr * a, const Type * to ) : CastExpr( a->location, a, to, GeneratedCast ) {}
317
318 /// Wrap a cast-to-void expression around an existing expression (always generated)
319 CastExpr( const Expr * a ) : CastExpr( a->location, a, GeneratedCast ) {}
320
[cf32116]321 bool get_lvalue() const final;
322
[54e41b3]323 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
324private:
325 CastExpr * clone() const override { return new CastExpr{ *this }; }
[f3cc5b6]326 MUTATE_FRIEND
[54e41b3]327};
328
329/// A cast to "keyword types", e.g. `(thread &)t`
330class KeywordCastExpr final : public Expr {
331public:
332 ptr<Expr> arg;
[4ef08f7]333 struct Concrete {
334 std::string field;
335 std::string getter;
336
337 Concrete() = default;
338 Concrete(const Concrete &) = default;
339 };
[312029a]340 ast::AggregateDecl::Aggregate target;
[4ef08f7]341 Concrete concrete_target;
342
[54e41b3]343
[312029a]344 KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
[54e41b3]345 : Expr( loc ), arg( a ), target( t ) {}
346
[4ef08f7]347 KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t, const Concrete & ct )
348 : Expr( loc ), arg( a ), target( t ), concrete_target( ct ) {}
349
[54e41b3]350 /// Get a name for the target type
[312029a]351 const char * targetString() const;
[54e41b3]352
353 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
354private:
355 KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
[f3cc5b6]356 MUTATE_FRIEND
[54e41b3]357};
358
359/// A virtual dynamic cast, e.g. `(virtual exception)e`
360class VirtualCastExpr final : public Expr {
361public:
362 ptr<Expr> arg;
363
364 VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to )
365 : Expr( loc, to ), arg( a ) {}
366
367 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
368private:
369 VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; }
[f3cc5b6]370 MUTATE_FRIEND
[54e41b3]371};
372
373/// A member selection operation before expression resolution, e.g. `q.p`
374class UntypedMemberExpr final : public Expr {
375public:
376 ptr<Expr> member;
377 ptr<Expr> aggregate;
378
379 UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg )
380 : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); }
381
[cf32116]382 bool get_lvalue() const final;
383
[54e41b3]384 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
385private:
386 UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
[f3cc5b6]387 MUTATE_FRIEND
[54e41b3]388};
389
390/// A member selection operation after expression resolution, e.g. `q.p`
391class MemberExpr final : public Expr {
392public:
393 readonly<DeclWithType> member;
394 ptr<Expr> aggregate;
395
396 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg );
397
[cf32116]398 bool get_lvalue() const final;
399
[54e41b3]400 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
401private:
402 MemberExpr * clone() const override { return new MemberExpr{ *this }; }
[f3cc5b6]403 MUTATE_FRIEND
[ae265b55]404
405 // Custructor overload meant only for AST conversion
406 enum NoOpConstruction { NoOpConstructionChosen };
407 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg,
408 NoOpConstruction overloadSelector );
409 friend class ::ConverterOldToNew;
410 friend class ::ConverterNewToOld;
[54e41b3]411};
412
[c36298d]413/// A compile-time constant.
414/// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted.
415/// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
[54e41b3]416class ConstantExpr final : public Expr {
417public:
[c36298d]418 // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
[54e41b3]419 std::string rep;
420
[87701b6]421 ConstantExpr(
[7870799]422 const CodeLocation & loc, const Type * ty, const std::string & r,
[c36298d]423 std::optional<unsigned long long> i )
[490fb92e]424 : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
[87701b6]425
[c36298d]426 /// Gets the integer value of this constant, if one is appropriate to its type.
427 /// Throws a SemanticError if the type is not appropriate for value-as-integer.
428 /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
[54e41b3]429 long long int intValue() const;
430
431 /// generates a boolean constant of the given bool
432 static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
433 /// generates an integer constant of the given int
434 static ConstantExpr * from_int( const CodeLocation & loc, int i );
435 /// generates an integer constant of the given unsigned long int
436 static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
437 /// generates a null pointer value for the given type. void * if omitted.
438 static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
439
440 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
441private:
442 ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
[f3cc5b6]443 MUTATE_FRIEND
[c36298d]444
445 std::optional<unsigned long long> ival;
446
447 // Intended only for legacy support of roundtripping the old AST.
448 // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
449 // In the old AST it's constExpr->constant.type
450 ptr<Type> underlyer;
451 friend class ::ConverterOldToNew;
452 friend class ::ConverterNewToOld;
[54e41b3]453};
454
455/// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
456class SizeofExpr final : public Expr {
457public:
458 ptr<Expr> expr;
459 ptr<Type> type;
460
461 SizeofExpr( const CodeLocation & loc, const Expr * e );
462 SizeofExpr( const CodeLocation & loc, const Type * t );
463 // deliberately no disambiguating overload for nullptr_t
464
465 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
466private:
467 SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
[f3cc5b6]468 MUTATE_FRIEND
[54e41b3]469};
470
471/// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
472class AlignofExpr final : public Expr {
473public:
474 ptr<Expr> expr;
475 ptr<Type> type;
476
477 AlignofExpr( const CodeLocation & loc, const Expr * e );
478 AlignofExpr( const CodeLocation & loc, const Type * t );
479 // deliberately no disambiguating overload for nullptr_t
480
481 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
482private:
483 AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
[f3cc5b6]484 MUTATE_FRIEND
[54e41b3]485};
486
487/// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
488class UntypedOffsetofExpr final : public Expr {
489public:
490 ptr<Type> type;
491 std::string member;
492
493 UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
494 : Expr( loc ), type( ty ), member( mem ) {}
495
496 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
497private:
498 UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
[f3cc5b6]499 MUTATE_FRIEND
[54e41b3]500};
501
502/// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
503class OffsetofExpr final : public Expr {
504public:
505 ptr<Type> type;
506 readonly<DeclWithType> member;
507
508 OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
509
510 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
511private:
512 OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
[f3cc5b6]513 MUTATE_FRIEND
[54e41b3]514};
515
516/// a pack of field-offsets for a generic type
517class OffsetPackExpr final : public Expr {
518public:
519 ptr<StructInstType> type;
520
521 OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
522
523 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
524private:
525 OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
[f3cc5b6]526 MUTATE_FRIEND
[54e41b3]527};
528
529/// Variants of short-circuiting logical expression
530enum LogicalFlag { OrExpr, AndExpr };
531
532/// Short-circuiting boolean expression (`&&` or `||`)
533class LogicalExpr final : public Expr {
534public:
535 ptr<Expr> arg1;
536 ptr<Expr> arg2;
537 LogicalFlag isAnd;
538
539 LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
540
541 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
542private:
543 LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
[f3cc5b6]544 MUTATE_FRIEND
[54e41b3]545};
546
547/// Three-argument conditional e.g. `p ? a : b`
548class ConditionalExpr final : public Expr {
549public:
550 ptr<Expr> arg1;
551 ptr<Expr> arg2;
552 ptr<Expr> arg3;
553
554 ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
555 : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
556
557 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
558private:
559 ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
[f3cc5b6]560 MUTATE_FRIEND
[54e41b3]561};
562
563/// Comma expression e.g. `( a , b )`
564class CommaExpr final : public Expr {
565public:
566 ptr<Expr> arg1;
567 ptr<Expr> arg2;
568
[87701b6]569 CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
[4e13e2a]570 : Expr( loc ), arg1( a1 ), arg2( a2 ) {
571 this->result = a2->result;
572 }
[54e41b3]573
[cf32116]574 bool get_lvalue() const final;
575
[54e41b3]576 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
577private:
578 CommaExpr * clone() const override { return new CommaExpr{ *this }; }
[f3cc5b6]579 MUTATE_FRIEND
[54e41b3]580};
581
[264e691]582/// A type used as an expression (e.g. a type generator parameter)
583class TypeExpr final : public Expr {
584public:
585 ptr<Type> type;
586
587 TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
588
[69bafd2]589 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
[264e691]590private:
591 TypeExpr * clone() const override { return new TypeExpr{ *this }; }
[f3cc5b6]592 MUTATE_FRIEND
[79f7875]593};
594
[54e41b3]595/// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
596/// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
597class AsmExpr final : public Expr {
598public:
[665f432]599 std::string inout;
[9b4f329]600 ptr<Expr> constraint;
601 ptr<Expr> operand;
602
[665f432]603 AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
[9b4f329]604 : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
605
606 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
607private:
608 AsmExpr * clone() const override { return new AsmExpr{ *this }; }
609 MUTATE_FRIEND
610};
611
[17a0228a]612/// The application of a function to a set of parameters, along with a set of copy constructor
[9b4f329]613/// calls, one for each argument
614class ImplicitCopyCtorExpr final : public Expr {
615public:
616 ptr<ApplicationExpr> callExpr;
617
618 ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
[490fb92e]619 : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
[9b4f329]620
621 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
622private:
623 ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
624 MUTATE_FRIEND
625};
626
627/// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
628class ConstructorExpr final : public Expr {
629public:
630 ptr<Expr> callExpr;
631
632 ConstructorExpr( const CodeLocation & loc, const Expr * call );
633
634 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
635private:
636 ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
637 MUTATE_FRIEND
638};
639
640/// A C99 compound literal, e.g. `(MyType){ a, b, c }`
641class CompoundLiteralExpr final : public Expr {
642public:
643 ptr<Init> init;
644
645 CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
646
[cf32116]647 bool get_lvalue() const final;
648
[9b4f329]649 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
650private:
651 CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
652 MUTATE_FRIEND
653};
654
655/// A range, e.g. `3 ... 5` or `1~10`
656class RangeExpr final : public Expr {
657public:
658 ptr<Expr> low;
659 ptr<Expr> high;
660
661 RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
662 : Expr( loc ), low( l ), high( h ) {}
663
664 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
665private:
666 RangeExpr * clone() const override { return new RangeExpr{ *this }; }
667 MUTATE_FRIEND
668};
669
670/// A tuple expression before resolution, e.g. `[a, b, c]`
671class UntypedTupleExpr final : public Expr {
672public:
673 std::vector<ptr<Expr>> exprs;
674
675 UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
676 : Expr( loc ), exprs( std::move(xs) ) {}
677
678 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
679private:
680 UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
681 MUTATE_FRIEND
682};
683
684/// A tuple expression after resolution, e.g. `[a, b, c]`
685class TupleExpr final : public Expr {
686public:
687 std::vector<ptr<Expr>> exprs;
688
689 TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
690
691 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
692private:
693 TupleExpr * clone() const override { return new TupleExpr{ *this }; }
694 MUTATE_FRIEND
695};
696
697/// An element selection operation on a tuple value, e.g. `t.3` after analysis
698class TupleIndexExpr final : public Expr {
699public:
700 ptr<Expr> tuple;
701 unsigned index;
702
703 TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
704
[cf32116]705 bool get_lvalue() const final;
706
[9b4f329]707 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
708private:
709 TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
710 MUTATE_FRIEND
711};
712
[17a0228a]713/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
714/// multiple-assignment: both sides of the assignment have tuple type,
[9b4f329]715/// e.g. `[a, b, c] = [d, e, f];`
716/// mass-assignment: left-hand side has tuple type and right-hand side does not:
717/// e.g. `[a, b, c] = 42;`
718class TupleAssignExpr final : public Expr {
719public:
720 ptr<StmtExpr> stmtExpr;
721
[17a0228a]722 TupleAssignExpr(
723 const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
[9b4f329]724 std::vector<ptr<ObjectDecl>> && tempDecls );
[17a0228a]725
[9b4f329]726 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
[20de6fb]727
728 friend class ::ConverterOldToNew;
729
[9b4f329]730private:
731 TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
[20de6fb]732 TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
733
[9b4f329]734 MUTATE_FRIEND
735};
736
737/// A GCC "statement expression", e.g. `({ int x = 5; x })`
738class StmtExpr final : public Expr {
739public:
740 ptr<CompoundStmt> stmts;
741 std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
742 std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
743
[490fb92e]744 readonly<ExprStmt> resultExpr;
745
[9b4f329]746 StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
747
748 /// Set the result type of this StmtExpr based on its body
749 void computeResult();
750
751 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
752private:
753 StmtExpr * clone() const override { return new StmtExpr{ *this }; }
754 MUTATE_FRIEND
755};
756
757/// An expression which must only be evaluated once
758class UniqueExpr final : public Expr {
759 static unsigned long long nextId;
760public:
761 ptr<Expr> expr;
762 ptr<ObjectDecl> object;
763 ptr<VariableExpr> var;
764 unsigned long long id;
765
[d76f32c]766 UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
[9b4f329]767
768 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
769private:
770 UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
771 MUTATE_FRIEND
772};
773
774/// One option for resolving an initializer expression
775struct InitAlternative {
776 ptr<Type> type;
777 ptr<Designation> designation;
778
779 InitAlternative() = default;
780 InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
781};
782
783/// Pre-resolution initializer expression
784class UntypedInitExpr final : public Expr {
785public:
786 ptr<Expr> expr;
[60aaa51d]787 std::deque<InitAlternative> initAlts;
[9b4f329]788
[60aaa51d]789 UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
[9b4f329]790 : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
791
792 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
793private:
794 UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
795 MUTATE_FRIEND
796};
797
798/// Post-resolution initializer expression
799class InitExpr final : public Expr {
800public:
801 ptr<Expr> expr;
802 ptr<Designation> designation;
803
804 InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
805 : Expr( loc, e->result ), expr( e ), designation( des ) {}
806
807 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
808private:
809 InitExpr * clone() const override { return new InitExpr{ *this }; }
810 MUTATE_FRIEND
811};
812
813/// Expression containing a deleted identifier.
814/// Internal to resolver.
815class DeletedExpr final : public Expr {
816public:
817 ptr<Expr> expr;
[e67991f]818 readonly<Decl> deleteStmt;
[9b4f329]819
[e67991f]820 DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
[9b4f329]821 : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
822
823 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
824private:
825 DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
826 MUTATE_FRIEND
827};
828
829/// Use of a default argument.
830/// Internal to resolver.
831class DefaultArgExpr final : public Expr {
832public:
833 ptr<Expr> expr;
834
835 DefaultArgExpr( const CodeLocation & loc, const Expr * e )
836 : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
837
838 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
839private:
840 DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
841 MUTATE_FRIEND
842};
843
844/// C11 _Generic expression
845class GenericExpr final : public Expr {
846public:
847 /// One arm of the _Generic expr
848 struct Association {
849 ptr<Type> type;
850 ptr<Expr> expr;
851
852 Association() = default;
853 // default case
854 Association( const Expr * e ) : type(), expr( e ) {}
855 // non-default case
856 Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
857 };
858
859 ptr<Expr> control;
860 std::vector<Association> associations;
861
862 GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
863 : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
864
865 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
866private:
867 GenericExpr * clone() const override { return new GenericExpr{ *this }; }
868 MUTATE_FRIEND
[54e41b3]869};
[6d51bd7]870
[246c245]871
[79f7875]872}
873
[f3cc5b6]874#undef MUTATE_FRIEND
875
[79f7875]876// Local Variables: //
877// tab-width: 4 //
878// mode: c++ //
879// compile-command: "make install" //
880// End: //
Note: See TracBrowser for help on using the repository browser.