OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [go/] [gofrontend/] [expressions.h] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 714 jeremybenn
// expressions.h -- Go frontend expression handling.     -*- C++ -*-
2
 
3
// Copyright 2009 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
#ifndef GO_EXPRESSIONS_H
8
#define GO_EXPRESSIONS_H
9
 
10
#include <gmp.h>
11
#include <mpfr.h>
12
 
13
#include "operator.h"
14
 
15
class Gogo;
16
class Translate_context;
17
class Traverse;
18
class Statement_inserter;
19
class Type;
20
struct Type_context;
21
class Function_type;
22
class Map_type;
23
class Struct_type;
24
class Struct_field;
25
class Expression_list;
26
class Var_expression;
27
class Temporary_reference_expression;
28
class Set_and_use_temporary_expression;
29
class String_expression;
30
class Binary_expression;
31
class Call_expression;
32
class Func_expression;
33
class Unknown_expression;
34
class Index_expression;
35
class Map_index_expression;
36
class Bound_method_expression;
37
class Field_reference_expression;
38
class Interface_field_reference_expression;
39
class Type_guard_expression;
40
class Receive_expression;
41
class Named_object;
42
class Export;
43
class Import;
44
class Temporary_statement;
45
class Label;
46
class Ast_dump_context;
47
class String_dump;
48
 
49
// The base class for all expressions.
50
 
51
class Expression
52
{
53
 public:
54
  // The types of expressions.
55
  enum Expression_classification
56
  {
57
    EXPRESSION_ERROR,
58
    EXPRESSION_TYPE,
59
    EXPRESSION_UNARY,
60
    EXPRESSION_BINARY,
61
    EXPRESSION_CONST_REFERENCE,
62
    EXPRESSION_VAR_REFERENCE,
63
    EXPRESSION_TEMPORARY_REFERENCE,
64
    EXPRESSION_SET_AND_USE_TEMPORARY,
65
    EXPRESSION_SINK,
66
    EXPRESSION_FUNC_REFERENCE,
67
    EXPRESSION_UNKNOWN_REFERENCE,
68
    EXPRESSION_BOOLEAN,
69
    EXPRESSION_STRING,
70
    EXPRESSION_INTEGER,
71
    EXPRESSION_FLOAT,
72
    EXPRESSION_COMPLEX,
73
    EXPRESSION_NIL,
74
    EXPRESSION_IOTA,
75
    EXPRESSION_CALL,
76
    EXPRESSION_CALL_RESULT,
77
    EXPRESSION_BOUND_METHOD,
78
    EXPRESSION_INDEX,
79
    EXPRESSION_ARRAY_INDEX,
80
    EXPRESSION_STRING_INDEX,
81
    EXPRESSION_MAP_INDEX,
82
    EXPRESSION_SELECTOR,
83
    EXPRESSION_FIELD_REFERENCE,
84
    EXPRESSION_INTERFACE_FIELD_REFERENCE,
85
    EXPRESSION_ALLOCATION,
86
    EXPRESSION_TYPE_GUARD,
87
    EXPRESSION_CONVERSION,
88
    EXPRESSION_UNSAFE_CONVERSION,
89
    EXPRESSION_STRUCT_CONSTRUCTION,
90
    EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
91
    EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
92
    EXPRESSION_MAP_CONSTRUCTION,
93
    EXPRESSION_COMPOSITE_LITERAL,
94
    EXPRESSION_HEAP_COMPOSITE,
95
    EXPRESSION_RECEIVE,
96
    EXPRESSION_TYPE_DESCRIPTOR,
97
    EXPRESSION_TYPE_INFO,
98
    EXPRESSION_STRUCT_FIELD_OFFSET,
99
    EXPRESSION_MAP_DESCRIPTOR,
100
    EXPRESSION_LABEL_ADDR
101
  };
102
 
103
  Expression(Expression_classification, Location);
104
 
105
  virtual ~Expression();
106
 
107
  // Make an error expression.  This is used when a parse error occurs
108
  // to prevent cascading errors.
109
  static Expression*
110
  make_error(Location);
111
 
112
  // Make an expression which is really a type.  This is used during
113
  // parsing.
114
  static Expression*
115
  make_type(Type*, Location);
116
 
117
  // Make a unary expression.
118
  static Expression*
119
  make_unary(Operator, Expression*, Location);
120
 
121
  // Make a binary expression.
122
  static Expression*
123
  make_binary(Operator, Expression*, Expression*, Location);
124
 
125
  // Make a reference to a constant in an expression.
126
  static Expression*
127
  make_const_reference(Named_object*, Location);
128
 
129
  // Make a reference to a variable in an expression.
130
  static Expression*
131
  make_var_reference(Named_object*, Location);
132
 
133
  // Make a reference to a temporary variable.  Temporary variables
134
  // are always created by a single statement, which is what we use to
135
  // refer to them.
136
  static Temporary_reference_expression*
137
  make_temporary_reference(Temporary_statement*, Location);
138
 
139
  // Make an expressions which sets a temporary variable and then
140
  // evaluates to a reference to that temporary variable.  This is
141
  // used to set a temporary variable while retaining the order of
142
  // evaluation.
143
  static Set_and_use_temporary_expression*
144
  make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
145
 
146
  // Make a sink expression--a reference to the blank identifier _.
147
  static Expression*
148
  make_sink(Location);
149
 
150
  // Make a reference to a function in an expression.
151
  static Expression*
152
  make_func_reference(Named_object*, Expression* closure, Location);
153
 
154
  // Make a reference to an unknown name.  In a correct program this
155
  // will always be lowered to a real const/var/func reference.
156
  static Unknown_expression*
157
  make_unknown_reference(Named_object*, Location);
158
 
159
  // Make a constant bool expression.
160
  static Expression*
161
  make_boolean(bool val, Location);
162
 
163
  // Make a constant string expression.
164
  static Expression*
165
  make_string(const std::string&, Location);
166
 
167
  // Make a character constant expression.  TYPE should be NULL for an
168
  // abstract type.
169
  static Expression*
170
  make_character(const mpz_t*, Type*, Location);
171
 
172
  // Make a constant integer expression.  TYPE should be NULL for an
173
  // abstract type.
174
  static Expression*
175
  make_integer(const mpz_t*, Type*, Location);
176
 
177
  // Make a constant float expression.  TYPE should be NULL for an
178
  // abstract type.
179
  static Expression*
180
  make_float(const mpfr_t*, Type*, Location);
181
 
182
  // Make a constant complex expression.  TYPE should be NULL for an
183
  // abstract type.
184
  static Expression*
185
  make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, Location);
186
 
187
  // Make a nil expression.
188
  static Expression*
189
  make_nil(Location);
190
 
191
  // Make an iota expression.  This is used for the predeclared
192
  // constant iota.
193
  static Expression*
194
  make_iota();
195
 
196
  // Make a call expression.
197
  static Call_expression*
198
  make_call(Expression* func, Expression_list* args, bool is_varargs,
199
            Location);
200
 
201
  // Make a reference to a specific result of a call expression which
202
  // returns a tuple.
203
  static Expression*
204
  make_call_result(Call_expression*, unsigned int index);
205
 
206
  // Make an expression which is a method bound to its first
207
  // parameter.
208
  static Bound_method_expression*
209
  make_bound_method(Expression* object, Named_object* method, Location);
210
 
211
  // Make an index or slice expression.  This is a parser expression
212
  // which represents LEFT[START:END].  END may be NULL, meaning an
213
  // index rather than a slice.  At parse time we may not know the
214
  // type of LEFT.  After parsing this is lowered to an array index, a
215
  // string index, or a map index.
216
  static Expression*
217
  make_index(Expression* left, Expression* start, Expression* end,
218
             Location);
219
 
220
  // Make an array index expression.  END may be NULL, in which case
221
  // this is an lvalue.
222
  static Expression*
223
  make_array_index(Expression* array, Expression* start, Expression* end,
224
                   Location);
225
 
226
  // Make a string index expression.  END may be NULL.  This is never
227
  // an lvalue.
228
  static Expression*
229
  make_string_index(Expression* string, Expression* start, Expression* end,
230
                    Location);
231
 
232
  // Make a map index expression.  This is an lvalue.
233
  static Map_index_expression*
234
  make_map_index(Expression* map, Expression* val, Location);
235
 
236
  // Make a selector.  This is a parser expression which represents
237
  // LEFT.NAME.  At parse time we may not know the type of the left
238
  // hand side.
239
  static Expression*
240
  make_selector(Expression* left, const std::string& name, Location);
241
 
242
  // Make a reference to a field in a struct.
243
  static Field_reference_expression*
244
  make_field_reference(Expression*, unsigned int field_index, Location);
245
 
246
  // Make a reference to a field of an interface, with an associated
247
  // object.
248
  static Expression*
249
  make_interface_field_reference(Expression*, const std::string&,
250
                                 Location);
251
 
252
  // Make an allocation expression.
253
  static Expression*
254
  make_allocation(Type*, Location);
255
 
256
  // Make a type guard expression.
257
  static Expression*
258
  make_type_guard(Expression*, Type*, Location);
259
 
260
  // Make a type cast expression.
261
  static Expression*
262
  make_cast(Type*, Expression*, Location);
263
 
264
  // Make an unsafe type cast expression.  This is only used when
265
  // passing parameter to builtin functions that are part of the Go
266
  // runtime.
267
  static Expression*
268
  make_unsafe_cast(Type*, Expression*, Location);
269
 
270
  // Make a composite literal.  The DEPTH parameter is how far down we
271
  // are in a list of composite literals with omitted types.
272
  static Expression*
273
  make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
274
                         Location);
275
 
276
  // Make a struct composite literal.
277
  static Expression*
278
  make_struct_composite_literal(Type*, Expression_list*, Location);
279
 
280
  // Make a slice composite literal.
281
  static Expression*
282
  make_slice_composite_literal(Type*, Expression_list*, Location);
283
 
284
  // Take a composite literal and allocate it on the heap.
285
  static Expression*
286
  make_heap_composite(Expression*, Location);
287
 
288
  // Make a receive expression.  VAL is NULL for a unary receive.
289
  static Receive_expression*
290
  make_receive(Expression* channel, Location);
291
 
292
  // Make an expression which evaluates to the address of the type
293
  // descriptor for TYPE.
294
  static Expression*
295
  make_type_descriptor(Type* type, Location);
296
 
297
  // Make an expression which evaluates to some characteristic of a
298
  // type.  These are only used for type descriptors, so there is no
299
  // location parameter.
300
  enum Type_info
301
    {
302
      // The size of a value of the type.
303
      TYPE_INFO_SIZE,
304
      // The required alignment of a value of the type.
305
      TYPE_INFO_ALIGNMENT,
306
      // The required alignment of a value of the type when used as a
307
      // field in a struct.
308
      TYPE_INFO_FIELD_ALIGNMENT
309
    };
310
 
311
  static Expression*
312
  make_type_info(Type* type, Type_info);
313
 
314
  // Make an expression which evaluates to the offset of a field in a
315
  // struct.  This is only used for type descriptors, so there is no
316
  // location parameter.
317
  static Expression*
318
  make_struct_field_offset(Struct_type*, const Struct_field*);
319
 
320
  // Make an expression which evaluates to the address of the map
321
  // descriptor for TYPE.
322
  static Expression*
323
  make_map_descriptor(Map_type* type, Location);
324
 
325
  // Make an expression which evaluates to the address of an unnamed
326
  // label.
327
  static Expression*
328
  make_label_addr(Label*, Location);
329
 
330
  // Return the expression classification.
331
  Expression_classification
332
  classification() const
333
  { return this->classification_; }
334
 
335
  // Return the location of the expression.
336
  Location
337
  location() const
338
  { return this->location_; }
339
 
340
  // Return whether this is a constant expression.
341
  bool
342
  is_constant() const
343
  { return this->do_is_constant(); }
344
 
345
  // If this is not a constant expression with integral type, return
346
  // false.  If it is one, return true, and set VAL to the value.  VAL
347
  // should already be initialized.  If this returns true, it sets
348
  // *PTYPE to the type of the value, or NULL for an abstract type.
349
  // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
350
  // to have its final value.
351
  bool
352
  integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
353
 
354
  // If this is not a constant expression with floating point type,
355
  // return false.  If it is one, return true, and set VAL to the
356
  // value.  VAL should already be initialized.  If this returns true,
357
  // it sets *PTYPE to the type of the value, or NULL for an abstract
358
  // type.
359
  bool
360
  float_constant_value(mpfr_t val, Type** ptype) const;
361
 
362
  // If this is not a constant expression with complex type, return
363
  // false.  If it is one, return true, and set REAL and IMAG to the
364
  // value.  REAL and IMAG should already be initialized.  If this
365
  // return strue, it sets *PTYPE to the type of the value, or NULL
366
  // for an abstract type.
367
  bool
368
  complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
369
 
370
  // If this is not a constant expression with string type, return
371
  // false.  If it is one, return true, and set VAL to the value.
372
  bool
373
  string_constant_value(std::string* val) const
374
  { return this->do_string_constant_value(val); }
375
 
376
  // This is called if the value of this expression is being
377
  // discarded.  This issues warnings about computed values being
378
  // unused.
379
  void
380
  discarding_value()
381
  { this->do_discarding_value(); }
382
 
383
  // Return whether this is an error expression.
384
  bool
385
  is_error_expression() const
386
  { return this->classification_ == EXPRESSION_ERROR; }
387
 
388
  // Return whether this expression really represents a type.
389
  bool
390
  is_type_expression() const
391
  { return this->classification_ == EXPRESSION_TYPE; }
392
 
393
  // If this is a variable reference, return the Var_expression
394
  // structure.  Otherwise, return NULL.  This is a controlled dynamic
395
  // cast.
396
  Var_expression*
397
  var_expression()
398
  { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
399
 
400
  const Var_expression*
401
  var_expression() const
402
  { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
403
 
404
  // If this is a reference to a temporary variable, return the
405
  // Temporary_reference_expression.  Otherwise, return NULL.
406
  Temporary_reference_expression*
407
  temporary_reference_expression()
408
  {
409
    return this->convert<Temporary_reference_expression,
410
                         EXPRESSION_TEMPORARY_REFERENCE>();
411
  }
412
 
413
  // If this is a set-and-use-temporary, return the
414
  // Set_and_use_temporary_expression.  Otherwise, return NULL.
415
  Set_and_use_temporary_expression*
416
  set_and_use_temporary_expression()
417
  {
418
    return this->convert<Set_and_use_temporary_expression,
419
                         EXPRESSION_SET_AND_USE_TEMPORARY>();
420
  }
421
 
422
  // Return whether this is a sink expression.
423
  bool
424
  is_sink_expression() const
425
  { return this->classification_ == EXPRESSION_SINK; }
426
 
427
  // If this is a string expression, return the String_expression
428
  // structure.  Otherwise, return NULL.
429
  String_expression*
430
  string_expression()
431
  { return this->convert<String_expression, EXPRESSION_STRING>(); }
432
 
433
  // Return whether this is the expression nil.
434
  bool
435
  is_nil_expression() const
436
  { return this->classification_ == EXPRESSION_NIL; }
437
 
438
  // If this is an indirection through a pointer, return the
439
  // expression being pointed through.  Otherwise return this.
440
  Expression*
441
  deref();
442
 
443
  // If this is a binary expression, return the Binary_expression
444
  // structure.  Otherwise return NULL.
445
  Binary_expression*
446
  binary_expression()
447
  { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
448
 
449
  // If this is a call expression, return the Call_expression
450
  // structure.  Otherwise, return NULL.  This is a controlled dynamic
451
  // cast.
452
  Call_expression*
453
  call_expression()
454
  { return this->convert<Call_expression, EXPRESSION_CALL>(); }
455
 
456
  // If this is an expression which refers to a function, return the
457
  // Func_expression structure.  Otherwise, return NULL.
458
  Func_expression*
459
  func_expression()
460
  { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
461
 
462
  const Func_expression*
463
  func_expression() const
464
  { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
465
 
466
  // If this is an expression which refers to an unknown name, return
467
  // the Unknown_expression structure.  Otherwise, return NULL.
468
  Unknown_expression*
469
  unknown_expression()
470
  { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
471
 
472
  const Unknown_expression*
473
  unknown_expression() const
474
  {
475
    return this->convert<const Unknown_expression,
476
                         EXPRESSION_UNKNOWN_REFERENCE>();
477
  }
478
 
479
  // If this is an index expression, return the Index_expression
480
  // structure.  Otherwise, return NULL.
481
  Index_expression*
482
  index_expression()
483
  { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
484
 
485
  // If this is an expression which refers to indexing in a map,
486
  // return the Map_index_expression structure.  Otherwise, return
487
  // NULL.
488
  Map_index_expression*
489
  map_index_expression()
490
  { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
491
 
492
  // If this is a bound method expression, return the
493
  // Bound_method_expression structure.  Otherwise, return NULL.
494
  Bound_method_expression*
495
  bound_method_expression()
496
  { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
497
 
498
  // If this is a reference to a field in a struct, return the
499
  // Field_reference_expression structure.  Otherwise, return NULL.
500
  Field_reference_expression*
501
  field_reference_expression()
502
  {
503
    return this->convert<Field_reference_expression,
504
                         EXPRESSION_FIELD_REFERENCE>();
505
  }
506
 
507
  // If this is a reference to a field in an interface, return the
508
  // Interface_field_reference_expression structure.  Otherwise,
509
  // return NULL.
510
  Interface_field_reference_expression*
511
  interface_field_reference_expression()
512
  {
513
    return this->convert<Interface_field_reference_expression,
514
                         EXPRESSION_INTERFACE_FIELD_REFERENCE>();
515
  }
516
 
517
  // If this is a type guard expression, return the
518
  // Type_guard_expression structure.  Otherwise, return NULL.
519
  Type_guard_expression*
520
  type_guard_expression()
521
  { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
522
 
523
  // If this is a receive expression, return the Receive_expression
524
  // structure.  Otherwise, return NULL.
525
  Receive_expression*
526
  receive_expression()
527
  { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
528
 
529
  // Return true if this is a composite literal.
530
  bool
531
  is_composite_literal() const;
532
 
533
  // Return true if this is a composite literal which is not constant.
534
  bool
535
  is_nonconstant_composite_literal() const;
536
 
537
  // Return true if this is a reference to a local variable.
538
  bool
539
  is_local_variable() const;
540
 
541
  // Traverse an expression.
542
  static int
543
  traverse(Expression**, Traverse*);
544
 
545
  // Traverse subexpressions of this expression.
546
  int
547
  traverse_subexpressions(Traverse*);
548
 
549
  // Lower an expression.  This is called immediately after parsing.
550
  // FUNCTION is the function we are in; it will be NULL for an
551
  // expression initializing a global variable.  INSERTER may be used
552
  // to insert statements before the statement or initializer
553
  // containing this expression; it is normally used to create
554
  // temporary variables.  IOTA_VALUE is the value that we should give
555
  // to any iota expressions.  This function must resolve expressions
556
  // which could not be fully parsed into their final form.  It
557
  // returns the same Expression or a new one.
558
  Expression*
559
  lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
560
        int iota_value)
561
  { return this->do_lower(gogo, function, inserter, iota_value); }
562
 
563
  // Determine the real type of an expression with abstract integer,
564
  // floating point, or complex type.  TYPE_CONTEXT describes the
565
  // expected type.
566
  void
567
  determine_type(const Type_context*);
568
 
569
  // Check types in an expression.
570
  void
571
  check_types(Gogo* gogo)
572
  { this->do_check_types(gogo); }
573
 
574
  // Determine the type when there is no context.
575
  void
576
  determine_type_no_context();
577
 
578
  // Return the current type of the expression.  This may be changed
579
  // by determine_type.
580
  Type*
581
  type()
582
  { return this->do_type(); }
583
 
584
  // Return a copy of an expression.
585
  Expression*
586
  copy()
587
  { return this->do_copy(); }
588
 
589
  // Return whether the expression is addressable--something which may
590
  // be used as the operand of the unary & operator.
591
  bool
592
  is_addressable() const
593
  { return this->do_is_addressable(); }
594
 
595
  // Note that we are taking the address of this expression.  ESCAPES
596
  // is true if this address escapes the current function.
597
  void
598
  address_taken(bool escapes)
599
  { this->do_address_taken(escapes); }
600
 
601
  // Return whether this expression must be evaluated in order
602
  // according to the order of evaluation rules.  This is basically
603
  // true of all expressions with side-effects.
604
  bool
605
  must_eval_in_order() const
606
  { return this->do_must_eval_in_order(); }
607
 
608
  // Return whether subexpressions of this expression must be
609
  // evaluated in order.  This is true of index expressions and
610
  // pointer indirections.  This sets *SKIP to the number of
611
  // subexpressions to skip during traversing, as index expressions
612
  // only requiring moving the index, not the array.
613
  bool
614
  must_eval_subexpressions_in_order(int* skip) const
615
  {
616
    *skip = 0;
617
    return this->do_must_eval_subexpressions_in_order(skip);
618
  }
619
 
620
  // Return the tree for this expression.
621
  tree
622
  get_tree(Translate_context*);
623
 
624
  // Return a tree handling any conversions which must be done during
625
  // assignment.
626
  static tree
627
  convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
628
                         tree rhs_tree, Location location);
629
 
630
  // Return a tree converting a value of one interface type to another
631
  // interface type.  If FOR_TYPE_GUARD is true this is for a type
632
  // assertion.
633
  static tree
634
  convert_interface_to_interface(Translate_context*, Type* lhs_type,
635
                                 Type* rhs_type, tree rhs_tree,
636
                                 bool for_type_guard, Location);
637
 
638
  // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
639
  // TYPE is the type of both sides.
640
  static tree
641
  comparison_tree(Translate_context*, Operator op, Type* left_type,
642
                  tree left_tree, Type* right_type, tree right_tree,
643
                  Location);
644
 
645
  // Return a tree for the multi-precision integer VAL in TYPE.
646
  static tree
647
  integer_constant_tree(mpz_t val, tree type);
648
 
649
  // Return a tree for the floating point value VAL in TYPE.
650
  static tree
651
  float_constant_tree(mpfr_t val, tree type);
652
 
653
  // Return a tree for the complex value REAL/IMAG in TYPE.
654
  static tree
655
  complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
656
 
657
  // Export the expression.  This is only used for constants.  It will
658
  // be used for things like values of named constants and sizes of
659
  // arrays.
660
  void
661
  export_expression(Export* exp) const
662
  { this->do_export(exp); }
663
 
664
  // Import an expression.
665
  static Expression*
666
  import_expression(Import*);
667
 
668
  // Return a tree which checks that VAL, of arbitrary integer type,
669
  // is non-negative and is not more than the maximum value of
670
  // BOUND_TYPE.  If SOFAR is not NULL, it is or'red into the result.
671
  // The return value may be NULL if SOFAR is NULL.
672
  static tree
673
  check_bounds(tree val, tree bound_type, tree sofar, Location);
674
 
675
  // Dump an expression to a dump constext.
676
  void
677
  dump_expression(Ast_dump_context*) const;
678
 
679
 protected:
680
  // May be implemented by child class: traverse the expressions.
681
  virtual int
682
  do_traverse(Traverse*);
683
 
684
  // Return a lowered expression.
685
  virtual Expression*
686
  do_lower(Gogo*, Named_object*, Statement_inserter*, int)
687
  { return this; }
688
 
689
  // Return whether this is a constant expression.
690
  virtual bool
691
  do_is_constant() const
692
  { return false; }
693
 
694
  // Return whether this is a constant expression of integral type,
695
  // and set VAL to the value.
696
  virtual bool
697
  do_integer_constant_value(bool, mpz_t, Type**) const
698
  { return false; }
699
 
700
  // Return whether this is a constant expression of floating point
701
  // type, and set VAL to the value.
702
  virtual bool
703
  do_float_constant_value(mpfr_t, Type**) const
704
  { return false; }
705
 
706
  // Return whether this is a constant expression of complex type, and
707
  // set REAL and IMAGE to the value.
708
  virtual bool
709
  do_complex_constant_value(mpfr_t, mpfr_t, Type**) const
710
  { return false; }
711
 
712
  // Return whether this is a constant expression of string type, and
713
  // set VAL to the value.
714
  virtual bool
715
  do_string_constant_value(std::string*) const
716
  { return false; }
717
 
718
  // Called by the parser if the value is being discarded.
719
  virtual void
720
  do_discarding_value();
721
 
722
  // Child class holds type.
723
  virtual Type*
724
  do_type() = 0;
725
 
726
  // Child class implements determining type information.
727
  virtual void
728
  do_determine_type(const Type_context*) = 0;
729
 
730
  // Child class implements type checking if needed.
731
  virtual void
732
  do_check_types(Gogo*)
733
  { }
734
 
735
  // Child class implements copying.
736
  virtual Expression*
737
  do_copy() = 0;
738
 
739
  // Child class implements whether the expression is addressable.
740
  virtual bool
741
  do_is_addressable() const
742
  { return false; }
743
 
744
  // Child class implements taking the address of an expression.
745
  virtual void
746
  do_address_taken(bool)
747
  { }
748
 
749
  // Child class implements whether this expression must be evaluated
750
  // in order.
751
  virtual bool
752
  do_must_eval_in_order() const
753
  { return false; }
754
 
755
  // Child class implements whether this expressions requires that
756
  // subexpressions be evaluated in order.  The child implementation
757
  // may set *SKIP if it should be non-zero.
758
  virtual bool
759
  do_must_eval_subexpressions_in_order(int* /* skip */) const
760
  { return false; }
761
 
762
  // Child class implements conversion to tree.
763
  virtual tree
764
  do_get_tree(Translate_context*) = 0;
765
 
766
  // Child class implements export.
767
  virtual void
768
  do_export(Export*) const;
769
 
770
  // For children to call to give an error for an unused value.
771
  void
772
  unused_value_error();
773
 
774
  // For children to call when they detect that they are in error.
775
  void
776
  set_is_error();
777
 
778
  // For children to call to report an error conveniently.
779
  void
780
  report_error(const char*);
781
 
782
  // Child class implements dumping to a dump context.
783
  virtual void
784
  do_dump_expression(Ast_dump_context*) const = 0;
785
 
786
 private:
787
  // Convert to the desired statement classification, or return NULL.
788
  // This is a controlled dynamic cast.
789
  template<typename Expression_class,
790
           Expression_classification expr_classification>
791
  Expression_class*
792
  convert()
793
  {
794
    return (this->classification_ == expr_classification
795
            ? static_cast<Expression_class*>(this)
796
            : NULL);
797
  }
798
 
799
  template<typename Expression_class,
800
           Expression_classification expr_classification>
801
  const Expression_class*
802
  convert() const
803
  {
804
    return (this->classification_ == expr_classification
805
            ? static_cast<const Expression_class*>(this)
806
            : NULL);
807
  }
808
 
809
  static tree
810
  convert_type_to_interface(Translate_context*, Type*, Type*, tree,
811
                            Location);
812
 
813
  static tree
814
  get_interface_type_descriptor(Translate_context*, Type*, tree,
815
                                Location);
816
 
817
  static tree
818
  convert_interface_to_type(Translate_context*, Type*, Type*, tree,
819
                            Location);
820
 
821
  // The expression classification.
822
  Expression_classification classification_;
823
  // The location in the input file.
824
  Location location_;
825
};
826
 
827
// A list of Expressions.
828
 
829
class Expression_list
830
{
831
 public:
832
  Expression_list()
833
    : entries_()
834
  { }
835
 
836
  // Return whether the list is empty.
837
  bool
838
  empty() const
839
  { return this->entries_.empty(); }
840
 
841
  // Return the number of entries in the list.
842
  size_t
843
  size() const
844
  { return this->entries_.size(); }
845
 
846
  // Add an entry to the end of the list.
847
  void
848
  push_back(Expression* expr)
849
  { this->entries_.push_back(expr); }
850
 
851
  void
852
  append(Expression_list* add)
853
  { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
854
 
855
  // Reserve space in the list.
856
  void
857
  reserve(size_t size)
858
  { this->entries_.reserve(size); }
859
 
860
  // Traverse the expressions in the list.
861
  int
862
  traverse(Traverse*);
863
 
864
  // Copy the list.
865
  Expression_list*
866
  copy();
867
 
868
  // Return true if the list contains an error expression.
869
  bool
870
  contains_error() const;
871
 
872
  // Return the first and last elements.
873
  Expression*&
874
  front()
875
  { return this->entries_.front(); }
876
 
877
  Expression*
878
  front() const
879
  { return this->entries_.front(); }
880
 
881
  Expression*&
882
  back()
883
  { return this->entries_.back(); }
884
 
885
  Expression*
886
  back() const
887
  { return this->entries_.back(); }
888
 
889
  // Iterators.
890
 
891
  typedef std::vector<Expression*>::iterator iterator;
892
  typedef std::vector<Expression*>::const_iterator const_iterator;
893
 
894
  iterator
895
  begin()
896
  { return this->entries_.begin(); }
897
 
898
  const_iterator
899
  begin() const
900
  { return this->entries_.begin(); }
901
 
902
  iterator
903
  end()
904
  { return this->entries_.end(); }
905
 
906
  const_iterator
907
  end() const
908
  { return this->entries_.end(); }
909
 
910
  // Erase an entry.
911
  void
912
  erase(iterator p)
913
  { this->entries_.erase(p); }
914
 
915
 private:
916
  std::vector<Expression*> entries_;
917
};
918
 
919
// An abstract base class for an expression which is only used by the
920
// parser, and is lowered in the lowering pass.
921
 
922
class Parser_expression : public Expression
923
{
924
 public:
925
  Parser_expression(Expression_classification classification,
926
                    Location location)
927
    : Expression(classification, location)
928
  { }
929
 
930
 protected:
931
  virtual Expression*
932
  do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
933
 
934
  Type*
935
  do_type();
936
 
937
  void
938
  do_determine_type(const Type_context*)
939
  { go_unreachable(); }
940
 
941
  void
942
  do_check_types(Gogo*)
943
  { go_unreachable(); }
944
 
945
  tree
946
  do_get_tree(Translate_context*)
947
  { go_unreachable(); }
948
};
949
 
950
// An expression which is simply a variable.
951
 
952
class Var_expression : public Expression
953
{
954
 public:
955
  Var_expression(Named_object* variable, Location location)
956
    : Expression(EXPRESSION_VAR_REFERENCE, location),
957
      variable_(variable)
958
  { }
959
 
960
  // Return the variable.
961
  Named_object*
962
  named_object() const
963
  { return this->variable_; }
964
 
965
 protected:
966
  Expression*
967
  do_lower(Gogo*, Named_object*, Statement_inserter*, int);
968
 
969
  Type*
970
  do_type();
971
 
972
  void
973
  do_determine_type(const Type_context*);
974
 
975
  Expression*
976
  do_copy()
977
  { return this; }
978
 
979
  bool
980
  do_is_addressable() const
981
  { return true; }
982
 
983
  void
984
  do_address_taken(bool);
985
 
986
  tree
987
  do_get_tree(Translate_context*);
988
 
989
  void
990
  do_dump_expression(Ast_dump_context*) const;
991
 
992
 private:
993
  // The variable we are referencing.
994
  Named_object* variable_;
995
};
996
 
997
// A reference to a temporary variable.
998
 
999
class Temporary_reference_expression : public Expression
1000
{
1001
 public:
1002
  Temporary_reference_expression(Temporary_statement* statement,
1003
                                 Location location)
1004
    : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1005
      statement_(statement), is_lvalue_(false)
1006
  { }
1007
 
1008
  // Indicate that this reference appears on the left hand side of an
1009
  // assignment statement.
1010
  void
1011
  set_is_lvalue()
1012
  { this->is_lvalue_ = true; }
1013
 
1014
 protected:
1015
  Type*
1016
  do_type();
1017
 
1018
  void
1019
  do_determine_type(const Type_context*)
1020
  { }
1021
 
1022
  Expression*
1023
  do_copy()
1024
  { return make_temporary_reference(this->statement_, this->location()); }
1025
 
1026
  bool
1027
  do_is_addressable() const
1028
  { return true; }
1029
 
1030
  void
1031
  do_address_taken(bool);
1032
 
1033
  tree
1034
  do_get_tree(Translate_context*);
1035
 
1036
  void
1037
  do_dump_expression(Ast_dump_context*) const;
1038
 
1039
 private:
1040
  // The statement where the temporary variable is defined.
1041
  Temporary_statement* statement_;
1042
  // Whether this reference appears on the left hand side of an
1043
  // assignment statement.
1044
  bool is_lvalue_;
1045
};
1046
 
1047
// Set and use a temporary variable.
1048
 
1049
class Set_and_use_temporary_expression : public Expression
1050
{
1051
 public:
1052
  Set_and_use_temporary_expression(Temporary_statement* statement,
1053
                                   Expression* expr, Location location)
1054
    : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1055
      statement_(statement), expr_(expr)
1056
  { }
1057
 
1058
  // Return the temporary.
1059
  Temporary_statement*
1060
  temporary() const
1061
  { return this->statement_; }
1062
 
1063
  // Return the expression.
1064
  Expression*
1065
  expression() const
1066
  { return this->expr_; }
1067
 
1068
 protected:
1069
  Type*
1070
  do_type();
1071
 
1072
  void
1073
  do_determine_type(const Type_context*)
1074
  { }
1075
 
1076
  Expression*
1077
  do_copy()
1078
  {
1079
    return make_set_and_use_temporary(this->statement_, this->expr_,
1080
                                      this->location());
1081
  }
1082
 
1083
  bool
1084
  do_is_addressable() const
1085
  { return true; }
1086
 
1087
  void
1088
  do_address_taken(bool);
1089
 
1090
  tree
1091
  do_get_tree(Translate_context*);
1092
 
1093
  void
1094
  do_dump_expression(Ast_dump_context*) const;
1095
 
1096
 private:
1097
  // The statement where the temporary variable is defined.
1098
  Temporary_statement* statement_;
1099
  // The expression to assign to the temporary.
1100
  Expression* expr_;
1101
};
1102
 
1103
// A string expression.
1104
 
1105
class String_expression : public Expression
1106
{
1107
 public:
1108
  String_expression(const std::string& val, Location location)
1109
    : Expression(EXPRESSION_STRING, location),
1110
      val_(val), type_(NULL)
1111
  { }
1112
 
1113
  const std::string&
1114
  val() const
1115
  { return this->val_; }
1116
 
1117
  static Expression*
1118
  do_import(Import*);
1119
 
1120
 protected:
1121
  bool
1122
  do_is_constant() const
1123
  { return true; }
1124
 
1125
  bool
1126
  do_string_constant_value(std::string* val) const
1127
  {
1128
    *val = this->val_;
1129
    return true;
1130
  }
1131
 
1132
  Type*
1133
  do_type();
1134
 
1135
  void
1136
  do_determine_type(const Type_context*);
1137
 
1138
  Expression*
1139
  do_copy()
1140
  { return this; }
1141
 
1142
  tree
1143
  do_get_tree(Translate_context*);
1144
 
1145
  // Write string literal to a string dump.
1146
  static void
1147
  export_string(String_dump* exp, const String_expression* str);
1148
 
1149
  void
1150
  do_export(Export*) const;
1151
 
1152
  void
1153
  do_dump_expression(Ast_dump_context*) const;
1154
 
1155
 private:
1156
  // The string value.  This is immutable.
1157
  const std::string val_;
1158
  // The type as determined by context.
1159
  Type* type_;
1160
};
1161
 
1162
// A binary expression.
1163
 
1164
class Binary_expression : public Expression
1165
{
1166
 public:
1167
  Binary_expression(Operator op, Expression* left, Expression* right,
1168
                    Location location)
1169
    : Expression(EXPRESSION_BINARY, location),
1170
      op_(op), left_(left), right_(right)
1171
  { }
1172
 
1173
  // Return the operator.
1174
  Operator
1175
  op()
1176
  { return this->op_; }
1177
 
1178
  // Return the left hand expression.
1179
  Expression*
1180
  left()
1181
  { return this->left_; }
1182
 
1183
  // Return the right hand expression.
1184
  Expression*
1185
  right()
1186
  { return this->right_; }
1187
 
1188
  // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1189
  // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1190
  // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL.  Return true
1191
  // if this could be done, false if not.
1192
  static bool
1193
  eval_integer(Operator op, Type* left_type, mpz_t left_val,
1194
               Type* right_type, mpz_t right_val, Location,
1195
               mpz_t val);
1196
 
1197
  // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1198
  // Return true if this could be done, false if not.
1199
  static bool
1200
  eval_float(Operator op, Type* left_type, mpfr_t left_val,
1201
             Type* right_type, mpfr_t right_val, mpfr_t val,
1202
             Location);
1203
 
1204
  // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1205
  // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG.  Return true if this
1206
  // could be done, false if not.
1207
  static bool
1208
  eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1209
               mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1210
               mpfr_t right_imag, mpfr_t real, mpfr_t imag, Location);
1211
 
1212
  // Compare integer constants according to OP.
1213
  static bool
1214
  compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1215
 
1216
  // Compare floating point constants according to OP.
1217
  static bool
1218
  compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1219
 
1220
  // Compare complex constants according to OP.
1221
  static bool
1222
  compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1223
                  mpfr_t right_val, mpfr_t right_imag);
1224
 
1225
  static Expression*
1226
  do_import(Import*);
1227
 
1228
  // Report an error if OP can not be applied to TYPE.  Return whether
1229
  // it can.  OTYPE is the type of the other operand.
1230
  static bool
1231
  check_operator_type(Operator op, Type* type, Type* otype, Location);
1232
 
1233
 protected:
1234
  int
1235
  do_traverse(Traverse* traverse);
1236
 
1237
  Expression*
1238
  do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1239
 
1240
  bool
1241
  do_is_constant() const
1242
  { return this->left_->is_constant() && this->right_->is_constant(); }
1243
 
1244
  bool
1245
  do_integer_constant_value(bool, mpz_t val, Type**) const;
1246
 
1247
  bool
1248
  do_float_constant_value(mpfr_t val, Type**) const;
1249
 
1250
  bool
1251
  do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1252
 
1253
  void
1254
  do_discarding_value();
1255
 
1256
  Type*
1257
  do_type();
1258
 
1259
  void
1260
  do_determine_type(const Type_context*);
1261
 
1262
  void
1263
  do_check_types(Gogo*);
1264
 
1265
  Expression*
1266
  do_copy()
1267
  {
1268
    return Expression::make_binary(this->op_, this->left_->copy(),
1269
                                   this->right_->copy(), this->location());
1270
  }
1271
 
1272
  tree
1273
  do_get_tree(Translate_context*);
1274
 
1275
  void
1276
  do_export(Export*) const;
1277
 
1278
  void
1279
  do_dump_expression(Ast_dump_context*) const;
1280
 
1281
 private:
1282
  Expression*
1283
  lower_struct_comparison(Gogo*, Statement_inserter*);
1284
 
1285
  Expression*
1286
  lower_array_comparison(Gogo*, Statement_inserter*);
1287
 
1288
  Expression*
1289
  lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1290
 
1291
  Expression*
1292
  operand_address(Statement_inserter*, Expression*);
1293
 
1294
  // The binary operator to apply.
1295
  Operator op_;
1296
  // The left hand side operand.
1297
  Expression* left_;
1298
  // The right hand side operand.
1299
  Expression* right_;
1300
};
1301
 
1302
// A call expression.  The go statement needs to dig inside this.
1303
 
1304
class Call_expression : public Expression
1305
{
1306
 public:
1307
  Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1308
                  Location location)
1309
    : Expression(EXPRESSION_CALL, location),
1310
      fn_(fn), args_(args), type_(NULL), results_(NULL), tree_(NULL),
1311
      is_varargs_(is_varargs), are_hidden_fields_ok_(false),
1312
      varargs_are_lowered_(false), types_are_determined_(false),
1313
      is_deferred_(false), issued_error_(false)
1314
  { }
1315
 
1316
  // The function to call.
1317
  Expression*
1318
  fn() const
1319
  { return this->fn_; }
1320
 
1321
  // The arguments.
1322
  Expression_list*
1323
  args()
1324
  { return this->args_; }
1325
 
1326
  const Expression_list*
1327
  args() const
1328
  { return this->args_; }
1329
 
1330
  // Get the function type.
1331
  Function_type*
1332
  get_function_type() const;
1333
 
1334
  // Return the number of values this call will return.
1335
  size_t
1336
  result_count() const;
1337
 
1338
  // Return the temporary variable which holds result I.  This is only
1339
  // valid after the expression has been lowered, and is only valid
1340
  // for calls which return multiple results.
1341
  Temporary_statement*
1342
  result(size_t i) const;
1343
 
1344
  // Return whether this is a call to the predeclared function
1345
  // recover.
1346
  bool
1347
  is_recover_call() const;
1348
 
1349
  // Set the argument for a call to recover.
1350
  void
1351
  set_recover_arg(Expression*);
1352
 
1353
  // Whether the last argument is a varargs argument (f(a...)).
1354
  bool
1355
  is_varargs() const
1356
  { return this->is_varargs_; }
1357
 
1358
  // Note that varargs have already been lowered.
1359
  void
1360
  set_varargs_are_lowered()
1361
  { this->varargs_are_lowered_ = true; }
1362
 
1363
  // Note that it is OK for this call to set hidden fields when
1364
  // passing arguments.
1365
  void
1366
  set_hidden_fields_are_ok()
1367
  { this->are_hidden_fields_ok_ = true; }
1368
 
1369
  // Whether this call is being deferred.
1370
  bool
1371
  is_deferred() const
1372
  { return this->is_deferred_; }
1373
 
1374
  // Note that the call is being deferred.
1375
  void
1376
  set_is_deferred()
1377
  { this->is_deferred_ = true; }
1378
 
1379
  // We have found an error with this call expression; return true if
1380
  // we should report it.
1381
  bool
1382
  issue_error();
1383
 
1384
 protected:
1385
  int
1386
  do_traverse(Traverse*);
1387
 
1388
  virtual Expression*
1389
  do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1390
 
1391
  void
1392
  do_discarding_value()
1393
  { }
1394
 
1395
  virtual Type*
1396
  do_type();
1397
 
1398
  virtual void
1399
  do_determine_type(const Type_context*);
1400
 
1401
  virtual void
1402
  do_check_types(Gogo*);
1403
 
1404
  Expression*
1405
  do_copy()
1406
  {
1407
    return Expression::make_call(this->fn_->copy(),
1408
                                 (this->args_ == NULL
1409
                                  ? NULL
1410
                                  : this->args_->copy()),
1411
                                 this->is_varargs_, this->location());
1412
  }
1413
 
1414
  bool
1415
  do_must_eval_in_order() const;
1416
 
1417
  virtual tree
1418
  do_get_tree(Translate_context*);
1419
 
1420
  virtual bool
1421
  do_is_recover_call() const;
1422
 
1423
  virtual void
1424
  do_set_recover_arg(Expression*);
1425
 
1426
  // Let a builtin expression change the argument list.
1427
  void
1428
  set_args(Expression_list* args)
1429
  { this->args_ = args; }
1430
 
1431
  // Let a builtin expression lower varargs.
1432
  void
1433
  lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1434
                Type* varargs_type, size_t param_count);
1435
 
1436
  // Let a builtin expression check whether types have been
1437
  // determined.
1438
  bool
1439
  determining_types();
1440
 
1441
  void
1442
  do_dump_expression(Ast_dump_context*) const;
1443
 
1444
 private:
1445
  bool
1446
  check_argument_type(int, const Type*, const Type*, Location, bool);
1447
 
1448
  tree
1449
  interface_method_function(Translate_context*,
1450
                            Interface_field_reference_expression*,
1451
                            tree*);
1452
 
1453
  tree
1454
  set_results(Translate_context*, tree);
1455
 
1456
  // The function to call.
1457
  Expression* fn_;
1458
  // The arguments to pass.  This may be NULL if there are no
1459
  // arguments.
1460
  Expression_list* args_;
1461
  // The type of the expression, to avoid recomputing it.
1462
  Type* type_;
1463
  // The list of temporaries which will hold the results if the
1464
  // function returns a tuple.
1465
  std::vector<Temporary_statement*>* results_;
1466
  // The tree for the call, used for a call which returns a tuple.
1467
  tree tree_;
1468
  // True if the last argument is a varargs argument (f(a...)).
1469
  bool is_varargs_;
1470
  // True if this statement may pass hidden fields in the arguments.
1471
  // This is used for generated method stubs.
1472
  bool are_hidden_fields_ok_;
1473
  // True if varargs have already been lowered.
1474
  bool varargs_are_lowered_;
1475
  // True if types have been determined.
1476
  bool types_are_determined_;
1477
  // True if the call is an argument to a defer statement.
1478
  bool is_deferred_;
1479
  // True if we reported an error about a mismatch between call
1480
  // results and uses.  This is to avoid producing multiple errors
1481
  // when there are multiple Call_result_expressions.
1482
  bool issued_error_;
1483
};
1484
 
1485
// An expression which represents a pointer to a function.
1486
 
1487
class Func_expression : public Expression
1488
{
1489
 public:
1490
  Func_expression(Named_object* function, Expression* closure,
1491
                  Location location)
1492
    : Expression(EXPRESSION_FUNC_REFERENCE, location),
1493
      function_(function), closure_(closure)
1494
  { }
1495
 
1496
  // Return the object associated with the function.
1497
  const Named_object*
1498
  named_object() const
1499
  { return this->function_; }
1500
 
1501
  // Return the closure for this function.  This will return NULL if
1502
  // the function has no closure, which is the normal case.
1503
  Expression*
1504
  closure()
1505
  { return this->closure_; }
1506
 
1507
  // Return a tree for this function without evaluating the closure.
1508
  tree
1509
  get_tree_without_closure(Gogo*);
1510
 
1511
 protected:
1512
  int
1513
  do_traverse(Traverse*);
1514
 
1515
  Type*
1516
  do_type();
1517
 
1518
  void
1519
  do_determine_type(const Type_context*)
1520
  {
1521
    if (this->closure_ != NULL)
1522
      this->closure_->determine_type_no_context();
1523
  }
1524
 
1525
  Expression*
1526
  do_copy()
1527
  {
1528
    return Expression::make_func_reference(this->function_,
1529
                                           (this->closure_ == NULL
1530
                                            ? NULL
1531
                                            : this->closure_->copy()),
1532
                                           this->location());
1533
  }
1534
 
1535
  tree
1536
  do_get_tree(Translate_context*);
1537
 
1538
  void
1539
  do_dump_expression(Ast_dump_context*) const;
1540
 
1541
 private:
1542
  // The function itself.
1543
  Named_object* function_;
1544
  // A closure.  This is normally NULL.  For a nested function, it may
1545
  // be a heap-allocated struct holding pointers to all the variables
1546
  // referenced by this function and defined in enclosing functions.
1547
  Expression* closure_;
1548
};
1549
 
1550
// A reference to an unknown name.
1551
 
1552
class Unknown_expression : public Parser_expression
1553
{
1554
 public:
1555
  Unknown_expression(Named_object* named_object, Location location)
1556
    : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1557
      named_object_(named_object), no_error_message_(false),
1558
      is_composite_literal_key_(false)
1559
  { }
1560
 
1561
  // The associated named object.
1562
  Named_object*
1563
  named_object() const
1564
  { return this->named_object_; }
1565
 
1566
  // The name of the identifier which was unknown.
1567
  const std::string&
1568
  name() const;
1569
 
1570
  // Call this to indicate that we should not give an error if this
1571
  // name is never defined.  This is used to avoid knock-on errors
1572
  // during an erroneous parse.
1573
  void
1574
  set_no_error_message()
1575
  { this->no_error_message_ = true; }
1576
 
1577
  // Note that this expression is being used as the key in a composite
1578
  // literal, so it may be OK if it is not resolved.
1579
  void
1580
  set_is_composite_literal_key()
1581
  { this->is_composite_literal_key_ = true; }
1582
 
1583
  // Note that this expression should no longer be treated as a
1584
  // composite literal key.
1585
  void
1586
  clear_is_composite_literal_key()
1587
  { this->is_composite_literal_key_ = false; }
1588
 
1589
 protected:
1590
  Expression*
1591
  do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1592
 
1593
  Expression*
1594
  do_copy()
1595
  { return new Unknown_expression(this->named_object_, this->location()); }
1596
 
1597
  void
1598
  do_dump_expression(Ast_dump_context*) const;
1599
 
1600
 private:
1601
  // The unknown name.
1602
  Named_object* named_object_;
1603
  // True if we should not give errors if this is undefined.  This is
1604
  // used if there was a parse failure.
1605
  bool no_error_message_;
1606
  // True if this is the key in a composite literal.
1607
  bool is_composite_literal_key_;
1608
};
1609
 
1610
// An index expression.  This is lowered to an array index, a string
1611
// index, or a map index.
1612
 
1613
class Index_expression : public Parser_expression
1614
{
1615
 public:
1616
  Index_expression(Expression* left, Expression* start, Expression* end,
1617
                   Location location)
1618
    : Parser_expression(EXPRESSION_INDEX, location),
1619
      left_(left), start_(start), end_(end), is_lvalue_(false)
1620
  { }
1621
 
1622
  // Record that this expression is an lvalue.
1623
  void
1624
  set_is_lvalue()
1625
  { this->is_lvalue_ = true; }
1626
 
1627
  // Dump an index expression, i.e. an expression of the form
1628
  // expr[expr] or expr[expr:expr], to a dump context.
1629
  static void
1630
  dump_index_expression(Ast_dump_context*, const Expression* expr,
1631
                        const Expression* start, const Expression* end);
1632
 
1633
 protected:
1634
  int
1635
  do_traverse(Traverse*);
1636
 
1637
  Expression*
1638
  do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1639
 
1640
  Expression*
1641
  do_copy()
1642
  {
1643
    return new Index_expression(this->left_->copy(), this->start_->copy(),
1644
                                (this->end_ == NULL
1645
                                 ? NULL
1646
                                 : this->end_->copy()),
1647
                                this->location());
1648
  }
1649
 
1650
  bool
1651
  do_must_eval_subexpressions_in_order(int* skip) const
1652
  {
1653
    *skip = 1;
1654
    return true;
1655
  }
1656
 
1657
  void
1658
  do_dump_expression(Ast_dump_context*) const;
1659
 
1660
 private:
1661
  // The expression being indexed.
1662
  Expression* left_;
1663
  // The first index.
1664
  Expression* start_;
1665
  // The second index.  This is NULL for an index, non-NULL for a
1666
  // slice.
1667
  Expression* end_;
1668
  // Whether this is being used as an l-value.  We set this during the
1669
  // parse because map index expressions need to know.
1670
  bool is_lvalue_;
1671
};
1672
 
1673
// An index into a map.
1674
 
1675
class Map_index_expression : public Expression
1676
{
1677
 public:
1678
  Map_index_expression(Expression* map, Expression* index,
1679
                       Location location)
1680
    : Expression(EXPRESSION_MAP_INDEX, location),
1681
      map_(map), index_(index), is_lvalue_(false),
1682
      is_in_tuple_assignment_(false)
1683
  { }
1684
 
1685
  // Return the map.
1686
  Expression*
1687
  map()
1688
  { return this->map_; }
1689
 
1690
  const Expression*
1691
  map() const
1692
  { return this->map_; }
1693
 
1694
  // Return the index.
1695
  Expression*
1696
  index()
1697
  { return this->index_; }
1698
 
1699
  const Expression*
1700
  index() const
1701
  { return this->index_; }
1702
 
1703
  // Get the type of the map being indexed.
1704
  Map_type*
1705
  get_map_type() const;
1706
 
1707
  // Record that this map expression is an lvalue.  The difference is
1708
  // that an lvalue always inserts the key.
1709
  void
1710
  set_is_lvalue()
1711
  { this->is_lvalue_ = true; }
1712
 
1713
  // Return whether this map expression occurs in an assignment to a
1714
  // pair of values.
1715
  bool
1716
  is_in_tuple_assignment() const
1717
  { return this->is_in_tuple_assignment_; }
1718
 
1719
  // Record that this map expression occurs in an assignment to a pair
1720
  // of values.
1721
  void
1722
  set_is_in_tuple_assignment()
1723
  { this->is_in_tuple_assignment_ = true; }
1724
 
1725
  // Return a tree for the map index.  This returns a tree which
1726
  // evaluates to a pointer to a value in the map.  If INSERT is true,
1727
  // the key will be inserted if not present, and the value pointer
1728
  // will be zero initialized.  If INSERT is false, and the key is not
1729
  // present in the map, the pointer will be NULL.
1730
  tree
1731
  get_value_pointer(Translate_context*, bool insert);
1732
 
1733
 protected:
1734
  int
1735
  do_traverse(Traverse*);
1736
 
1737
  Type*
1738
  do_type();
1739
 
1740
  void
1741
  do_determine_type(const Type_context*);
1742
 
1743
  void
1744
  do_check_types(Gogo*);
1745
 
1746
  Expression*
1747
  do_copy()
1748
  {
1749
    return Expression::make_map_index(this->map_->copy(),
1750
                                      this->index_->copy(),
1751
                                      this->location());
1752
  }
1753
 
1754
  bool
1755
  do_must_eval_subexpressions_in_order(int* skip) const
1756
  {
1757
    *skip = 1;
1758
    return true;
1759
  }
1760
 
1761
  // A map index expression is an lvalue but it is not addressable.
1762
 
1763
  tree
1764
  do_get_tree(Translate_context*);
1765
 
1766
  void
1767
  do_dump_expression(Ast_dump_context*) const;
1768
 
1769
 private:
1770
  // The map we are looking into.
1771
  Expression* map_;
1772
  // The index.
1773
  Expression* index_;
1774
  // Whether this is an lvalue.
1775
  bool is_lvalue_;
1776
  // Whether this is in a tuple assignment to a pair of values.
1777
  bool is_in_tuple_assignment_;
1778
};
1779
 
1780
// An expression which represents a method bound to its first
1781
// argument.
1782
 
1783
class Bound_method_expression : public Expression
1784
{
1785
 public:
1786
  Bound_method_expression(Expression* expr, Named_object* method,
1787
                          Location location)
1788
    : Expression(EXPRESSION_BOUND_METHOD, location),
1789
      expr_(expr), expr_type_(NULL), method_(method)
1790
  { }
1791
 
1792
  // Return the object which is the first argument.
1793
  Expression*
1794
  first_argument()
1795
  { return this->expr_; }
1796
 
1797
  // Return the implicit type of the first argument.  This will be
1798
  // non-NULL when using a method from an anonymous field without
1799
  // using an explicit stub.
1800
  Type*
1801
  first_argument_type() const
1802
  { return this->expr_type_; }
1803
 
1804
  // Return the method function.
1805
  Named_object*
1806
  method()
1807
  { return this->method_; }
1808
 
1809
  // Set the implicit type of the expression.
1810
  void
1811
  set_first_argument_type(Type* type)
1812
  { this->expr_type_ = type; }
1813
 
1814
 protected:
1815
  int
1816
  do_traverse(Traverse*);
1817
 
1818
  Type*
1819
  do_type();
1820
 
1821
  void
1822
  do_determine_type(const Type_context*);
1823
 
1824
  void
1825
  do_check_types(Gogo*);
1826
 
1827
  Expression*
1828
  do_copy()
1829
  {
1830
    return new Bound_method_expression(this->expr_->copy(), this->method_,
1831
                                       this->location());
1832
  }
1833
 
1834
  tree
1835
  do_get_tree(Translate_context*);
1836
 
1837
  void
1838
  do_dump_expression(Ast_dump_context*) const;
1839
 
1840
 private:
1841
  // The object used to find the method.  This is passed to the method
1842
  // as the first argument.
1843
  Expression* expr_;
1844
  // The implicit type of the object to pass to the method.  This is
1845
  // NULL in the normal case, non-NULL when using a method from an
1846
  // anonymous field which does not require a stub.
1847
  Type* expr_type_;
1848
  // The method itself.
1849
  Named_object* method_;
1850
};
1851
 
1852
// A reference to a field in a struct.
1853
 
1854
class Field_reference_expression : public Expression
1855
{
1856
 public:
1857
  Field_reference_expression(Expression* expr, unsigned int field_index,
1858
                             Location location)
1859
    : Expression(EXPRESSION_FIELD_REFERENCE, location),
1860
      expr_(expr), field_index_(field_index)
1861
  { }
1862
 
1863
  // Return the struct expression.
1864
  Expression*
1865
  expr() const
1866
  { return this->expr_; }
1867
 
1868
  // Return the field index.
1869
  unsigned int
1870
  field_index() const
1871
  { return this->field_index_; }
1872
 
1873
  // Set the struct expression.  This is used when parsing.
1874
  void
1875
  set_struct_expression(Expression* expr)
1876
  {
1877
    go_assert(this->expr_ == NULL);
1878
    this->expr_ = expr;
1879
  }
1880
 
1881
 protected:
1882
  int
1883
  do_traverse(Traverse* traverse)
1884
  { return Expression::traverse(&this->expr_, traverse); }
1885
 
1886
  Type*
1887
  do_type();
1888
 
1889
  void
1890
  do_determine_type(const Type_context*)
1891
  { this->expr_->determine_type_no_context(); }
1892
 
1893
  void
1894
  do_check_types(Gogo*);
1895
 
1896
  Expression*
1897
  do_copy()
1898
  {
1899
    return Expression::make_field_reference(this->expr_->copy(),
1900
                                            this->field_index_,
1901
                                            this->location());
1902
  }
1903
 
1904
  bool
1905
  do_is_addressable() const
1906
  { return this->expr_->is_addressable(); }
1907
 
1908
  tree
1909
  do_get_tree(Translate_context*);
1910
 
1911
  void
1912
  do_dump_expression(Ast_dump_context*) const;
1913
 
1914
 private:
1915
  // The expression we are looking into.  This should have a type of
1916
  // struct.
1917
  Expression* expr_;
1918
  // The zero-based index of the field we are retrieving.
1919
  unsigned int field_index_;
1920
};
1921
 
1922
// A reference to a field of an interface.
1923
 
1924
class Interface_field_reference_expression : public Expression
1925
{
1926
 public:
1927
  Interface_field_reference_expression(Expression* expr,
1928
                                       const std::string& name,
1929
                                       Location location)
1930
    : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1931
      expr_(expr), name_(name)
1932
  { }
1933
 
1934
  // Return the expression for the interface object.
1935
  Expression*
1936
  expr()
1937
  { return this->expr_; }
1938
 
1939
  // Return the name of the method to call.
1940
  const std::string&
1941
  name() const
1942
  { return this->name_; }
1943
 
1944
  // Return a tree for the pointer to the function to call, given a
1945
  // tree for the expression.
1946
  tree
1947
  get_function_tree(Translate_context*, tree);
1948
 
1949
  // Return a tree for the first argument to pass to the interface
1950
  // function, given a tree for the expression.  This is the real
1951
  // object associated with the interface object.
1952
  tree
1953
  get_underlying_object_tree(Translate_context*, tree);
1954
 
1955
 protected:
1956
  int
1957
  do_traverse(Traverse* traverse);
1958
 
1959
  Type*
1960
  do_type();
1961
 
1962
  void
1963
  do_determine_type(const Type_context*);
1964
 
1965
  void
1966
  do_check_types(Gogo*);
1967
 
1968
  Expression*
1969
  do_copy()
1970
  {
1971
    return Expression::make_interface_field_reference(this->expr_->copy(),
1972
                                                      this->name_,
1973
                                                      this->location());
1974
  }
1975
 
1976
  tree
1977
  do_get_tree(Translate_context*);
1978
 
1979
  void
1980
  do_dump_expression(Ast_dump_context*) const;
1981
 
1982
 private:
1983
  // The expression for the interface object.  This should have a type
1984
  // of interface or pointer to interface.
1985
  Expression* expr_;
1986
  // The field we are retrieving--the name of the method.
1987
  std::string name_;
1988
};
1989
 
1990
// A type guard expression.
1991
 
1992
class Type_guard_expression : public Expression
1993
{
1994
 public:
1995
  Type_guard_expression(Expression* expr, Type* type, Location location)
1996
    : Expression(EXPRESSION_TYPE_GUARD, location),
1997
      expr_(expr), type_(type)
1998
  { }
1999
 
2000
  // Return the expression to convert.
2001
  Expression*
2002
  expr()
2003
  { return this->expr_; }
2004
 
2005
  // Return the type to which to convert.
2006
  Type*
2007
  type()
2008
  { return this->type_; }
2009
 
2010
 protected:
2011
  int
2012
  do_traverse(Traverse* traverse);
2013
 
2014
  Type*
2015
  do_type()
2016
  { return this->type_; }
2017
 
2018
  void
2019
  do_determine_type(const Type_context*)
2020
  { this->expr_->determine_type_no_context(); }
2021
 
2022
  void
2023
  do_check_types(Gogo*);
2024
 
2025
  Expression*
2026
  do_copy()
2027
  {
2028
    return new Type_guard_expression(this->expr_->copy(), this->type_,
2029
                                     this->location());
2030
  }
2031
 
2032
  tree
2033
  do_get_tree(Translate_context*);
2034
 
2035
  void
2036
  do_dump_expression(Ast_dump_context*) const;
2037
 
2038
 private:
2039
  // The expression to convert.
2040
  Expression* expr_;
2041
  // The type to which to convert.
2042
  Type* type_;
2043
};
2044
 
2045
// A receive expression.
2046
 
2047
class Receive_expression : public Expression
2048
{
2049
 public:
2050
  Receive_expression(Expression* channel, Location location)
2051
    : Expression(EXPRESSION_RECEIVE, location),
2052
      channel_(channel)
2053
  { }
2054
 
2055
  // Return the channel.
2056
  Expression*
2057
  channel()
2058
  { return this->channel_; }
2059
 
2060
 protected:
2061
  int
2062
  do_traverse(Traverse* traverse)
2063
  { return Expression::traverse(&this->channel_, traverse); }
2064
 
2065
  void
2066
  do_discarding_value()
2067
  { }
2068
 
2069
  Type*
2070
  do_type();
2071
 
2072
  void
2073
  do_determine_type(const Type_context*)
2074
  { this->channel_->determine_type_no_context(); }
2075
 
2076
  void
2077
  do_check_types(Gogo*);
2078
 
2079
  Expression*
2080
  do_copy()
2081
  {
2082
    return Expression::make_receive(this->channel_->copy(), this->location());
2083
  }
2084
 
2085
  bool
2086
  do_must_eval_in_order() const
2087
  { return true; }
2088
 
2089
  tree
2090
  do_get_tree(Translate_context*);
2091
 
2092
  void
2093
  do_dump_expression(Ast_dump_context*) const;
2094
 
2095
 private:
2096
  // The channel from which we are receiving.
2097
  Expression* channel_;
2098
};
2099
 
2100
#endif // !defined(GO_EXPRESSIONS_H)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.