source: src/AST/Expr.hpp@ 82f791f

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 82f791f 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
Line 
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
11// Last Modified By : Peter A. Buhr
12// Created On : Fri May 10 10:30:00 2019
13// Update Count : 7
14//
15
16#pragma once
17
18#include <cassert>
19#include <deque>
20#include <map>
21#include <string>
22#include <utility> // for move
23#include <vector>
24#include <optional>
25
26#include "Fwd.hpp" // for UniqueId
27#include "Label.hpp"
28#include "Decl.hpp"
29#include "ParseNode.hpp"
30#include "Visitor.hpp"
31
32// Must be included in *all* AST classes; should be #undef'd at the end of the file
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
37
38class ConverterOldToNew;
39class ConverterNewToOld;
40
41namespace ast {
42
43/// Contains the ID of a declaration and a type that is derived from that declaration,
44/// but subject to decay-to-pointer and type parameter renaming
45struct ParamEntry {
46 UniqueId decl;
47 readonly<Decl> declptr;
48 ptr<Type> actualType;
49 ptr<Type> formalType;
50 ptr<Expr> expr;
51
52 ParamEntry() : decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {}
53 ParamEntry(
54 UniqueId id, const Decl * declptr, const Type * actual, const Type * formal,
55 const Expr * e )
56 : decl( id ), declptr( declptr ), actualType( actual ), formalType( formal ), expr( e ) {}
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:
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 */
73 struct InferUnion {
74 // mode is now unused
75 enum { Empty, Slots, Params } mode;
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 }
87 } data;
88
89 /// initializes from other InferUnion
90 void init_from( const InferUnion& o ) {
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);
96 }
97 }
98
99 /// initializes from other InferUnion (move semantics)
100 void init_from( InferUnion&& o ) {
101 data.resnSlots = o.data.resnSlots;
102 data.inferParams = o.data.inferParams;
103 o.data.resnSlots = nullptr;
104 o.data.inferParams = nullptr;
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;
112
113 bool hasSlots() const { return data.resnSlots; }
114
115 ResnSlots& resnSlots() {
116 if (!data.resnSlots) {
117 data.resnSlots = new ResnSlots();
118 }
119 return *data.resnSlots;
120 }
121
122 const ResnSlots& resnSlots() const {
123 if (data.resnSlots) {
124 return *data.resnSlots;
125 }
126 assertf(false, "Mode was not already resnSlots");
127 abort();
128 }
129
130 InferredParams& inferParams() {
131 if (!data.inferParams) {
132 data.inferParams = new InferredParams();
133 }
134 return *data.inferParams;
135 }
136
137 const InferredParams& inferParams() const {
138 if (data.inferParams) {
139 return *data.inferParams;
140 }
141 assertf(false, "Mode was not already Params");
142 abort();
143 }
144
145 void set_inferParams( InferredParams * ps ) {
146 delete data.resnSlots;
147 data.resnSlots = nullptr;
148 delete data.inferParams;
149 data.inferParams = ps;
150 }
151
152 /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode
153 /// and the other is in `Params`.
154 void splice( InferUnion && o ) {
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;
163 }
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 }
179 }
180 };
181
182 ptr<Type> result;
183 ptr<TypeSubstitution> env;
184 InferUnion inferred;
185 bool extension = false;
186
187 Expr( const CodeLocation & loc, const Type * res = nullptr )
188 : ParseNode( loc ), result( res ), env(), inferred() {}
189
190 Expr * set_extension( bool ex ) { extension = ex; return this; }
191 virtual bool get_lvalue() const;
192
193 virtual const Expr * accept( Visitor & v ) const override = 0;
194private:
195 Expr * clone() const override = 0;
196 MUTATE_FRIEND
197};
198
199/// The application of a function to a set of parameters.
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
208 bool get_lvalue() const final;
209
210 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
211private:
212 ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; }
213 MUTATE_FRIEND
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
225 bool get_lvalue() const final;
226
227 /// Creates a new dereference expression
228 static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg );
229 /// Creates a new assignment expression
230 static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs );
231
232 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
233private:
234 UntypedExpr * clone() const override { return new UntypedExpr{ *this }; }
235 MUTATE_FRIEND
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 }; }
249 MUTATE_FRIEND
250};
251
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
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
278 /// Generate AddressExpr wrapping given expression at same location
279 AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {}
280
281 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
282private:
283 AddressExpr * clone() const override { return new AddressExpr{ *this }; }
284 MUTATE_FRIEND
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 }; }
298 MUTATE_FRIEND
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
310 CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
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
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
321 bool get_lvalue() const final;
322
323 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
324private:
325 CastExpr * clone() const override { return new CastExpr{ *this }; }
326 MUTATE_FRIEND
327};
328
329/// A cast to "keyword types", e.g. `(thread &)t`
330class KeywordCastExpr final : public Expr {
331public:
332 ptr<Expr> arg;
333 struct Concrete {
334 std::string field;
335 std::string getter;
336
337 Concrete() = default;
338 Concrete(const Concrete &) = default;
339 };
340 ast::AggregateDecl::Aggregate target;
341 Concrete concrete_target;
342
343
344 KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
345 : Expr( loc ), arg( a ), target( t ) {}
346
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
350 /// Get a name for the target type
351 const char * targetString() const;
352
353 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
354private:
355 KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
356 MUTATE_FRIEND
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 }; }
370 MUTATE_FRIEND
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
382 bool get_lvalue() const final;
383
384 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
385private:
386 UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
387 MUTATE_FRIEND
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
398 bool get_lvalue() const final;
399
400 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
401private:
402 MemberExpr * clone() const override { return new MemberExpr{ *this }; }
403 MUTATE_FRIEND
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;
411};
412
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.
416class ConstantExpr final : public Expr {
417public:
418 // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
419 std::string rep;
420
421 ConstantExpr(
422 const CodeLocation & loc, const Type * ty, const std::string & r,
423 std::optional<unsigned long long> i )
424 : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
425
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.
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 }; }
443 MUTATE_FRIEND
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;
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 }; }
468 MUTATE_FRIEND
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 }; }
484 MUTATE_FRIEND
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 }; }
499 MUTATE_FRIEND
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 }; }
513 MUTATE_FRIEND
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 }; }
526 MUTATE_FRIEND
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 }; }
544 MUTATE_FRIEND
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 }; }
560 MUTATE_FRIEND
561};
562
563/// Comma expression e.g. `( a , b )`
564class CommaExpr final : public Expr {
565public:
566 ptr<Expr> arg1;
567 ptr<Expr> arg2;
568
569 CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
570 : Expr( loc ), arg1( a1 ), arg2( a2 ) {
571 this->result = a2->result;
572 }
573
574 bool get_lvalue() const final;
575
576 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
577private:
578 CommaExpr * clone() const override { return new CommaExpr{ *this }; }
579 MUTATE_FRIEND
580};
581
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
589 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
590private:
591 TypeExpr * clone() const override { return new TypeExpr{ *this }; }
592 MUTATE_FRIEND
593};
594
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:
599 std::string inout;
600 ptr<Expr> constraint;
601 ptr<Expr> operand;
602
603 AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
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
612/// The application of a function to a set of parameters, along with a set of copy constructor
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 )
619 : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
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
647 bool get_lvalue() const final;
648
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
705 bool get_lvalue() const final;
706
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
713/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
714/// multiple-assignment: both sides of the assignment have tuple type,
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
722 TupleAssignExpr(
723 const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
724 std::vector<ptr<ObjectDecl>> && tempDecls );
725
726 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
727
728 friend class ::ConverterOldToNew;
729
730private:
731 TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
732 TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
733
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
744 readonly<ExprStmt> resultExpr;
745
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
766 UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
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;
787 std::deque<InitAlternative> initAlts;
788
789 UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
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;
818 readonly<Decl> deleteStmt;
819
820 DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
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
869};
870
871
872}
873
874#undef MUTATE_FRIEND
875
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.