source: src/AST/Expr.hpp@ cd6a6ff

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 cd6a6ff was bb87dd0, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Fixing test init1-ERROR on new ast. Applying equivalent of b81fd95, #189, to new AST.

  • Property mode set to 100644
File size: 27.2 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/// Inidicates whether the cast is introduced by the CFA type system.
302/// GeneratedCast for casts that the resolver introduces to force a return type
303/// ExplicitCast for casts from user code
304/// ExplicitCast for casts from desugaring advanced CFA features into simpler CFA
305/// example
306/// int * p; // declaration
307/// (float *) p; // use, with subject cast
308/// subject cast being GeneratedCast means we are considering an interpretation with a type mismatch
309/// subject cast being ExplicitCast means someone in charge wants it that way
310enum GeneratedFlag { ExplicitCast, GeneratedCast };
311
312/// A type cast, e.g. `(int)e`
313class CastExpr final : public Expr {
314public:
315 ptr<Expr> arg;
316 GeneratedFlag isGenerated;
317
318 CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
319 GeneratedFlag g = GeneratedCast ) : Expr( loc, to ), arg( a ), isGenerated( g ) {}
320 /// Cast-to-void
321 CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast );
322
323 /// Wrap a cast expression around an existing expression (always generated)
324 CastExpr( const Expr * a, const Type * to ) : CastExpr( a->location, a, to, GeneratedCast ) {}
325
326 /// Wrap a cast-to-void expression around an existing expression (always generated)
327 CastExpr( const Expr * a ) : CastExpr( a->location, a, GeneratedCast ) {}
328
329 bool get_lvalue() const final;
330
331 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
332private:
333 CastExpr * clone() const override { return new CastExpr{ *this }; }
334 MUTATE_FRIEND
335};
336
337/// A cast to "keyword types", e.g. `(thread &)t`
338class KeywordCastExpr final : public Expr {
339public:
340 ptr<Expr> arg;
341 struct Concrete {
342 std::string field;
343 std::string getter;
344
345 Concrete() = default;
346 Concrete(const Concrete &) = default;
347 };
348 ast::AggregateDecl::Aggregate target;
349 Concrete concrete_target;
350
351
352 KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
353 : Expr( loc ), arg( a ), target( t ) {}
354
355 KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t, const Concrete & ct )
356 : Expr( loc ), arg( a ), target( t ), concrete_target( ct ) {}
357
358 /// Get a name for the target type
359 const char * targetString() const;
360
361 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
362private:
363 KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
364 MUTATE_FRIEND
365};
366
367/// A virtual dynamic cast, e.g. `(virtual exception)e`
368class VirtualCastExpr final : public Expr {
369public:
370 ptr<Expr> arg;
371
372 VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to )
373 : Expr( loc, to ), arg( a ) {}
374
375 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
376private:
377 VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; }
378 MUTATE_FRIEND
379};
380
381/// A member selection operation before expression resolution, e.g. `q.p`
382class UntypedMemberExpr final : public Expr {
383public:
384 ptr<Expr> member;
385 ptr<Expr> aggregate;
386
387 UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg )
388 : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); }
389
390 bool get_lvalue() const final;
391
392 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
393private:
394 UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
395 MUTATE_FRIEND
396};
397
398/// A member selection operation after expression resolution, e.g. `q.p`
399class MemberExpr final : public Expr {
400public:
401 readonly<DeclWithType> member;
402 ptr<Expr> aggregate;
403
404 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg );
405
406 bool get_lvalue() const final;
407
408 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
409private:
410 MemberExpr * clone() const override { return new MemberExpr{ *this }; }
411 MUTATE_FRIEND
412
413 // Custructor overload meant only for AST conversion
414 enum NoOpConstruction { NoOpConstructionChosen };
415 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg,
416 NoOpConstruction overloadSelector );
417 friend class ::ConverterOldToNew;
418 friend class ::ConverterNewToOld;
419};
420
421/// A compile-time constant.
422/// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted.
423/// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
424class ConstantExpr final : public Expr {
425public:
426 // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
427 std::string rep;
428
429 ConstantExpr(
430 const CodeLocation & loc, const Type * ty, const std::string & r,
431 std::optional<unsigned long long> i )
432 : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
433
434 /// Gets the integer value of this constant, if one is appropriate to its type.
435 /// Throws a SemanticError if the type is not appropriate for value-as-integer.
436 /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
437 long long int intValue() const;
438
439 /// generates a boolean constant of the given bool
440 static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
441 /// generates an integer constant of the given int
442 static ConstantExpr * from_int( const CodeLocation & loc, int i );
443 /// generates an integer constant of the given unsigned long int
444 static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
445 /// generates a null pointer value for the given type. void * if omitted.
446 static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
447
448 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
449private:
450 ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
451 MUTATE_FRIEND
452
453 std::optional<unsigned long long> ival;
454
455 // Intended only for legacy support of roundtripping the old AST.
456 // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
457 // In the old AST it's constExpr->constant.type
458 ptr<Type> underlyer;
459 friend class ::ConverterOldToNew;
460 friend class ::ConverterNewToOld;
461};
462
463/// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
464class SizeofExpr final : public Expr {
465public:
466 ptr<Expr> expr;
467 ptr<Type> type;
468
469 SizeofExpr( const CodeLocation & loc, const Expr * e );
470 SizeofExpr( const CodeLocation & loc, const Type * t );
471 // deliberately no disambiguating overload for nullptr_t
472
473 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
474private:
475 SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
476 MUTATE_FRIEND
477};
478
479/// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
480class AlignofExpr final : public Expr {
481public:
482 ptr<Expr> expr;
483 ptr<Type> type;
484
485 AlignofExpr( const CodeLocation & loc, const Expr * e );
486 AlignofExpr( const CodeLocation & loc, const Type * t );
487 // deliberately no disambiguating overload for nullptr_t
488
489 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
490private:
491 AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
492 MUTATE_FRIEND
493};
494
495/// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
496class UntypedOffsetofExpr final : public Expr {
497public:
498 ptr<Type> type;
499 std::string member;
500
501 UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
502 : Expr( loc ), type( ty ), member( mem ) {}
503
504 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
505private:
506 UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
507 MUTATE_FRIEND
508};
509
510/// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
511class OffsetofExpr final : public Expr {
512public:
513 ptr<Type> type;
514 readonly<DeclWithType> member;
515
516 OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
517
518 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
519private:
520 OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
521 MUTATE_FRIEND
522};
523
524/// a pack of field-offsets for a generic type
525class OffsetPackExpr final : public Expr {
526public:
527 ptr<StructInstType> type;
528
529 OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
530
531 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
532private:
533 OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
534 MUTATE_FRIEND
535};
536
537/// Variants of short-circuiting logical expression
538enum LogicalFlag { OrExpr, AndExpr };
539
540/// Short-circuiting boolean expression (`&&` or `||`)
541class LogicalExpr final : public Expr {
542public:
543 ptr<Expr> arg1;
544 ptr<Expr> arg2;
545 LogicalFlag isAnd;
546
547 LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
548
549 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
550private:
551 LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
552 MUTATE_FRIEND
553};
554
555/// Three-argument conditional e.g. `p ? a : b`
556class ConditionalExpr final : public Expr {
557public:
558 ptr<Expr> arg1;
559 ptr<Expr> arg2;
560 ptr<Expr> arg3;
561
562 ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
563 : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
564
565 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
566private:
567 ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
568 MUTATE_FRIEND
569};
570
571/// Comma expression e.g. `( a , b )`
572class CommaExpr final : public Expr {
573public:
574 ptr<Expr> arg1;
575 ptr<Expr> arg2;
576
577 CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
578 : Expr( loc ), arg1( a1 ), arg2( a2 ) {
579 this->result = a2->result;
580 }
581
582 bool get_lvalue() const final;
583
584 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
585private:
586 CommaExpr * clone() const override { return new CommaExpr{ *this }; }
587 MUTATE_FRIEND
588};
589
590/// A type used as an expression (e.g. a type generator parameter)
591class TypeExpr final : public Expr {
592public:
593 ptr<Type> type;
594
595 TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
596
597 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
598private:
599 TypeExpr * clone() const override { return new TypeExpr{ *this }; }
600 MUTATE_FRIEND
601};
602
603/// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
604/// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
605class AsmExpr final : public Expr {
606public:
607 std::string inout;
608 ptr<Expr> constraint;
609 ptr<Expr> operand;
610
611 AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
612 : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
613
614 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
615private:
616 AsmExpr * clone() const override { return new AsmExpr{ *this }; }
617 MUTATE_FRIEND
618};
619
620/// The application of a function to a set of parameters, along with a set of copy constructor
621/// calls, one for each argument
622class ImplicitCopyCtorExpr final : public Expr {
623public:
624 ptr<ApplicationExpr> callExpr;
625
626 ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
627 : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
628
629 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
630private:
631 ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
632 MUTATE_FRIEND
633};
634
635/// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
636class ConstructorExpr final : public Expr {
637public:
638 ptr<Expr> callExpr;
639
640 ConstructorExpr( const CodeLocation & loc, const Expr * call );
641
642 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
643private:
644 ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
645 MUTATE_FRIEND
646};
647
648/// A C99 compound literal, e.g. `(MyType){ a, b, c }`
649class CompoundLiteralExpr final : public Expr {
650public:
651 ptr<Init> init;
652
653 CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
654
655 bool get_lvalue() const final;
656
657 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
658private:
659 CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
660 MUTATE_FRIEND
661};
662
663/// A range, e.g. `3 ... 5` or `1~10`
664class RangeExpr final : public Expr {
665public:
666 ptr<Expr> low;
667 ptr<Expr> high;
668
669 RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
670 : Expr( loc ), low( l ), high( h ) {}
671
672 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
673private:
674 RangeExpr * clone() const override { return new RangeExpr{ *this }; }
675 MUTATE_FRIEND
676};
677
678/// A tuple expression before resolution, e.g. `[a, b, c]`
679class UntypedTupleExpr final : public Expr {
680public:
681 std::vector<ptr<Expr>> exprs;
682
683 UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
684 : Expr( loc ), exprs( std::move(xs) ) {}
685
686 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
687private:
688 UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
689 MUTATE_FRIEND
690};
691
692/// A tuple expression after resolution, e.g. `[a, b, c]`
693class TupleExpr final : public Expr {
694public:
695 std::vector<ptr<Expr>> exprs;
696
697 TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
698
699 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
700private:
701 TupleExpr * clone() const override { return new TupleExpr{ *this }; }
702 MUTATE_FRIEND
703};
704
705/// An element selection operation on a tuple value, e.g. `t.3` after analysis
706class TupleIndexExpr final : public Expr {
707public:
708 ptr<Expr> tuple;
709 unsigned index;
710
711 TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
712
713 bool get_lvalue() const final;
714
715 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
716private:
717 TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
718 MUTATE_FRIEND
719};
720
721/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
722/// multiple-assignment: both sides of the assignment have tuple type,
723/// e.g. `[a, b, c] = [d, e, f];`
724/// mass-assignment: left-hand side has tuple type and right-hand side does not:
725/// e.g. `[a, b, c] = 42;`
726class TupleAssignExpr final : public Expr {
727public:
728 ptr<StmtExpr> stmtExpr;
729
730 TupleAssignExpr(
731 const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
732 std::vector<ptr<ObjectDecl>> && tempDecls );
733
734 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
735
736 friend class ::ConverterOldToNew;
737
738private:
739 TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
740 TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
741
742 MUTATE_FRIEND
743};
744
745/// A GCC "statement expression", e.g. `({ int x = 5; x })`
746class StmtExpr final : public Expr {
747public:
748 ptr<CompoundStmt> stmts;
749 std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
750 std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
751
752 readonly<ExprStmt> resultExpr;
753
754 StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
755
756 /// Set the result type of this StmtExpr based on its body
757 void computeResult();
758
759 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
760private:
761 StmtExpr * clone() const override { return new StmtExpr{ *this }; }
762 MUTATE_FRIEND
763};
764
765/// An expression which must only be evaluated once
766class UniqueExpr final : public Expr {
767 static unsigned long long nextId;
768public:
769 ptr<Expr> expr;
770 ptr<ObjectDecl> object;
771 ptr<VariableExpr> var;
772 unsigned long long id;
773
774 UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
775
776 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
777private:
778 UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
779 MUTATE_FRIEND
780};
781
782/// One option for resolving an initializer expression
783struct InitAlternative {
784 ptr<Type> type;
785 ptr<Designation> designation;
786
787 InitAlternative() = default;
788 InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
789};
790
791/// Pre-resolution initializer expression
792class UntypedInitExpr final : public Expr {
793public:
794 ptr<Expr> expr;
795 std::deque<InitAlternative> initAlts;
796
797 UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
798 : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
799
800 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
801private:
802 UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
803 MUTATE_FRIEND
804};
805
806/// Post-resolution initializer expression
807class InitExpr final : public Expr {
808public:
809 ptr<Expr> expr;
810 ptr<Designation> designation;
811
812 InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
813 : Expr( loc, e->result ), expr( e ), designation( des ) {}
814
815 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
816private:
817 InitExpr * clone() const override { return new InitExpr{ *this }; }
818 MUTATE_FRIEND
819};
820
821/// Expression containing a deleted identifier.
822/// Internal to resolver.
823class DeletedExpr final : public Expr {
824public:
825 ptr<Expr> expr;
826 readonly<Decl> deleteStmt;
827
828 DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
829 : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
830
831 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
832private:
833 DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
834 MUTATE_FRIEND
835};
836
837/// Use of a default argument.
838/// Internal to resolver.
839class DefaultArgExpr final : public Expr {
840public:
841 ptr<Expr> expr;
842
843 DefaultArgExpr( const CodeLocation & loc, const Expr * e )
844 : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
845
846 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
847private:
848 DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
849 MUTATE_FRIEND
850};
851
852/// C11 _Generic expression
853class GenericExpr final : public Expr {
854public:
855 /// One arm of the _Generic expr
856 struct Association {
857 ptr<Type> type;
858 ptr<Expr> expr;
859
860 Association() = default;
861 // default case
862 Association( const Expr * e ) : type(), expr( e ) {}
863 // non-default case
864 Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
865 };
866
867 ptr<Expr> control;
868 std::vector<Association> associations;
869
870 GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
871 : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
872
873 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
874private:
875 GenericExpr * clone() const override { return new GenericExpr{ *this }; }
876 MUTATE_FRIEND
877};
878
879
880}
881
882#undef MUTATE_FRIEND
883
884// Local Variables: //
885// tab-width: 4 //
886// mode: c++ //
887// compile-command: "make install" //
888// End: //
Note: See TracBrowser for help on using the repository browser.