Daily bump.
[gcc.git] / gcc / go / gofrontend / gogo.h
1 // gogo.h -- Go frontend parsed representation. -*- 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_GOGO_H
8 #define GO_GOGO_H
9
10 #include "go-linemap.h"
11
12 class Traverse;
13 class Statement_inserter;
14 class Type;
15 class Type_equal;
16 class Typed_identifier;
17 class Typed_identifier_list;
18 class Function_type;
19 class Expression;
20 class Expression_list;
21 class Statement;
22 class Temporary_statement;
23 class Block;
24 class Function;
25 class Bindings;
26 class Bindings_snapshot;
27 class Package;
28 class Variable;
29 class Pointer_type;
30 class Struct_type;
31 class Struct_field;
32 class Struct_field_list;
33 class Array_type;
34 class Map_type;
35 class Channel_type;
36 class Interface_type;
37 class Named_type;
38 class Forward_declaration_type;
39 class Named_object;
40 class Label;
41 class Translate_context;
42 class Backend;
43 class Export;
44 class Export_function_body;
45 class Import;
46 class Import_function_body;
47 class Bexpression;
48 class Btype;
49 class Bstatement;
50 class Bblock;
51 class Bvariable;
52 class Blabel;
53 class Bfunction;
54 class Escape_context;
55 class Node;
56
57 // This file declares the basic classes used to hold the internal
58 // representation of Go which is built by the parser.
59
60 // The name of some backend object. Backend objects have a
61 // user-visible name and an assembler name. The user visible name
62 // might include arbitrary Unicode characters. The assembler name
63 // will not.
64
65 class Backend_name
66 {
67 public:
68 Backend_name()
69 : prefix_(NULL), components_(), count_(0), suffix_(),
70 is_asm_name_(false), is_non_identifier_(false)
71 {}
72
73 // Set the prefix. Prefixes are always constant strings.
74 void
75 set_prefix(const char* p)
76 {
77 go_assert(this->prefix_ == NULL && !this->is_asm_name_);
78 this->prefix_ = p;
79 }
80
81 // Set the suffix.
82 void
83 set_suffix(const std::string& s)
84 {
85 go_assert(this->suffix_.empty() && !this->is_asm_name_);
86 this->suffix_ = s;
87 }
88
89 // Append to the suffix.
90 void
91 append_suffix(const std::string& s)
92 {
93 if (this->is_asm_name_)
94 this->components_[0].append(s);
95 else
96 this->suffix_.append(s);
97 }
98
99 // Add a component.
100 void
101 add(const std::string& c)
102 {
103 go_assert(this->count_ < Backend_name::max_components
104 && !this->is_asm_name_);
105 this->components_[this->count_] = c;
106 ++this->count_;
107 }
108
109 // Set an assembler name specified by the user. This overrides both
110 // the user-visible name and the assembler name. No further
111 // encoding is applied.
112 void
113 set_asm_name(const std::string& n)
114 {
115 go_assert(this->prefix_ == NULL
116 && this->count_ == 0
117 && this->suffix_.empty()
118 && !this->is_asm_name_);
119 this->components_[0] = n;
120 this->is_asm_name_ = true;
121 }
122
123 // Whether some component includes some characters that can't appear
124 // in an identifier.
125 bool
126 is_non_identifier() const
127 { return this->is_non_identifier_; }
128
129 // Record that some component includes some character that can't
130 // appear in an identifier.
131 void
132 set_is_non_identifier()
133 { this->is_non_identifier_ = true; }
134
135 // Get the user visible name.
136 std::string
137 name() const;
138
139 // Get the assembler name. This may be the same as the user visible
140 // name.
141 std::string
142 asm_name() const;
143
144 // Get an optional assembler name: if it would be the same as the
145 // user visible name, this returns the empty string.
146 std::string
147 optional_asm_name() const;
148
149 private:
150 // The maximum number of components.
151 static const int max_components = 4;
152
153 // An optional prefix that does not require encoding.
154 const char *prefix_;
155 // Up to four components. The name will include these components
156 // separated by dots. Each component will be underscore-encoded
157 // (see the long comment near the top of names.cc).
158 std::string components_[Backend_name::max_components];
159 // Number of components.
160 int count_;
161 // An optional suffix that does not require encoding.
162 std::string suffix_;
163 // True if components_[0] is an assembler name specified by the user.
164 bool is_asm_name_;
165 // True if some component includes some character that can't
166 // normally appear in an identifier.
167 bool is_non_identifier_;
168 };
169
170 // An initialization function for an imported package. This is a
171 // magic function which initializes variables and runs the "init"
172 // function.
173
174 class Import_init
175 {
176 public:
177 Import_init(const std::string& package_name, const std::string& init_name,
178 int priority)
179 : package_name_(package_name), init_name_(init_name), priority_(priority)
180 { }
181
182 // The name of the package being imported.
183 const std::string&
184 package_name() const
185 { return this->package_name_; }
186
187 // The name of the package's init function.
188 const std::string&
189 init_name() const
190 { return this->init_name_; }
191
192 // Older V1 export data uses a priority scheme to order
193 // initialization functions; functions with a lower priority number
194 // must be run first. This value will be set to -1 for current
195 // generation objects, and will take on a non-negative value only
196 // when importing a V1-vintage object.
197 int
198 priority() const
199 { return this->priority_; }
200
201 // Reset priority.
202 void
203 set_priority(int new_priority)
204 { this->priority_ = new_priority; }
205
206 // Record the fact that some other init fcn must be run before this init fcn.
207 void
208 record_precursor_fcn(std::string init_fcn_name)
209 { this->precursor_functions_.insert(init_fcn_name); }
210
211 // Return the list of precursor fcns for this fcn (must be run before it).
212 const std::set<std::string>&
213 precursors() const
214 { return this->precursor_functions_; }
215
216 // Whether this is a dummy init, which is used only to record transitive import.
217 bool
218 is_dummy() const
219 { return this->init_name_[0] == '~'; }
220
221 private:
222 // The name of the package being imported.
223 std::string package_name_;
224 // The name of the package's init function.
225 std::string init_name_;
226 // Names of init functions that must be run before this fcn.
227 std::set<std::string> precursor_functions_;
228 // Priority for this function. See note above on obsolescence.
229 int priority_;
230 };
231
232 // For sorting purposes.
233
234 struct Import_init_lt {
235 bool operator()(const Import_init* i1, const Import_init* i2) const
236 {
237 return i1->init_name() < i2->init_name();
238 }
239 };
240
241 // Set of import init objects.
242 class Import_init_set : public std::set<Import_init*, Import_init_lt> {
243 };
244
245 inline bool
246 priority_compare(const Import_init* i1, const Import_init* i2)
247 {
248 if (i1->priority() < i2->priority())
249 return true;
250 if (i1->priority() > i2->priority())
251 return false;
252 if (i1->package_name() != i2->package_name())
253 return i1->package_name() < i2->package_name();
254 return i1->init_name() < i2->init_name();
255 }
256
257 // The holder for the internal representation of the entire
258 // compilation unit.
259
260 class Gogo
261 {
262 public:
263 // Create the IR, passing in the sizes of the types "int" and
264 // "uintptr" in bits.
265 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
266
267 // Get the backend generator.
268 Backend*
269 backend()
270 { return this->backend_; }
271
272 // Get the Location generator.
273 Linemap*
274 linemap()
275 { return this->linemap_; }
276
277 // Get the package name.
278 const std::string&
279 package_name() const;
280
281 // Set the package name.
282 void
283 set_package_name(const std::string&, Location);
284
285 // Return whether this is the "main" package.
286 bool
287 is_main_package() const;
288
289 // If necessary, adjust the name to use for a hidden symbol. We add
290 // the package name, so that hidden symbols in different packages do
291 // not collide.
292 std::string
293 pack_hidden_name(const std::string& name, bool is_exported) const
294 {
295 return (is_exported
296 ? name
297 : '.' + this->pkgpath() + '.' + name);
298 }
299
300 // Unpack a name which may have been hidden. Returns the
301 // user-visible name of the object.
302 static std::string
303 unpack_hidden_name(const std::string& name)
304 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
305
306 // Return whether a possibly packed name is hidden.
307 static bool
308 is_hidden_name(const std::string& name)
309 { return name[0] == '.'; }
310
311 // Return the package path of a hidden name.
312 static std::string
313 hidden_name_pkgpath(const std::string& name)
314 {
315 go_assert(Gogo::is_hidden_name(name));
316 return name.substr(1, name.rfind('.') - 1);
317 }
318
319 // Given a name which may or may not have been hidden, append the
320 // appropriate version of the name to the result string.
321 static void
322 append_possibly_hidden_name(std::string *result, const std::string& name);
323
324 // Given a name which may or may not have been hidden, return the
325 // name to use in an error message.
326 static std::string
327 message_name(const std::string& name);
328
329 // Return whether a name is the blank identifier _.
330 static bool
331 is_sink_name(const std::string& name)
332 {
333 return (name[0] == '.'
334 && name[name.length() - 1] == '_'
335 && name[name.length() - 2] == '.')
336 || (name[0] == '_'
337 && name.length() == 1);
338 }
339
340 // Helper used when adding parameters (including receiver param) to the
341 // bindings of a function. If the specified parameter name is empty or
342 // corresponds to the sink name, param name is replaced with a new unique
343 // name. PNAME is the address of a string containing the parameter variable
344 // name to be checked/updated; TAG is a descriptive tag to be used in
345 // manufacturing the new unique name, and COUNT is the address of a counter
346 // holding the number of params renamed so far with the tag in question.
347 static void
348 rename_if_empty(std::string* pname, const char* tag, unsigned* count);
349
350 // Convert a pkgpath into a string suitable for a symbol
351 static std::string
352 pkgpath_for_symbol(const std::string& pkgpath);
353
354 // Compute a hash code for a string, given a seed.
355 static unsigned int
356 hash_string(const std::string&, unsigned int);
357
358 // Return the package path to use for reflect.Type.PkgPath.
359 const std::string&
360 pkgpath() const;
361
362 // Return the package path to use for a symbol name.
363 const std::string&
364 pkgpath_symbol() const;
365
366 // Set the package path from a command line option.
367 void
368 set_pkgpath(const std::string&);
369
370 // Set the prefix from a command line option.
371 void
372 set_prefix(const std::string&);
373
374 // Return whether pkgpath was set from a command line option.
375 bool
376 pkgpath_from_option() const
377 { return this->pkgpath_from_option_; }
378
379 // Return the relative import path as set from the command line.
380 // Returns an empty string if it was not set.
381 const std::string&
382 relative_import_path() const
383 { return this->relative_import_path_; }
384
385 // Set the relative import path from a command line option.
386 void
387 set_relative_import_path(const std::string& s)
388 { this->relative_import_path_ = s; }
389
390 // Set the C header file to write. This is used for the runtime
391 // package.
392 void
393 set_c_header(const std::string& s)
394 { this->c_header_ = s; }
395
396 // Read an embedcfg file.
397 void
398 read_embedcfg(const char* filename);
399
400 // Return whether the current file imports "embed".
401 bool
402 is_embed_imported() const;
403
404 // Build an initializer for a variable with a go:embed directive.
405 Expression*
406 initializer_for_embeds(Type*, const std::vector<std::string>*, Location);
407
408 // Return whether to check for division by zero in binary operations.
409 bool
410 check_divide_by_zero() const
411 { return this->check_divide_by_zero_; }
412
413 // Set the option to check division by zero from a command line option.
414 void
415 set_check_divide_by_zero(bool b)
416 { this->check_divide_by_zero_ = b; }
417
418 // Return whether to check for division overflow in binary operations.
419 bool
420 check_divide_overflow() const
421 { return this->check_divide_overflow_; }
422
423 // Set the option to check division overflow from a command line option.
424 void
425 set_check_divide_overflow(bool b)
426 { this->check_divide_overflow_ = b; }
427
428 // Return whether we are compiling the runtime package.
429 bool
430 compiling_runtime() const
431 { return this->compiling_runtime_; }
432
433 // Set whether we are compiling the runtime package.
434 void
435 set_compiling_runtime(bool b)
436 { this->compiling_runtime_ = b; }
437
438 // Return the level of escape analysis debug information to emit.
439 int
440 debug_escape_level() const
441 { return this->debug_escape_level_; }
442
443 // Set the level of escape analysis debugging from a command line option.
444 void
445 set_debug_escape_level(int level)
446 { this->debug_escape_level_ = level; }
447
448 // Return the hash for debug escape analysis.
449 std::string
450 debug_escape_hash() const
451 { return this->debug_escape_hash_; }
452
453 // Set the hash value for debug escape analysis.
454 void
455 set_debug_escape_hash(const std::string& s)
456 { this->debug_escape_hash_ = s; }
457
458 // Return whether to output optimization diagnostics.
459 bool
460 debug_optimization() const
461 { return this->debug_optimization_; }
462
463 // Set the option to output optimization diagnostics.
464 void
465 set_debug_optimization(bool b)
466 { this->debug_optimization_ = b; }
467
468 // Dump to stderr for debugging
469 void debug_dump();
470
471 // Return the size threshold used to determine whether to issue
472 // a nil-check for a given pointer dereference. A threshold of -1
473 // implies that all potentially faulting dereference ops should
474 // be nil-checked. A positive threshold of N implies that a deref
475 // of *P where P has size less than N doesn't need a nil check.
476 int64_t
477 nil_check_size_threshold() const
478 { return this->nil_check_size_threshold_; }
479
480 // Set the nil-check size threshold, as described above.
481 void
482 set_nil_check_size_threshold(int64_t bytes)
483 { this->nil_check_size_threshold_ = bytes; }
484
485 // Return whether runtime.eqtype calls are needed when comparing
486 // type descriptors.
487 bool
488 need_eqtype() const
489 { return this->need_eqtype_; }
490
491 // Set if calls to runtime.eqtype are needed.
492 void
493 set_need_eqtype(bool b)
494 { this->need_eqtype_ = b; }
495
496 // Import a package. FILENAME is the file name argument, LOCAL_NAME
497 // is the local name to give to the package. If LOCAL_NAME is empty
498 // the declarations are added to the global scope.
499 void
500 import_package(const std::string& filename, const std::string& local_name,
501 bool is_local_name_exported, bool must_exist, Location);
502
503 // Whether we are the global binding level.
504 bool
505 in_global_scope() const;
506
507 // Look up a name in the current binding contours.
508 Named_object*
509 lookup(const std::string&, Named_object** pfunction) const;
510
511 // Look up a name in the current block.
512 Named_object*
513 lookup_in_block(const std::string&) const;
514
515 // Look up a name in the global namespace--the universal scope.
516 Named_object*
517 lookup_global(const char*) const;
518
519 // Add a new imported package. REAL_NAME is the real name of the
520 // package. ALIAS is the alias of the package; this may be the same
521 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
522 // this package should be added to the global namespace; this is
523 // true if the alias is ".". LOCATION is the location of the import
524 // statement. This returns the new package, or NULL on error.
525 Package*
526 add_imported_package(const std::string& real_name, const std::string& alias,
527 bool is_alias_exported,
528 const std::string& pkgpath,
529 const std::string& pkgpath_symbol,
530 Location location,
531 bool* padd_to_globals);
532
533 // Register a package. This package may or may not be imported.
534 // This returns the Package structure for the package, creating if
535 // it necessary.
536 Package*
537 register_package(const std::string& pkgpath,
538 const std::string& pkgpath_symbol, Location);
539
540 // Look up a package by pkgpath, and return its pkgpath_symbol.
541 std::string
542 pkgpath_symbol_for_package(const std::string&);
543
544 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
545 // method function should be added to the type of its receiver.
546 Named_object*
547 start_function(const std::string& name, Function_type* type,
548 bool add_method_to_type, Location);
549
550 // Finish compiling a function.
551 void
552 finish_function(Location);
553
554 // Return the current function.
555 Named_object*
556 current_function() const;
557
558 // Return the current block.
559 Block*
560 current_block();
561
562 // Start a new block. This is not initially associated with a
563 // function.
564 void
565 start_block(Location);
566
567 // Finish the current block and return it.
568 Block*
569 finish_block(Location);
570
571 // Declare an erroneous name. This is used to avoid knock-on errors
572 // after a parsing error.
573 Named_object*
574 add_erroneous_name(const std::string& name);
575
576 // Declare an unknown name. This is used while parsing. The name
577 // must be resolved by the end of the parse. Unknown names are
578 // always added at the package level.
579 Named_object*
580 add_unknown_name(const std::string& name, Location);
581
582 // Declare a function.
583 Named_object*
584 declare_function(const std::string&, Function_type*, Location);
585
586 // Declare a function at the package level. This is used for
587 // functions generated for a type.
588 Named_object*
589 declare_package_function(const std::string&, Function_type*, Location);
590
591 // Add a function declaration to the list of functions we may want
592 // to inline.
593 void
594 add_imported_inlinable_function(Named_object*);
595
596 // Add a function to the list of functions that we do want to
597 // inline.
598 void
599 add_imported_inline_function(Named_object* no)
600 { this->imported_inline_functions_.push_back(no); }
601
602 // Add a label.
603 Label*
604 add_label_definition(const std::string&, Location);
605
606 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
607 // report errors for a goto from the current location to the label
608 // location.
609 Label*
610 add_label_reference(const std::string&, Location,
611 bool issue_goto_errors);
612
613 // An analysis set is a list of functions paired with a boolean that indicates
614 // whether the list of functions are recursive.
615 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
616
617 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
618 // package.
619 void
620 add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
621 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
622
623 // Return a snapshot of the current binding state.
624 Bindings_snapshot*
625 bindings_snapshot(Location);
626
627 // Add a statement to the current block.
628 void
629 add_statement(Statement*);
630
631 // Add a block to the current block.
632 void
633 add_block(Block*, Location);
634
635 // Add a constant.
636 Named_object*
637 add_constant(const Typed_identifier&, Expression*, int iota_value);
638
639 // Add a type.
640 void
641 add_type(const std::string&, Type*, Location);
642
643 // Add a named type. This is used for builtin types, and to add an
644 // imported type to the global scope.
645 void
646 add_named_type(Named_type*);
647
648 // Declare a type.
649 Named_object*
650 declare_type(const std::string&, Location);
651
652 // Declare a type at the package level. This is used when the
653 // parser sees an unknown name where a type name is required.
654 Named_object*
655 declare_package_type(const std::string&, Location);
656
657 // Define a type which was already declared.
658 void
659 define_type(Named_object*, Named_type*);
660
661 // Add a variable.
662 Named_object*
663 add_variable(const std::string&, Variable*);
664
665 // Add a sink--a reference to the blank identifier _.
666 Named_object*
667 add_sink();
668
669 // Add a type which needs to be verified. This is used for sink
670 // types, just to give appropriate error messages.
671 void
672 add_type_to_verify(Type* type);
673
674 // Add a named object to the current namespace. This is used for
675 // import . "package".
676 void
677 add_dot_import_object(Named_object*);
678
679 // Add an identifier to the list of names seen in the file block.
680 void
681 add_file_block_name(const std::string& name, Location location)
682 { this->file_block_names_[name] = location; }
683
684 // Add a linkname, from the go:linkname compiler directive. This
685 // changes the externally visible name of GO_NAME to be EXT_NAME.
686 // If EXT_NAME is the empty string, GO_NAME is unchanged, but the
687 // symbol is made publicly visible.
688 void
689 add_linkname(const std::string& go_name, bool is_exported,
690 const std::string& ext_name, Location location);
691
692 // Mark all local variables in current bindings as used. This is
693 // used when there is a parse error to avoid useless errors.
694 void
695 mark_locals_used();
696
697 // Note that we've seen an interface type. This is used to build
698 // all required interface method tables.
699 void
700 record_interface_type(Interface_type*);
701
702 // Note that we need an initialization function.
703 void
704 set_need_init_fn()
705 { this->need_init_fn_ = true; }
706
707 // Return whether the current file imported the unsafe package.
708 bool
709 current_file_imported_unsafe() const
710 { return this->current_file_imported_unsafe_; }
711
712 // Clear out all names in file scope. This is called when we start
713 // parsing a new file.
714 void
715 clear_file_scope();
716
717 // Record that VAR1 must be initialized after VAR2. This is used
718 // when VAR2 does not appear in VAR1's INIT or PREINIT.
719 void
720 record_var_depends_on(Variable* var1, Named_object* var2)
721 {
722 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
723 this->var_deps_[var1] = var2;
724 }
725
726 // Return the variable that VAR depends on, or NULL if none.
727 Named_object*
728 var_depends_on(Variable* var) const
729 {
730 Var_deps::const_iterator p = this->var_deps_.find(var);
731 return p != this->var_deps_.end() ? p->second : NULL;
732 }
733
734 // Queue up a type-specific hash function to be written out. This
735 // is used when a type-specific hash function is needed when not at
736 // top level.
737 void
738 queue_hash_function(Type* type, int64_t size, Backend_name*,
739 Function_type* hash_fntype);
740
741 // Queue up a type-specific equal function to be written out. This
742 // is used when a type-specific equal function is needed when not at
743 // top level.
744 void
745 queue_equal_function(Type* type, Named_type* name, int64_t size,
746 Backend_name*, Function_type* equal_fntype);
747
748 // Write out queued specific type functions.
749 void
750 write_specific_type_functions();
751
752 // Whether we are done writing out specific type functions.
753 bool
754 specific_type_functions_are_written() const
755 { return this->specific_type_functions_are_written_; }
756
757 // Add a pointer that needs to be added to the list of objects
758 // traversed by the garbage collector. This should be an expression
759 // of pointer type that points to static storage. It's not
760 // necessary to add global variables to this list, just global
761 // variable initializers that would otherwise not be seen.
762 void
763 add_gc_root(Expression* expr)
764 {
765 this->set_need_init_fn();
766 this->gc_roots_.push_back(expr);
767 }
768
769 // Add a type to the descriptor list.
770 void
771 add_type_descriptor(Type* type)
772 { this->type_descriptors_.push_back(type); }
773
774 // Traverse the tree. See the Traverse class.
775 void
776 traverse(Traverse*);
777
778 // Define the predeclared global names.
779 void
780 define_global_names();
781
782 // Verify and complete all types.
783 void
784 verify_types();
785
786 // Lower the parse tree.
787 void
788 lower_parse_tree();
789
790 // Lower all the statements in a block.
791 void
792 lower_block(Named_object* function, Block*);
793
794 // Lower an expression.
795 void
796 lower_expression(Named_object* function, Statement_inserter*, Expression**);
797
798 // Lower a constant.
799 void
800 lower_constant(Named_object*);
801
802 // Flatten all the statements in a block.
803 void
804 flatten_block(Named_object* function, Block*);
805
806 // Flatten an expression.
807 void
808 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
809
810 // Create all necessary function descriptors.
811 void
812 create_function_descriptors();
813
814 // Finalize the method lists and build stub methods for named types.
815 void
816 finalize_methods();
817
818 // Finalize the method list for one type.
819 void
820 finalize_methods_for_type(Type*);
821
822 // Work out the types to use for unspecified variables and
823 // constants.
824 void
825 determine_types();
826
827 // Type check the program.
828 void
829 check_types();
830
831 // Check the types in a single block. This is used for complicated
832 // go statements.
833 void
834 check_types_in_block(Block*);
835
836 // Check for return statements.
837 void
838 check_return_statements();
839
840 // Remove deadcode.
841 void
842 remove_deadcode();
843
844 // Make implicit type conversions explicit.
845 void
846 add_conversions();
847
848 // Make implicit type conversions explicit in a block.
849 void
850 add_conversions_in_block(Block*);
851
852 // Analyze the program flow for escape information.
853 void
854 analyze_escape();
855
856 // Discover the groups of possibly recursive functions in this package.
857 void
858 discover_analysis_sets();
859
860 // Build a connectivity graph between the objects in each analyzed function.
861 void
862 assign_connectivity(Escape_context*, Named_object*);
863
864 // Traverse the objects in the connecitivty graph from the sink, adjusting the
865 // escape levels of each object.
866 void
867 propagate_escape(Escape_context*, Node*);
868
869 // Add notes about the escape level of a function's input and output
870 // parameters for exporting and importing top level functions.
871 void
872 tag_function(Escape_context*, Named_object*);
873
874 // Reclaim memory of escape analysis Nodes.
875 void
876 reclaim_escape_nodes();
877
878 // Do all exports.
879 void
880 do_exports();
881
882 // Add an import control function for an imported package to the
883 // list.
884 void
885 add_import_init_fn(const std::string& package_name,
886 const std::string& init_name, int prio);
887
888 // Return the Import_init for a given init name.
889 Import_init*
890 lookup_init(const std::string& init_name);
891
892 // Turn short-cut operators (&&, ||) into explicit if statements.
893 void
894 remove_shortcuts();
895
896 // Turn short-cut operators into explicit if statements in a block.
897 void
898 remove_shortcuts_in_block(Block*);
899
900 // Use temporary variables to force order of evaluation.
901 void
902 order_evaluations();
903
904 // Order evaluations in a block.
905 void
906 order_block(Block*);
907
908 // Add write barriers as needed.
909 void
910 add_write_barriers();
911
912 // Return whether an assignment that sets LHS to RHS needs a write
913 // barrier.
914 bool
915 assign_needs_write_barrier(Expression* lhs,
916 Unordered_set(const Named_object*)*);
917
918 // Return whether EXPR is the address of a variable that can be set
919 // without a write barrier. That is, if this returns true, then an
920 // assignment to *EXPR does not require a write barrier.
921 bool
922 is_nonwb_pointer(Expression* expr, Unordered_set(const Named_object*)*);
923
924 // Return an assignment that sets LHS to RHS using a write barrier.
925 // This returns an if statement that checks whether write barriers
926 // are enabled. If not, it does LHS = RHS, otherwise it calls the
927 // appropriate write barrier function.
928 Statement*
929 assign_with_write_barrier(Function*, Block*, Statement_inserter*,
930 Expression* lhs, Expression* rhs, Location);
931
932 // Return a statement that tests whether write barriers are enabled
933 // and executes either the efficient code (WITHOUT) or the write
934 // barrier function call (WITH), depending.
935 Statement*
936 check_write_barrier(Block*, Statement* without, Statement* with);
937
938 // Flatten parse tree.
939 void
940 flatten();
941
942 // Build thunks for functions which call recover.
943 void
944 build_recover_thunks();
945
946 // Simplify statements which might use thunks: go and defer
947 // statements.
948 void
949 simplify_thunk_statements();
950
951 // Dump AST if -fgo-dump-ast is set.
952 void
953 dump_ast(const char* basename);
954
955 // Dump Call Graph if -fgo-dump-calls is set.
956 void
957 dump_call_graph(const char* basename);
958
959 // Dump Connection Graphs if -fgo-dump-connections is set.
960 void
961 dump_connection_graphs(const char* basename);
962
963 // Convert named types to the backend representation.
964 void
965 convert_named_types();
966
967 // Convert named types in a list of bindings.
968 void
969 convert_named_types_in_bindings(Bindings*);
970
971 // True if named types have been converted to the backend
972 // representation.
973 bool
974 named_types_are_converted() const
975 { return this->named_types_are_converted_; }
976
977 // Give an error if the initialization of VAR depends on itself.
978 void
979 check_self_dep(Named_object*);
980
981 // Write out the global values.
982 void
983 write_globals();
984
985 // Build required interface method tables.
986 void
987 build_interface_method_tables();
988
989 // Return an expression which allocates memory to hold values of type TYPE.
990 Expression*
991 allocate_memory(Type *type, Location);
992
993 // Get the backend name to use for an exported function, a method,
994 // or a function/method declaration.
995 void
996 function_backend_name(const std::string& go_name, const Package*,
997 const Type* receiver, Backend_name*);
998
999 // Return the name to use for a function descriptor.
1000 void
1001 function_descriptor_backend_name(Named_object*, Backend_name*);
1002
1003 // Return the name to use for a generated stub method.
1004 std::string
1005 stub_method_name(const Package*, const std::string& method_name);
1006
1007 // Get the backend name of the hash function for TYPE.
1008 void
1009 hash_function_name(const Type*, Backend_name*);
1010
1011 // Get the backend name of the equal function for TYPE.
1012 void
1013 equal_function_name(const Type*, const Named_type*, Backend_name*);
1014
1015 // Get the backend name to use for a global variable.
1016 void
1017 global_var_backend_name(const std::string& go_name, const Package*,
1018 Backend_name*);
1019
1020 // Return a name to use for an error case. This should only be used
1021 // after reporting an error, and is used to avoid useless knockon
1022 // errors.
1023 static std::string
1024 erroneous_name();
1025
1026 // Return whether the name indicates an error.
1027 static bool
1028 is_erroneous_name(const std::string&);
1029
1030 // Return a name to use for a thunk function. A thunk function is
1031 // one we create during the compilation, for a go statement or a
1032 // defer statement or a method expression.
1033 std::string
1034 thunk_name();
1035
1036 // Return whether an object is a thunk.
1037 static bool
1038 is_thunk(const Named_object*);
1039
1040 // Return the name to use for an init function.
1041 std::string
1042 init_function_name();
1043
1044 // Return the name to use for a nested function.
1045 std::string
1046 nested_function_name(Named_object* enclosing);
1047
1048 // Return the name to use for a sink funciton.
1049 std::string
1050 sink_function_name();
1051
1052 // Return the name to use for an (erroneous) redefined function.
1053 std::string
1054 redefined_function_name();
1055
1056 // Return the name for use for a recover thunk.
1057 std::string
1058 recover_thunk_name(const std::string& name, const Type* rtype);
1059
1060 // Return the name to use for the GC root variable.
1061 std::string
1062 gc_root_name();
1063
1064 // Return the name to use for a composite literal or string
1065 // initializer.
1066 std::string
1067 initializer_name();
1068
1069 // Return the name of the variable used to represent the zero value
1070 // of a map.
1071 std::string
1072 map_zero_value_name();
1073
1074 // Get the name of the magic initialization function.
1075 const std::string&
1076 get_init_fn_name();
1077
1078 // Return the name for a dummy init function, which is not a real
1079 // function but only for tracking transitive import.
1080 std::string
1081 dummy_init_fn_name();
1082
1083 // Return the package path symbol from an init function name, which
1084 // can be a real init function or a dummy one.
1085 std::string
1086 pkgpath_symbol_from_init_fn_name(std::string);
1087
1088 // Get the backend name for a type descriptor symbol.
1089 void
1090 type_descriptor_backend_name(const Type*, Named_type*, Backend_name*);
1091
1092 // Return the name of the type descriptor list symbol of a package.
1093 // The argument is an encoded pkgpath, as with pkgpath_symbol.
1094 std::string
1095 type_descriptor_list_symbol(const std::string&);
1096
1097 // Return the name of the list of all type descriptor lists.
1098 std::string
1099 typelists_symbol();
1100
1101 // Return the assembler name for the GC symbol for a type.
1102 std::string
1103 gc_symbol_name(Type*);
1104
1105 // Return the assembler name for a ptrmask variable.
1106 std::string
1107 ptrmask_symbol_name(const std::string& ptrmask_sym_name);
1108
1109 // Return the name to use for an interface method table.
1110 std::string
1111 interface_method_table_name(Interface_type*, Type*, bool is_pointer);
1112
1113 // If NAME is a special name used as a Go identifier, return the
1114 // position within the string where the special part of the name
1115 // occurs.
1116 static size_t
1117 special_name_pos(const std::string& name);
1118
1119 private:
1120 // During parsing, we keep a stack of functions. Each function on
1121 // the stack is one that we are currently parsing. For each
1122 // function, we keep track of the current stack of blocks.
1123 struct Open_function
1124 {
1125 // The function.
1126 Named_object* function;
1127 // The stack of active blocks in the function.
1128 std::vector<Block*> blocks;
1129 };
1130
1131 // The stack of functions.
1132 typedef std::vector<Open_function> Open_functions;
1133
1134 // Set up the built-in unsafe package.
1135 void
1136 import_unsafe(const std::string&, bool is_exported, Location);
1137
1138 // Return the current binding contour.
1139 Bindings*
1140 current_bindings();
1141
1142 const Bindings*
1143 current_bindings() const;
1144
1145 void
1146 write_c_header();
1147
1148 // Get the decl for the magic initialization function.
1149 Named_object*
1150 initialization_function_decl();
1151
1152 // Create the magic initialization function.
1153 Named_object*
1154 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
1155
1156 // Initialize imported packages. BFUNCTION is the function
1157 // into which the package init calls will be placed.
1158 void
1159 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
1160
1161 // Register variables with the garbage collector.
1162 void
1163 register_gc_vars(const std::vector<Named_object*>&,
1164 std::vector<Bstatement*>&,
1165 Bfunction* init_bfunction);
1166
1167 // Build the list of type descriptors.
1168 void
1169 build_type_descriptor_list();
1170
1171 // Register the type descriptors with the runtime.
1172 void
1173 register_type_descriptors(std::vector<Bstatement*>&,
1174 Bfunction* init_bfunction);
1175
1176 void
1177 propagate_writebarrierrec();
1178
1179 Named_object*
1180 write_barrier_variable();
1181
1182 static bool
1183 is_digits(const std::string&);
1184
1185 // Type used to map go:embed patterns to a list of files.
1186 typedef Unordered_map(std::string, std::vector<std::string>) Embed_patterns;
1187
1188 // Type used to map go:embed file names to their full path.
1189 typedef Unordered_map(std::string, std::string) Embed_files;
1190
1191 // Type used to map import names to packages.
1192 typedef std::map<std::string, Package*> Imports;
1193
1194 // Type used to map package names to packages.
1195 typedef std::map<std::string, Package*> Packages;
1196
1197 // Type used to map variables to the function calls that set them.
1198 // This is used for initialization dependency analysis.
1199 typedef std::map<Variable*, Named_object*> Var_deps;
1200
1201 // Type used to map identifiers in the file block to the location
1202 // where they were defined.
1203 typedef Unordered_map(std::string, Location) File_block_names;
1204
1205 // Type used to queue writing a type specific function.
1206 struct Specific_type_function
1207 {
1208 enum Specific_type_function_kind { SPECIFIC_HASH, SPECIFIC_EQUAL };
1209
1210 Type* type;
1211 Named_type* name;
1212 int64_t size;
1213 Specific_type_function_kind kind;
1214 Backend_name bname;
1215 Function_type* fntype;
1216
1217 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
1218 Specific_type_function_kind akind,
1219 Backend_name* abname,
1220 Function_type* afntype)
1221 : type(atype), name(aname), size(asize), kind(akind),
1222 bname(*abname), fntype(afntype)
1223 { }
1224 };
1225
1226 // Recompute init priorities.
1227 void
1228 recompute_init_priorities();
1229
1230 // Recursive helper used by the routine above.
1231 void
1232 update_init_priority(Import_init* ii,
1233 std::set<const Import_init *>* visited);
1234
1235 // The backend generator.
1236 Backend* backend_;
1237 // The object used to keep track of file names and line numbers.
1238 Linemap* linemap_;
1239 // The package we are compiling.
1240 Package* package_;
1241 // The list of currently open functions during parsing.
1242 Open_functions functions_;
1243 // The global binding contour. This includes the builtin functions
1244 // and the package we are compiling.
1245 Bindings* globals_;
1246 // The list of names we have seen in the file block.
1247 File_block_names file_block_names_;
1248 // Mapping from import file names to packages.
1249 Imports imports_;
1250 // Whether the magic unsafe package was imported.
1251 bool imported_unsafe_;
1252 // Whether the magic unsafe package was imported by the current file.
1253 bool current_file_imported_unsafe_;
1254 // Mapping from package names we have seen to packages. This does
1255 // not include the package we are compiling.
1256 Packages packages_;
1257 // The functions named "init", if there are any.
1258 std::vector<Named_object*> init_functions_;
1259 // A mapping from variables to the function calls that initialize
1260 // them, if it is not stored in the variable's init or preinit.
1261 // This is used for dependency analysis.
1262 Var_deps var_deps_;
1263 // Whether we need a magic initialization function.
1264 bool need_init_fn_;
1265 // The name of the magic initialization function.
1266 std::string init_fn_name_;
1267 // A list of import control variables for packages that we import.
1268 Import_init_set imported_init_fns_;
1269 // The package path used for reflection data.
1270 std::string pkgpath_;
1271 // The package path to use for a symbol name.
1272 std::string pkgpath_symbol_;
1273 // The prefix to use for symbols, from the -fgo-prefix option.
1274 std::string prefix_;
1275 // Whether pkgpath_ has been set.
1276 bool pkgpath_set_;
1277 // Whether an explicit package path was set by -fgo-pkgpath.
1278 bool pkgpath_from_option_;
1279 // Whether an explicit prefix was set by -fgo-prefix.
1280 bool prefix_from_option_;
1281 // The relative import path, from the -fgo-relative-import-path
1282 // option.
1283 std::string relative_import_path_;
1284 // The C header file to write, from the -fgo-c-header option.
1285 std::string c_header_;
1286 // Patterns from an embedcfg file.
1287 Embed_patterns embed_patterns_;
1288 // Mapping from file to full path from an embedcfg file.
1289 Embed_files embed_files_;
1290 // Whether or not to check for division by zero, from the
1291 // -fgo-check-divide-zero option.
1292 bool check_divide_by_zero_;
1293 // Whether or not to check for division overflow, from the
1294 // -fgo-check-divide-overflow option.
1295 bool check_divide_overflow_;
1296 // Whether we are compiling the runtime package, from the
1297 // -fgo-compiling-runtime option.
1298 bool compiling_runtime_;
1299 // The level of escape analysis debug information to emit, from the
1300 // -fgo-debug-escape option.
1301 int debug_escape_level_;
1302 // A hash value for debug escape analysis, from the
1303 // -fgo-debug-escape-hash option. The analysis is run only on
1304 // functions with names that hash to the matching value.
1305 std::string debug_escape_hash_;
1306 // Whether to output optimization diagnostics, from the
1307 // -fgo-debug-optimization option.
1308 bool debug_optimization_;
1309 // Nil-check size threshhold.
1310 int64_t nil_check_size_threshold_;
1311 // Whether runtime.eqtype calls are needed when comparing type
1312 // descriptors.
1313 bool need_eqtype_;
1314 // A list of types to verify.
1315 std::vector<Type*> verify_types_;
1316 // A list of interface types defined while parsing.
1317 std::vector<Interface_type*> interface_types_;
1318 // Type specific functions to write out.
1319 std::vector<Specific_type_function*> specific_type_functions_;
1320 // Whether we are done writing out specific type functions.
1321 bool specific_type_functions_are_written_;
1322 // Whether named types have been converted.
1323 bool named_types_are_converted_;
1324 // A list containing groups of possibly mutually recursive functions to be
1325 // considered during escape analysis.
1326 std::vector<Analysis_set> analysis_sets_;
1327 // A list of objects to add to the GC roots.
1328 std::vector<Expression*> gc_roots_;
1329 // A list of type descriptors that we need to register.
1330 std::vector<Type*> type_descriptors_;
1331 // A list of function declarations with imported bodies that we may
1332 // want to inline.
1333 std::vector<Named_object*> imported_inlinable_functions_;
1334 // A list of functions that we want to inline. These will be sent
1335 // to the backend.
1336 std::vector<Named_object*> imported_inline_functions_;
1337 };
1338
1339 // A block of statements.
1340
1341 class Block
1342 {
1343 public:
1344 Block(Block* enclosing, Location);
1345
1346 // Return the enclosing block.
1347 const Block*
1348 enclosing() const
1349 { return this->enclosing_; }
1350
1351 // Return the bindings of the block.
1352 Bindings*
1353 bindings()
1354 { return this->bindings_; }
1355
1356 const Bindings*
1357 bindings() const
1358 { return this->bindings_; }
1359
1360 // Look at the block's statements.
1361 const std::vector<Statement*>*
1362 statements() const
1363 { return &this->statements_; }
1364
1365 // Return the start location. This is normally the location of the
1366 // left curly brace which starts the block.
1367 Location
1368 start_location() const
1369 { return this->start_location_; }
1370
1371 // Return the end location. This is normally the location of the
1372 // right curly brace which ends the block.
1373 Location
1374 end_location() const
1375 { return this->end_location_; }
1376
1377 // Add a statement to the block.
1378 void
1379 add_statement(Statement*);
1380
1381 // Add a statement to the front of the block.
1382 void
1383 add_statement_at_front(Statement*);
1384
1385 // Replace a statement in a block.
1386 void
1387 replace_statement(size_t index, Statement*);
1388
1389 // Add a Statement before statement number INDEX.
1390 void
1391 insert_statement_before(size_t index, Statement*);
1392
1393 // Add a Statement after statement number INDEX.
1394 void
1395 insert_statement_after(size_t index, Statement*);
1396
1397 // Set the end location of the block.
1398 void
1399 set_end_location(Location location)
1400 { this->end_location_ = location; }
1401
1402 // Traverse the tree.
1403 int
1404 traverse(Traverse*);
1405
1406 // Set final types for unspecified variables and constants.
1407 void
1408 determine_types();
1409
1410 // Return true if execution of this block may fall through to the
1411 // next block.
1412 bool
1413 may_fall_through() const;
1414
1415 // Write the export data for the block's statements to the string.
1416 void
1417 export_block(Export_function_body*);
1418
1419 // Turn exported block data into a block.
1420 static bool
1421 import_block(Block*, Import_function_body*, Location);
1422
1423 // Convert the block to the backend representation.
1424 Bblock*
1425 get_backend(Translate_context*);
1426
1427 // Iterate over statements.
1428
1429 typedef std::vector<Statement*>::iterator iterator;
1430
1431 iterator
1432 begin()
1433 { return this->statements_.begin(); }
1434
1435 iterator
1436 end()
1437 { return this->statements_.end(); }
1438
1439 private:
1440 // Enclosing block.
1441 Block* enclosing_;
1442 // Statements in the block.
1443 std::vector<Statement*> statements_;
1444 // Binding contour.
1445 Bindings* bindings_;
1446 // Location of start of block.
1447 Location start_location_;
1448 // Location of end of block.
1449 Location end_location_;
1450 };
1451
1452 // A function.
1453
1454 class Function
1455 {
1456 public:
1457 Function(Function_type* type, Named_object*, Block*, Location);
1458
1459 // Return the function's type.
1460 Function_type*
1461 type() const
1462 { return this->type_; }
1463
1464 // Return the enclosing function if there is one.
1465 Named_object*
1466 enclosing() const
1467 { return this->enclosing_; }
1468
1469 // Set the enclosing function. This is used when building thunks
1470 // for functions which call recover.
1471 void
1472 set_enclosing(Named_object* enclosing)
1473 {
1474 go_assert(this->enclosing_ == NULL);
1475 this->enclosing_ = enclosing;
1476 }
1477
1478 // The result variables.
1479 typedef std::vector<Named_object*> Results;
1480
1481 // Create the result variables in the outer block.
1482 void
1483 create_result_variables(Gogo*);
1484
1485 // Update the named result variables when cloning a function which
1486 // calls recover.
1487 void
1488 update_result_variables();
1489
1490 // Return the result variables.
1491 Results*
1492 result_variables()
1493 { return this->results_; }
1494
1495 bool
1496 is_sink() const
1497 { return this->is_sink_; }
1498
1499 void
1500 set_is_sink()
1501 { this->is_sink_ = true; }
1502
1503 // Whether the result variables have names.
1504 bool
1505 results_are_named() const
1506 { return this->results_are_named_; }
1507
1508 // Return the assembler name.
1509 const std::string&
1510 asm_name() const
1511 { return this->asm_name_; }
1512
1513 // Set the assembler name.
1514 void
1515 set_asm_name(const std::string& asm_name)
1516 { this->asm_name_ = asm_name; }
1517
1518 // Mark this symbol as exported by a linkname directive.
1519 void
1520 set_is_exported_by_linkname()
1521 { this->is_exported_by_linkname_ = true; }
1522
1523 // Return the pragmas for this function.
1524 unsigned int
1525 pragmas() const
1526 { return this->pragmas_; }
1527
1528 // Set the pragmas for this function.
1529 void
1530 set_pragmas(unsigned int pragmas)
1531 {
1532 this->pragmas_ = pragmas;
1533 }
1534
1535 // Return the index to use for a nested function.
1536 unsigned int
1537 next_nested_function_index()
1538 {
1539 ++this->nested_functions_;
1540 return this->nested_functions_;
1541 }
1542
1543 // Whether this method should not be included in the type
1544 // descriptor.
1545 bool
1546 nointerface() const;
1547
1548 // Record that this method should not be included in the type
1549 // descriptor.
1550 void
1551 set_nointerface();
1552
1553 // Record that this function is a stub method created for an unnamed
1554 // type.
1555 void
1556 set_is_unnamed_type_stub_method()
1557 {
1558 go_assert(this->is_method());
1559 this->is_unnamed_type_stub_method_ = true;
1560 }
1561
1562 // Return the amount of enclosed variables in this closure.
1563 size_t
1564 closure_field_count() const
1565 { return this->closure_fields_.size(); }
1566
1567 // Add a new field to the closure variable.
1568 void
1569 add_closure_field(Named_object* var, Location loc)
1570 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1571
1572 // Whether this function needs a closure.
1573 bool
1574 needs_closure() const
1575 { return !this->closure_fields_.empty(); }
1576
1577 // Return the closure variable, creating it if necessary. This is
1578 // passed to the function as a static chain parameter.
1579 Named_object*
1580 closure_var();
1581
1582 // Set the closure variable. This is used when building thunks for
1583 // functions which call recover.
1584 void
1585 set_closure_var(Named_object* v)
1586 {
1587 go_assert(this->closure_var_ == NULL);
1588 this->closure_var_ = v;
1589 }
1590
1591 // Return the variable for a reference to field INDEX in the closure
1592 // variable.
1593 Named_object*
1594 enclosing_var(unsigned int index)
1595 {
1596 go_assert(index < this->closure_fields_.size());
1597 return closure_fields_[index].first;
1598 }
1599
1600 // Set the type of the closure variable if there is one.
1601 void
1602 set_closure_type();
1603
1604 // Get the block of statements associated with the function.
1605 Block*
1606 block() const
1607 { return this->block_; }
1608
1609 // Get the location of the start of the function.
1610 Location
1611 location() const
1612 { return this->location_; }
1613
1614 // Return whether this function is actually a method.
1615 bool
1616 is_method() const;
1617
1618 // Add a label definition to the function.
1619 Label*
1620 add_label_definition(Gogo*, const std::string& label_name, Location);
1621
1622 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1623 // if we should report errors for a goto from the current location
1624 // to the label location.
1625 Label*
1626 add_label_reference(Gogo*, const std::string& label_name,
1627 Location, bool issue_goto_errors);
1628
1629 // Warn about labels that are defined but not used.
1630 void
1631 check_labels() const;
1632
1633 // Note that a new local type has been added. Return its index.
1634 unsigned int
1635 new_local_type_index()
1636 { return this->local_type_count_++; }
1637
1638 // Whether this function calls the predeclared recover function.
1639 bool
1640 calls_recover() const
1641 { return this->calls_recover_; }
1642
1643 // Record that this function calls the predeclared recover function.
1644 // This is set during the lowering pass.
1645 void
1646 set_calls_recover()
1647 { this->calls_recover_ = true; }
1648
1649 // Whether this is a recover thunk function.
1650 bool
1651 is_recover_thunk() const
1652 { return this->is_recover_thunk_; }
1653
1654 // Record that this is a thunk built for a function which calls
1655 // recover.
1656 void
1657 set_is_recover_thunk()
1658 { this->is_recover_thunk_ = true; }
1659
1660 // Whether this function already has a recover thunk.
1661 bool
1662 has_recover_thunk() const
1663 { return this->has_recover_thunk_; }
1664
1665 // Record that this function already has a recover thunk.
1666 void
1667 set_has_recover_thunk()
1668 { this->has_recover_thunk_ = true; }
1669
1670 // Record that this function is a thunk created for a defer
1671 // statement that calls the __go_set_defer_retaddr runtime function.
1672 void
1673 set_calls_defer_retaddr()
1674 { this->calls_defer_retaddr_ = true; }
1675
1676 // Whether this is a type hash or equality function created by the
1677 // compiler.
1678 bool
1679 is_type_specific_function()
1680 { return this->is_type_specific_function_; }
1681
1682 // Record that this function is a type hash or equality function
1683 // created by the compiler.
1684 void
1685 set_is_type_specific_function()
1686 { this->is_type_specific_function_ = true; }
1687
1688 // Mark the function as going into a unique section.
1689 void
1690 set_in_unique_section()
1691 { this->in_unique_section_ = true; }
1692
1693 // Return whether this function should be exported for inlining.
1694 bool
1695 export_for_inlining() const
1696 { return this->export_for_inlining_; }
1697
1698 // Mark the function to be exported for inlining.
1699 void
1700 set_export_for_inlining()
1701 { this->export_for_inlining_ = true; }
1702
1703 // Return whether this function is inline only.
1704 bool
1705 is_inline_only() const
1706 { return this->is_inline_only_; }
1707
1708 // Mark the function as inline only: the body should not be emitted
1709 // if it is not inlined.
1710 void
1711 set_is_inline_only()
1712 { this->is_inline_only_ = true; }
1713
1714 // Report whether the function is referenced by an inline body.
1715 bool
1716 is_referenced_by_inline() const
1717 { return this->is_referenced_by_inline_; }
1718
1719 // Mark the function as referenced by an inline body.
1720 void
1721 set_is_referenced_by_inline()
1722 { this->is_referenced_by_inline_ = true; }
1723
1724 // Swap with another function. Used only for the thunk which calls
1725 // recover.
1726 void
1727 swap_for_recover(Function *);
1728
1729 // Traverse the tree.
1730 int
1731 traverse(Traverse*);
1732
1733 // Determine types in the function.
1734 void
1735 determine_types();
1736
1737 // Return an expression for the function descriptor, given the named
1738 // object for this function. This may only be called for functions
1739 // without a closure. This will be an immutable struct with one
1740 // field that points to the function's code.
1741 Expression*
1742 descriptor(Gogo*, Named_object*);
1743
1744 // Set the descriptor for this function. This is used when a
1745 // function declaration is followed by a function definition.
1746 void
1747 set_descriptor(Expression* descriptor)
1748 {
1749 go_assert(this->descriptor_ == NULL);
1750 this->descriptor_ = descriptor;
1751 }
1752
1753 // Return the backend representation.
1754 Bfunction*
1755 get_or_make_decl(Gogo*, Named_object*);
1756
1757 // Return the function's decl after it has been built.
1758 Bfunction*
1759 get_decl() const;
1760
1761 // Set the function decl to hold a backend representation of the function
1762 // code.
1763 void
1764 build(Gogo*, Named_object*);
1765
1766 // Get the statement that assigns values to this function's result struct.
1767 Bstatement*
1768 return_value(Gogo*, Named_object*, Location) const;
1769
1770 // Get the backend name of this function.
1771 void
1772 backend_name(Gogo*, Named_object*, Backend_name*);
1773
1774 // Get an expression for the variable holding the defer stack.
1775 Expression*
1776 defer_stack(Location);
1777
1778 // Export the function.
1779 void
1780 export_func(Export*, const Named_object*) const;
1781
1782 // Export a function with a type.
1783 static void
1784 export_func_with_type(Export*, const Named_object*,
1785 const Function_type*, Results*, bool nointerface,
1786 const std::string& asm_name, Block* block, Location);
1787
1788 // Import a function. Reports whether the import succeeded.
1789 static bool
1790 import_func(Import*, std::string* pname, Package** pkg,
1791 bool* is_exported, Typed_identifier** receiver,
1792 Typed_identifier_list** pparameters,
1793 Typed_identifier_list** presults, bool* is_varargs,
1794 bool* nointerface, std::string* asm_name, std::string* body);
1795
1796 private:
1797 // Type for mapping from label names to Label objects.
1798 typedef Unordered_map(std::string, Label*) Labels;
1799
1800 void
1801 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1802
1803 typedef std::vector<std::pair<Named_object*,
1804 Location> > Closure_fields;
1805
1806 // The function's type.
1807 Function_type* type_;
1808 // The enclosing function. This is NULL when there isn't one, which
1809 // is the normal case.
1810 Named_object* enclosing_;
1811 // The result variables, if any.
1812 Results* results_;
1813 // If there is a closure, this is the list of variables which appear
1814 // in the closure. This is created by the parser, and then resolved
1815 // to a real type when we lower parse trees.
1816 Closure_fields closure_fields_;
1817 // The closure variable, passed as a parameter using the static
1818 // chain parameter. Normally NULL.
1819 Named_object* closure_var_;
1820 // The outer block of statements in the function.
1821 Block* block_;
1822 // The source location of the start of the function.
1823 Location location_;
1824 // Labels defined or referenced in the function.
1825 Labels labels_;
1826 // The number of local types defined in this function.
1827 unsigned int local_type_count_;
1828 // The assembler name: this is the name that will be put in the object file.
1829 // Set by the go:linkname compiler directive. This is normally empty.
1830 std::string asm_name_;
1831 // The function descriptor, if any.
1832 Expression* descriptor_;
1833 // The function decl.
1834 Bfunction* fndecl_;
1835 // The defer stack variable. A pointer to this variable is used to
1836 // distinguish the defer stack for one function from another. This
1837 // is NULL unless we actually need a defer stack.
1838 Temporary_statement* defer_stack_;
1839 // Pragmas for this function. This is a set of GOPRAGMA bits.
1840 unsigned int pragmas_;
1841 // Number of nested functions defined within this function.
1842 unsigned int nested_functions_;
1843 // True if this function is sink-named. No code is generated.
1844 bool is_sink_ : 1;
1845 // True if the result variables are named.
1846 bool results_are_named_ : 1;
1847 // True if this function is a stub method created for an unnamed
1848 // type.
1849 bool is_unnamed_type_stub_method_ : 1;
1850 // True if this function calls the predeclared recover function.
1851 bool calls_recover_ : 1;
1852 // True if this a thunk built for a function which calls recover.
1853 bool is_recover_thunk_ : 1;
1854 // True if this function already has a recover thunk.
1855 bool has_recover_thunk_ : 1;
1856 // True if this is a thunk built for a defer statement that calls
1857 // the __go_set_defer_retaddr runtime function.
1858 bool calls_defer_retaddr_ : 1;
1859 // True if this is a function built by the compiler to as a hash or
1860 // equality function for some type.
1861 bool is_type_specific_function_ : 1;
1862 // True if this function should be put in a unique section. This is
1863 // turned on for field tracking.
1864 bool in_unique_section_ : 1;
1865 // True if we should export the body of this function for
1866 // cross-package inlining.
1867 bool export_for_inlining_ : 1;
1868 // True if this function is inline only: if it should not be emitted
1869 // if it is not inlined.
1870 bool is_inline_only_ : 1;
1871 // True if this function is referenced from an inlined body that
1872 // will be put into the export data.
1873 bool is_referenced_by_inline_ : 1;
1874 // True if we should make this function visible to other packages
1875 // because of a go:linkname directive.
1876 bool is_exported_by_linkname_ : 1;
1877 };
1878
1879 // A snapshot of the current binding state.
1880
1881 class Bindings_snapshot
1882 {
1883 public:
1884 Bindings_snapshot(const Block*, Location);
1885
1886 // Report any errors appropriate for a goto from the current binding
1887 // state of B to this one.
1888 void
1889 check_goto_from(const Block* b, Location);
1890
1891 // Report any errors appropriate for a goto from this binding state
1892 // to the current state of B.
1893 void
1894 check_goto_to(const Block* b);
1895
1896 private:
1897 bool
1898 check_goto_block(Location, const Block*, const Block*, size_t*);
1899
1900 void
1901 check_goto_defs(Location, const Block*, size_t, size_t);
1902
1903 // The current block.
1904 const Block* block_;
1905 // The number of names currently defined in each open block.
1906 // Element 0 is this->block_, element 1 is
1907 // this->block_->enclosing(), etc.
1908 std::vector<size_t> counts_;
1909 // The location where this snapshot was taken.
1910 Location location_;
1911 };
1912
1913 // A function declaration.
1914
1915 class Function_declaration
1916 {
1917 public:
1918 Function_declaration(Function_type* fntype, Location location)
1919 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1920 fndecl_(NULL), pragmas_(0), imported_body_(),
1921 is_on_inlinable_list_(false)
1922 { }
1923
1924 Function_type*
1925 type() const
1926 { return this->fntype_; }
1927
1928 Location
1929 location() const
1930 { return this->location_; }
1931
1932 // Return whether this function declaration is a method.
1933 bool
1934 is_method() const;
1935
1936 const std::string&
1937 asm_name() const
1938 { return this->asm_name_; }
1939
1940 // Set the assembler name.
1941 void
1942 set_asm_name(const std::string& asm_name)
1943 { this->asm_name_ = asm_name; }
1944
1945 // Return the pragmas for this function.
1946 unsigned int
1947 pragmas() const
1948 { return this->pragmas_; }
1949
1950 // Set the pragmas for this function.
1951 void
1952 set_pragmas(unsigned int pragmas)
1953 {
1954 this->pragmas_ = pragmas;
1955 }
1956
1957 // Whether this method should not be included in the type
1958 // descriptor.
1959 bool
1960 nointerface() const;
1961
1962 // Record that this method should not be included in the type
1963 // descriptor.
1964 void
1965 set_nointerface();
1966
1967 // Whether we have an imported function body.
1968 bool
1969 has_imported_body() const
1970 { return !this->imported_body_.empty(); }
1971
1972 // Record the imported body of this function.
1973 void
1974 set_imported_body(Import* imp, const std::string& imported_body)
1975 {
1976 this->imp_ = imp;
1977 this->imported_body_ = imported_body;
1978 }
1979
1980 // Whether this declaration is on the list of inlinable functions.
1981 bool
1982 is_on_inlinable_list() const
1983 { return this->is_on_inlinable_list_; }
1984
1985 // Set that this function is on the list of inlinable functions.
1986 void
1987 set_is_on_inlinable_list()
1988 { this->is_on_inlinable_list_ = true; }
1989
1990 // Import the function body, creating a function.
1991 void
1992 import_function_body(Gogo*, Named_object*);
1993
1994 // Return an expression for the function descriptor, given the named
1995 // object for this function. This may only be called for functions
1996 // without a closure. This will be an immutable struct with one
1997 // field that points to the function's code.
1998 Expression*
1999 descriptor(Gogo*, Named_object*);
2000
2001 // Return true if we have created a descriptor for this declaration.
2002 bool
2003 has_descriptor() const
2004 { return this->descriptor_ != NULL; }
2005
2006 // Return a backend representation.
2007 Bfunction*
2008 get_or_make_decl(Gogo*, Named_object*);
2009
2010 // If there is a descriptor, build it into the backend
2011 // representation.
2012 void
2013 build_backend_descriptor(Gogo*);
2014
2015 // Get the backend name of this function declaration.
2016 void
2017 backend_name(Gogo*, Named_object*, Backend_name*);
2018
2019 // Export a function declaration.
2020 void
2021 export_func(Export* exp, const Named_object* no) const
2022 {
2023 Function::export_func_with_type(exp, no, this->fntype_, NULL,
2024 this->is_method() && this->nointerface(),
2025 this->asm_name_, NULL, this->location_);
2026 }
2027
2028 // Check that the types used in this declaration's signature are defined.
2029 void
2030 check_types() const;
2031
2032 private:
2033 // The type of the function.
2034 Function_type* fntype_;
2035 // The location of the declaration.
2036 Location location_;
2037 // The assembler name: this is the name to use in references to the
2038 // function. This is normally empty.
2039 std::string asm_name_;
2040 // The function descriptor, if any.
2041 Expression* descriptor_;
2042 // The function decl if needed.
2043 Bfunction* fndecl_;
2044 // Pragmas for this function. This is a set of GOPRAGMA bits.
2045 unsigned int pragmas_;
2046 // Importer for function body if imported from a different package.
2047 Import* imp_;
2048 // Export data for function body if imported from a different package.
2049 std::string imported_body_;
2050 // Whether this declaration is already on the list of inlinable functions.
2051 bool is_on_inlinable_list_;
2052 };
2053
2054 // A variable.
2055
2056 class Variable
2057 {
2058 public:
2059 Variable(Type*, Expression*, bool is_global, bool is_parameter,
2060 bool is_receiver, Location);
2061
2062 // Get the type of the variable.
2063 Type*
2064 type();
2065
2066 Type*
2067 type() const;
2068
2069 // Return whether the type is defined yet.
2070 bool
2071 has_type() const;
2072
2073 // Get the initial value.
2074 Expression*
2075 init() const
2076 { return this->init_; }
2077
2078 // Return whether there are any preinit statements.
2079 bool
2080 has_pre_init() const
2081 { return this->preinit_ != NULL; }
2082
2083 // Return the preinit statements if any.
2084 Block*
2085 preinit() const
2086 { return this->preinit_; }
2087
2088 // Return whether this is a global variable.
2089 bool
2090 is_global() const
2091 { return this->is_global_; }
2092
2093 // Return whether this is a function parameter.
2094 bool
2095 is_parameter() const
2096 { return this->is_parameter_; }
2097
2098 // Return whether this is a closure (static chain) parameter.
2099 bool
2100 is_closure() const
2101 { return this->is_closure_; }
2102
2103 // Change this parameter to be a closure.
2104 void
2105 set_is_closure()
2106 {
2107 this->is_closure_ = true;
2108 }
2109
2110 // Return whether this is the receiver parameter of a method.
2111 bool
2112 is_receiver() const
2113 { return this->is_receiver_; }
2114
2115 // Change this parameter to be a receiver. This is used when
2116 // creating the thunks created for functions which call recover.
2117 void
2118 set_is_receiver()
2119 {
2120 go_assert(this->is_parameter_);
2121 this->is_receiver_ = true;
2122 }
2123
2124 // Change this parameter to not be a receiver. This is used when
2125 // creating the thunks created for functions which call recover.
2126 void
2127 set_is_not_receiver()
2128 {
2129 go_assert(this->is_parameter_);
2130 this->is_receiver_ = false;
2131 }
2132
2133 // Return whether this is the varargs parameter of a function.
2134 bool
2135 is_varargs_parameter() const
2136 { return this->is_varargs_parameter_; }
2137
2138 // Return whether this is a global sink variable, created only to
2139 // run an initializer.
2140 bool
2141 is_global_sink() const
2142 { return this->is_global_sink_; }
2143
2144 // Record that this is a global sink variable.
2145 void
2146 set_is_global_sink()
2147 {
2148 go_assert(this->is_global_);
2149 this->is_global_sink_ = true;
2150 }
2151
2152 // Whether this variable's address is taken.
2153 bool
2154 is_address_taken() const
2155 { return this->is_address_taken_; }
2156
2157 // Whether this variable should live in the heap.
2158 bool
2159 is_in_heap() const
2160 { return this->is_address_taken_ && !this->is_global_; }
2161
2162 // Note that something takes the address of this variable.
2163 void
2164 set_address_taken()
2165 { this->is_address_taken_ = true; }
2166
2167 // Return whether the address is taken but does not escape.
2168 bool
2169 is_non_escaping_address_taken() const
2170 { return this->is_non_escaping_address_taken_; }
2171
2172 // Note that something takes the address of this variable such that
2173 // the address does not escape the function.
2174 void
2175 set_non_escaping_address_taken()
2176 { this->is_non_escaping_address_taken_ = true; }
2177
2178 // Get the source location of the variable's declaration.
2179 Location
2180 location() const
2181 { return this->location_; }
2182
2183 // Record that this is the varargs parameter of a function.
2184 void
2185 set_is_varargs_parameter()
2186 {
2187 go_assert(this->is_parameter_);
2188 this->is_varargs_parameter_ = true;
2189 }
2190
2191 // Return whether the variable has been used.
2192 bool
2193 is_used() const
2194 { return this->is_used_; }
2195
2196 // Mark that the variable has been used.
2197 void
2198 set_is_used()
2199 { this->is_used_ = true; }
2200
2201 // Clear the initial value; used for error handling and write barriers.
2202 void
2203 clear_init()
2204 { this->init_ = NULL; }
2205
2206 // Set the initial value; used for converting shortcuts.
2207 void
2208 set_init(Expression* init)
2209 { this->init_ = init; }
2210
2211 // Get the preinit block, a block of statements to be run before the
2212 // initialization expression.
2213 Block*
2214 preinit_block(Gogo*);
2215
2216 // Add a statement to be run before the initialization expression.
2217 // This is only used for global variables.
2218 void
2219 add_preinit_statement(Gogo*, Statement*);
2220
2221 // Lower the initialization expression after parsing is complete.
2222 void
2223 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
2224
2225 // Flatten the initialization expression after ordering evaluations.
2226 void
2227 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
2228
2229 // A special case: the init value is used only to determine the
2230 // type. This is used if the variable is defined using := with the
2231 // comma-ok form of a map index or a receive expression. The init
2232 // value is actually the map index expression or receive expression.
2233 // We use this because we may not know the right type at parse time.
2234 void
2235 set_type_from_init_tuple()
2236 { this->type_from_init_tuple_ = true; }
2237
2238 // Another special case: the init value is used only to determine
2239 // the type. This is used if the variable is defined using := with
2240 // a range clause. The init value is the range expression. The
2241 // type of the variable is the index type of the range expression
2242 // (i.e., the first value returned by a range).
2243 void
2244 set_type_from_range_index()
2245 { this->type_from_range_index_ = true; }
2246
2247 // Another special case: like set_type_from_range_index, but the
2248 // type is the value type of the range expression (i.e., the second
2249 // value returned by a range).
2250 void
2251 set_type_from_range_value()
2252 { this->type_from_range_value_ = true; }
2253
2254 // Another special case: the init value is used only to determine
2255 // the type. This is used if the variable is defined using := with
2256 // a case in a select statement. The init value is the channel.
2257 // The type of the variable is the channel's element type.
2258 void
2259 set_type_from_chan_element()
2260 { this->type_from_chan_element_ = true; }
2261
2262 // After we lower the select statement, we once again set the type
2263 // from the initialization expression.
2264 void
2265 clear_type_from_chan_element()
2266 {
2267 go_assert(this->type_from_chan_element_);
2268 this->type_from_chan_element_ = false;
2269 }
2270
2271 // TRUE if this variable was created for a type switch clause.
2272 bool
2273 is_type_switch_var() const
2274 { return this->is_type_switch_var_; }
2275
2276 // Note that this variable was created for a type switch clause.
2277 void
2278 set_is_type_switch_var()
2279 { this->is_type_switch_var_ = true; }
2280
2281 // Mark the variable as going into a unique section.
2282 void
2283 set_in_unique_section()
2284 {
2285 go_assert(this->is_global_);
2286 this->in_unique_section_ = true;
2287 }
2288
2289 // Mark the variable as referenced by an inline body.
2290 void
2291 set_is_referenced_by_inline()
2292 {
2293 go_assert(this->is_global_);
2294 this->is_referenced_by_inline_ = true;
2295 }
2296
2297 // Attach any go:embed comments for this variable.
2298 void
2299 set_embeds(std::vector<std::string>* embeds)
2300 {
2301 go_assert(this->is_global_
2302 && this->init_ == NULL
2303 && this->preinit_ == NULL);
2304 this->embeds_ = embeds;
2305 }
2306
2307 // Return the top-level declaration for this variable.
2308 Statement*
2309 toplevel_decl()
2310 { return this->toplevel_decl_; }
2311
2312 // Set the top-level declaration for this variable. Only used for local
2313 // variables
2314 void
2315 set_toplevel_decl(Statement* s)
2316 {
2317 go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_);
2318 this->toplevel_decl_ = s;
2319 }
2320
2321 // Traverse the initializer expression.
2322 int
2323 traverse_expression(Traverse*, unsigned int traverse_mask);
2324
2325 // Determine the type of the variable if necessary.
2326 void
2327 determine_type();
2328
2329 // Get the backend representation of the variable.
2330 Bvariable*
2331 get_backend_variable(Gogo*, Named_object*, const Package*,
2332 const std::string&);
2333
2334 // Get the initial value of the variable. This may only
2335 // be called if has_pre_init() returns false.
2336 Bexpression*
2337 get_init(Gogo*, Named_object* function);
2338
2339 // Return a series of statements which sets the value of the
2340 // variable in DECL. This should only be called is has_pre_init()
2341 // returns true. DECL may be NULL for a sink variable.
2342 Bstatement*
2343 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
2344
2345 // Export the variable.
2346 void
2347 export_var(Export*, const Named_object*) const;
2348
2349 // Import a variable. Reports whether the import succeeded.
2350 static bool
2351 import_var(Import*, std::string* pname, Package** pkg, bool* is_exported,
2352 Type** ptype);
2353
2354 private:
2355 // The type of a tuple.
2356 Type*
2357 type_from_tuple(Expression*, bool) const;
2358
2359 // The type of a range.
2360 Type*
2361 type_from_range(Expression*, bool, bool) const;
2362
2363 // The element type of a channel.
2364 Type*
2365 type_from_chan_element(Expression*, bool) const;
2366
2367 // The variable's type. This may be NULL if the type is set from
2368 // the expression.
2369 Type* type_;
2370 // The initial value. This may be NULL if the variable should be
2371 // initialized to the default value for the type.
2372 Expression* init_;
2373 // Statements to run before the init statement.
2374 Block* preinit_;
2375 // Location of variable definition.
2376 Location location_;
2377 // Any associated go:embed comments.
2378 std::vector<std::string>* embeds_;
2379 // Backend representation.
2380 Bvariable* backend_;
2381 // Whether this is a global variable.
2382 bool is_global_ : 1;
2383 // Whether this is a function parameter.
2384 bool is_parameter_ : 1;
2385 // Whether this is a closure parameter.
2386 bool is_closure_ : 1;
2387 // Whether this is the receiver parameter of a method.
2388 bool is_receiver_ : 1;
2389 // Whether this is the varargs parameter of a function.
2390 bool is_varargs_parameter_ : 1;
2391 // Whether this is a global sink variable created to run an
2392 // initializer.
2393 bool is_global_sink_ : 1;
2394 // Whether this variable is ever referenced.
2395 bool is_used_ : 1;
2396 // Whether something takes the address of this variable. For a
2397 // local variable this implies that the variable has to be on the
2398 // heap if it escapes from its function.
2399 bool is_address_taken_ : 1;
2400 // Whether something takes the address of this variable such that
2401 // the address does not escape the function.
2402 bool is_non_escaping_address_taken_ : 1;
2403 // True if we have seen this variable in a traversal.
2404 bool seen_ : 1;
2405 // True if we have lowered the initialization expression.
2406 bool init_is_lowered_ : 1;
2407 // True if we have flattened the initialization expression.
2408 bool init_is_flattened_ : 1;
2409 // True if init is a tuple used to set the type.
2410 bool type_from_init_tuple_ : 1;
2411 // True if init is a range clause and the type is the index type.
2412 bool type_from_range_index_ : 1;
2413 // True if init is a range clause and the type is the value type.
2414 bool type_from_range_value_ : 1;
2415 // True if init is a channel and the type is the channel's element type.
2416 bool type_from_chan_element_ : 1;
2417 // True if this is a variable created for a type switch case.
2418 bool is_type_switch_var_ : 1;
2419 // True if we have determined types.
2420 bool determined_type_ : 1;
2421 // True if this variable should be put in a unique section. This is
2422 // used for field tracking.
2423 bool in_unique_section_ : 1;
2424 // True if this variable is referenced from an inlined body that
2425 // will be put into the export data.
2426 bool is_referenced_by_inline_ : 1;
2427 // The top-level declaration for this variable. Only used for local
2428 // variables. Must be a Temporary_statement if not NULL.
2429 Statement* toplevel_decl_;
2430 };
2431
2432 // A variable which is really the name for a function return value, or
2433 // part of one.
2434
2435 class Result_variable
2436 {
2437 public:
2438 Result_variable(Type* type, Function* function, int index,
2439 Location location)
2440 : type_(type), function_(function), index_(index), location_(location),
2441 backend_(NULL), is_address_taken_(false),
2442 is_non_escaping_address_taken_(false)
2443 { }
2444
2445 // Get the type of the result variable.
2446 Type*
2447 type() const
2448 { return this->type_; }
2449
2450 // Get the function that this is associated with.
2451 Function*
2452 function() const
2453 { return this->function_; }
2454
2455 // Index in the list of function results.
2456 int
2457 index() const
2458 { return this->index_; }
2459
2460 // The location of the variable definition.
2461 Location
2462 location() const
2463 { return this->location_; }
2464
2465 // Whether this variable's address is taken.
2466 bool
2467 is_address_taken() const
2468 { return this->is_address_taken_; }
2469
2470 // Note that something takes the address of this variable.
2471 void
2472 set_address_taken()
2473 { this->is_address_taken_ = true; }
2474
2475 // Return whether the address is taken but does not escape.
2476 bool
2477 is_non_escaping_address_taken() const
2478 { return this->is_non_escaping_address_taken_; }
2479
2480 // Note that something takes the address of this variable such that
2481 // the address does not escape the function.
2482 void
2483 set_non_escaping_address_taken()
2484 { this->is_non_escaping_address_taken_ = true; }
2485
2486 // Whether this variable should live in the heap.
2487 bool
2488 is_in_heap() const
2489 { return this->is_address_taken_; }
2490
2491 // Set the function. This is used when cloning functions which call
2492 // recover.
2493 void
2494 set_function(Function* function)
2495 { this->function_ = function; }
2496
2497 // Get the backend representation of the variable.
2498 Bvariable*
2499 get_backend_variable(Gogo*, Named_object*, const std::string&);
2500
2501 private:
2502 // Type of result variable.
2503 Type* type_;
2504 // Function with which this is associated.
2505 Function* function_;
2506 // Index in list of results.
2507 int index_;
2508 // Where the result variable is defined.
2509 Location location_;
2510 // Backend representation.
2511 Bvariable* backend_;
2512 // Whether something takes the address of this variable.
2513 bool is_address_taken_;
2514 // Whether something takes the address of this variable such that
2515 // the address does not escape the function.
2516 bool is_non_escaping_address_taken_;
2517 };
2518
2519 // The value we keep for a named constant. This lets us hold a type
2520 // and an expression.
2521
2522 class Named_constant
2523 {
2524 public:
2525 Named_constant(Type* type, Expression* expr, int iota_value,
2526 Location location)
2527 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2528 lowering_(false), is_sink_(false), bconst_(NULL)
2529 { }
2530
2531 Type*
2532 type() const
2533 { return this->type_; }
2534
2535 void
2536 set_type(Type* t);
2537
2538 Expression*
2539 expr() const
2540 { return this->expr_; }
2541
2542 int
2543 iota_value() const
2544 { return this->iota_value_; }
2545
2546 Location
2547 location() const
2548 { return this->location_; }
2549
2550 // Whether we are lowering.
2551 bool
2552 lowering() const
2553 { return this->lowering_; }
2554
2555 // Set that we are lowering.
2556 void
2557 set_lowering()
2558 { this->lowering_ = true; }
2559
2560 // We are no longer lowering.
2561 void
2562 clear_lowering()
2563 { this->lowering_ = false; }
2564
2565 bool
2566 is_sink() const
2567 { return this->is_sink_; }
2568
2569 void
2570 set_is_sink()
2571 { this->is_sink_ = true; }
2572
2573 // Traverse the expression.
2574 int
2575 traverse_expression(Traverse*);
2576
2577 // Determine the type of the constant if necessary.
2578 void
2579 determine_type();
2580
2581 // Indicate that we found and reported an error for this constant.
2582 void
2583 set_error();
2584
2585 // Export the constant.
2586 void
2587 export_const(Export*, const std::string& name) const;
2588
2589 // Import a constant.
2590 static void
2591 import_const(Import*, std::string*, Type**, Expression**);
2592
2593 // Get the backend representation of the constant value.
2594 Bexpression*
2595 get_backend(Gogo*, Named_object*);
2596
2597 private:
2598 // The type of the constant.
2599 Type* type_;
2600 // The expression for the constant.
2601 Expression* expr_;
2602 // If the predeclared constant iota is used in EXPR_, this is the
2603 // value it will have. We do this because at parse time we don't
2604 // know whether the name "iota" will refer to the predeclared
2605 // constant or to something else. We put in the right value in when
2606 // we lower.
2607 int iota_value_;
2608 // The location of the definition.
2609 Location location_;
2610 // Whether we are currently lowering this constant.
2611 bool lowering_;
2612 // Whether this constant is blank named and needs only type checking.
2613 bool is_sink_;
2614 // The backend representation of the constant value.
2615 Bexpression* bconst_;
2616 };
2617
2618 // A type declaration.
2619
2620 class Type_declaration
2621 {
2622 public:
2623 Type_declaration(Location location)
2624 : location_(location), in_function_(NULL), in_function_index_(0),
2625 methods_(), issued_warning_(false)
2626 { }
2627
2628 // Return the location.
2629 Location
2630 location() const
2631 { return this->location_; }
2632
2633 // Return the function in which this type is declared. This will
2634 // return NULL for a type declared in global scope.
2635 Named_object*
2636 in_function(unsigned int* pindex)
2637 {
2638 *pindex = this->in_function_index_;
2639 return this->in_function_;
2640 }
2641
2642 // Set the function in which this type is declared.
2643 void
2644 set_in_function(Named_object* f, unsigned int index)
2645 {
2646 this->in_function_ = f;
2647 this->in_function_index_ = index;
2648 }
2649
2650 // Add a method to this type. This is used when methods are defined
2651 // before the type.
2652 Named_object*
2653 add_method(const std::string& name, Function* function);
2654
2655 // Add a method declaration to this type.
2656 Named_object*
2657 add_method_declaration(const std::string& name, Package*,
2658 Function_type* type, Location location);
2659
2660 // Add an already created object as a method.
2661 void
2662 add_existing_method(Named_object* no)
2663 { this->methods_.push_back(no); }
2664
2665 // Return whether any methods were defined.
2666 bool
2667 has_methods() const;
2668
2669 // Return the methods.
2670 const std::vector<Named_object*>*
2671 methods() const
2672 { return &this->methods_; }
2673
2674 // Define methods when the real type is known.
2675 void
2676 define_methods(Named_type*);
2677
2678 // This is called if we are trying to use this type. It returns
2679 // true if we should issue a warning.
2680 bool
2681 using_type();
2682
2683 private:
2684 // The location of the type declaration.
2685 Location location_;
2686 // If this type is declared in a function, a pointer back to the
2687 // function in which it is defined.
2688 Named_object* in_function_;
2689 // The index of this type in IN_FUNCTION_.
2690 unsigned int in_function_index_;
2691 // Methods defined before the type is defined.
2692 std::vector<Named_object*> methods_;
2693 // True if we have issued a warning about a use of this type
2694 // declaration when it is undefined.
2695 bool issued_warning_;
2696 };
2697
2698 // An unknown object. These are created by the parser for forward
2699 // references to names which have not been seen before. In a correct
2700 // program, these will always point to a real definition by the end of
2701 // the parse. Because they point to another Named_object, these may
2702 // only be referenced by Unknown_expression objects.
2703
2704 class Unknown_name
2705 {
2706 public:
2707 Unknown_name(Location location)
2708 : location_(location), real_named_object_(NULL)
2709 { }
2710
2711 // Return the location where this name was first seen.
2712 Location
2713 location() const
2714 { return this->location_; }
2715
2716 // Return the real named object that this points to, or NULL if it
2717 // was never resolved.
2718 Named_object*
2719 real_named_object() const
2720 { return this->real_named_object_; }
2721
2722 // Set the real named object that this points to.
2723 void
2724 set_real_named_object(Named_object* no);
2725
2726 private:
2727 // The location where this name was first seen.
2728 Location location_;
2729 // The real named object when it is known.
2730 Named_object*
2731 real_named_object_;
2732 };
2733
2734 // A named object named. This is the result of a declaration. We
2735 // don't use a superclass because they all have to be handled
2736 // differently.
2737
2738 class Named_object
2739 {
2740 public:
2741 enum Classification
2742 {
2743 // An uninitialized Named_object. We should never see this.
2744 NAMED_OBJECT_UNINITIALIZED,
2745 // An erroneous name. This indicates a parse error, to avoid
2746 // later errors about undefined references.
2747 NAMED_OBJECT_ERRONEOUS,
2748 // An unknown name. This is used for forward references. In a
2749 // correct program, these will all be resolved by the end of the
2750 // parse.
2751 NAMED_OBJECT_UNKNOWN,
2752 // A const.
2753 NAMED_OBJECT_CONST,
2754 // A type.
2755 NAMED_OBJECT_TYPE,
2756 // A forward type declaration.
2757 NAMED_OBJECT_TYPE_DECLARATION,
2758 // A var.
2759 NAMED_OBJECT_VAR,
2760 // A result variable in a function.
2761 NAMED_OBJECT_RESULT_VAR,
2762 // The blank identifier--the special variable named _.
2763 NAMED_OBJECT_SINK,
2764 // A func.
2765 NAMED_OBJECT_FUNC,
2766 // A forward func declaration.
2767 NAMED_OBJECT_FUNC_DECLARATION,
2768 // A package.
2769 NAMED_OBJECT_PACKAGE
2770 };
2771
2772 // Return the classification.
2773 Classification
2774 classification() const
2775 { return this->classification_; }
2776
2777 // Classifiers.
2778
2779 bool
2780 is_erroneous() const
2781 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2782
2783 bool
2784 is_unknown() const
2785 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2786
2787 bool
2788 is_const() const
2789 { return this->classification_ == NAMED_OBJECT_CONST; }
2790
2791 bool
2792 is_type() const
2793 { return this->classification_ == NAMED_OBJECT_TYPE; }
2794
2795 bool
2796 is_type_declaration() const
2797 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2798
2799 bool
2800 is_variable() const
2801 { return this->classification_ == NAMED_OBJECT_VAR; }
2802
2803 bool
2804 is_result_variable() const
2805 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2806
2807 bool
2808 is_sink() const
2809 { return this->classification_ == NAMED_OBJECT_SINK; }
2810
2811 bool
2812 is_function() const
2813 { return this->classification_ == NAMED_OBJECT_FUNC; }
2814
2815 bool
2816 is_function_declaration() const
2817 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2818
2819 bool
2820 is_package() const
2821 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2822
2823 // Creators.
2824
2825 static Named_object*
2826 make_erroneous_name(const std::string& name)
2827 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2828
2829 static Named_object*
2830 make_unknown_name(const std::string& name, Location);
2831
2832 static Named_object*
2833 make_constant(const Typed_identifier&, const Package*, Expression*,
2834 int iota_value);
2835
2836 static Named_object*
2837 make_type(const std::string&, const Package*, Type*, Location);
2838
2839 static Named_object*
2840 make_type_declaration(const std::string&, const Package*, Location);
2841
2842 static Named_object*
2843 make_variable(const std::string&, const Package*, Variable*);
2844
2845 static Named_object*
2846 make_result_variable(const std::string&, Result_variable*);
2847
2848 static Named_object*
2849 make_sink();
2850
2851 static Named_object*
2852 make_function(const std::string&, const Package*, Function*);
2853
2854 static Named_object*
2855 make_function_declaration(const std::string&, const Package*, Function_type*,
2856 Location);
2857
2858 static Named_object*
2859 make_package(const std::string& alias, Package* package);
2860
2861 // Getters.
2862
2863 Unknown_name*
2864 unknown_value()
2865 {
2866 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2867 return this->u_.unknown_value;
2868 }
2869
2870 const Unknown_name*
2871 unknown_value() const
2872 {
2873 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2874 return this->u_.unknown_value;
2875 }
2876
2877 Named_constant*
2878 const_value()
2879 {
2880 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2881 return this->u_.const_value;
2882 }
2883
2884 const Named_constant*
2885 const_value() const
2886 {
2887 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2888 return this->u_.const_value;
2889 }
2890
2891 Named_type*
2892 type_value()
2893 {
2894 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2895 return this->u_.type_value;
2896 }
2897
2898 const Named_type*
2899 type_value() const
2900 {
2901 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2902 return this->u_.type_value;
2903 }
2904
2905 Type_declaration*
2906 type_declaration_value()
2907 {
2908 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2909 return this->u_.type_declaration;
2910 }
2911
2912 const Type_declaration*
2913 type_declaration_value() const
2914 {
2915 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2916 return this->u_.type_declaration;
2917 }
2918
2919 Variable*
2920 var_value()
2921 {
2922 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2923 return this->u_.var_value;
2924 }
2925
2926 const Variable*
2927 var_value() const
2928 {
2929 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2930 return this->u_.var_value;
2931 }
2932
2933 Result_variable*
2934 result_var_value()
2935 {
2936 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2937 return this->u_.result_var_value;
2938 }
2939
2940 const Result_variable*
2941 result_var_value() const
2942 {
2943 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2944 return this->u_.result_var_value;
2945 }
2946
2947 Function*
2948 func_value()
2949 {
2950 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2951 return this->u_.func_value;
2952 }
2953
2954 const Function*
2955 func_value() const
2956 {
2957 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2958 return this->u_.func_value;
2959 }
2960
2961 Function_declaration*
2962 func_declaration_value()
2963 {
2964 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2965 return this->u_.func_declaration_value;
2966 }
2967
2968 const Function_declaration*
2969 func_declaration_value() const
2970 {
2971 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2972 return this->u_.func_declaration_value;
2973 }
2974
2975 Package*
2976 package_value()
2977 {
2978 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2979 return this->u_.package_value;
2980 }
2981
2982 const Package*
2983 package_value() const
2984 {
2985 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2986 return this->u_.package_value;
2987 }
2988
2989 const std::string&
2990 name() const
2991 { return this->name_; }
2992
2993 // Return the name to use in an error message. The difference is
2994 // that if this Named_object is defined in a different package, this
2995 // will return PACKAGE.NAME.
2996 std::string
2997 message_name() const;
2998
2999 const Package*
3000 package() const
3001 { return this->package_; }
3002
3003 // Resolve an unknown value if possible. This returns the same
3004 // Named_object or a new one.
3005 Named_object*
3006 resolve()
3007 {
3008 Named_object* ret = this;
3009 if (this->is_unknown())
3010 {
3011 Named_object* r = this->unknown_value()->real_named_object();
3012 if (r != NULL)
3013 ret = r;
3014 }
3015 return ret;
3016 }
3017
3018 const Named_object*
3019 resolve() const
3020 {
3021 const Named_object* ret = this;
3022 if (this->is_unknown())
3023 {
3024 const Named_object* r = this->unknown_value()->real_named_object();
3025 if (r != NULL)
3026 ret = r;
3027 }
3028 return ret;
3029 }
3030
3031 // The location where this object was defined or referenced.
3032 Location
3033 location() const;
3034
3035 // Convert a variable to the backend representation.
3036 Bvariable*
3037 get_backend_variable(Gogo*, Named_object* function);
3038
3039 // Get the backend representation of this object.
3040 void
3041 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
3042 std::vector<Bfunction*>&);
3043
3044 // Define a type declaration.
3045 void
3046 set_type_value(Named_type*);
3047
3048 // Define a function declaration.
3049 void
3050 set_function_value(Function*);
3051
3052 // Declare an unknown name as a type declaration.
3053 void
3054 declare_as_type();
3055
3056 // Export this object.
3057 void
3058 export_named_object(Export*) const;
3059
3060 // Mark this named object as an invalid redefinition of another object.
3061 void
3062 set_is_redefinition()
3063 { this->is_redefinition_ = true; }
3064
3065 // Return whether or not this object is a invalid redefinition of another
3066 // object.
3067 bool
3068 is_redefinition() const
3069 { return this->is_redefinition_; }
3070
3071 private:
3072 Named_object(const std::string&, const Package*, Classification);
3073
3074 // The name of the object.
3075 std::string name_;
3076 // The package that this object is in. This is NULL if it is in the
3077 // file we are compiling.
3078 const Package* package_;
3079 // The type of object this is.
3080 Classification classification_;
3081 // The real data.
3082 union
3083 {
3084 Unknown_name* unknown_value;
3085 Named_constant* const_value;
3086 Named_type* type_value;
3087 Type_declaration* type_declaration;
3088 Variable* var_value;
3089 Result_variable* result_var_value;
3090 Function* func_value;
3091 Function_declaration* func_declaration_value;
3092 Package* package_value;
3093 } u_;
3094 // True if this object is an invalid redefinition of another object.
3095 bool is_redefinition_;
3096 };
3097
3098 // A binding contour. This binds names to objects.
3099
3100 class Bindings
3101 {
3102 public:
3103 // Type for mapping from names to objects.
3104 typedef Unordered_map(std::string, Named_object*) Contour;
3105
3106 Bindings(Bindings* enclosing);
3107
3108 // Add an erroneous name.
3109 Named_object*
3110 add_erroneous_name(const std::string& name)
3111 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
3112
3113 // Add an unknown name.
3114 Named_object*
3115 add_unknown_name(const std::string& name, Location location)
3116 {
3117 return this->add_named_object(Named_object::make_unknown_name(name,
3118 location));
3119 }
3120
3121 // Add a constant.
3122 Named_object*
3123 add_constant(const Typed_identifier& tid, const Package* package,
3124 Expression* expr, int iota_value)
3125 {
3126 return this->add_named_object(Named_object::make_constant(tid, package,
3127 expr,
3128 iota_value));
3129 }
3130
3131 // Add a type.
3132 Named_object*
3133 add_type(const std::string& name, const Package* package, Type* type,
3134 Location location)
3135 {
3136 return this->add_named_object(Named_object::make_type(name, package, type,
3137 location));
3138 }
3139
3140 // Add a named type. This is used for builtin types, and to add an
3141 // imported type to the global scope.
3142 Named_object*
3143 add_named_type(Named_type* named_type);
3144
3145 // Add a type declaration.
3146 Named_object*
3147 add_type_declaration(const std::string& name, const Package* package,
3148 Location location)
3149 {
3150 Named_object* no = Named_object::make_type_declaration(name, package,
3151 location);
3152 return this->add_named_object(no);
3153 }
3154
3155 // Add a variable.
3156 Named_object*
3157 add_variable(const std::string& name, const Package* package,
3158 Variable* variable)
3159 {
3160 return this->add_named_object(Named_object::make_variable(name, package,
3161 variable));
3162 }
3163
3164 // Add a result variable.
3165 Named_object*
3166 add_result_variable(const std::string& name, Result_variable* result)
3167 {
3168 return this->add_named_object(Named_object::make_result_variable(name,
3169 result));
3170 }
3171
3172 // Add a function.
3173 Named_object*
3174 add_function(const std::string& name, const Package*, Function* function);
3175
3176 // Add a function declaration.
3177 Named_object*
3178 add_function_declaration(const std::string& name, const Package* package,
3179 Function_type* type, Location location);
3180
3181 // Add a package. The location is the location of the import
3182 // statement.
3183 Named_object*
3184 add_package(const std::string& alias, Package* package)
3185 {
3186 Named_object* no = Named_object::make_package(alias, package);
3187 return this->add_named_object(no);
3188 }
3189
3190 // Define a type which was already declared.
3191 void
3192 define_type(Named_object*, Named_type*);
3193
3194 // Add a method to the list of objects. This is not added to the
3195 // lookup table.
3196 void
3197 add_method(Named_object*);
3198
3199 // Add a named object to this binding.
3200 Named_object*
3201 add_named_object(Named_object* no)
3202 { return this->add_named_object_to_contour(&this->bindings_, no); }
3203
3204 // Clear all names in file scope from the bindings.
3205 void
3206 clear_file_scope(Gogo*);
3207
3208 // Look up a name in this binding contour and in any enclosing
3209 // binding contours. This returns NULL if the name is not found.
3210 Named_object*
3211 lookup(const std::string&) const;
3212
3213 // Look up a name in this binding contour without looking in any
3214 // enclosing binding contours. Returns NULL if the name is not found.
3215 Named_object*
3216 lookup_local(const std::string&) const;
3217
3218 // Remove a name.
3219 void
3220 remove_binding(Named_object*);
3221
3222 // Mark all variables as used. This is used for some types of parse
3223 // error.
3224 void
3225 mark_locals_used();
3226
3227 // Traverse the tree. See the Traverse class.
3228 int
3229 traverse(Traverse*, bool is_global);
3230
3231 // Iterate over definitions. This does not include things which
3232 // were only declared.
3233
3234 typedef std::vector<Named_object*>::const_iterator
3235 const_definitions_iterator;
3236
3237 const_definitions_iterator
3238 begin_definitions() const
3239 { return this->named_objects_.begin(); }
3240
3241 const_definitions_iterator
3242 end_definitions() const
3243 { return this->named_objects_.end(); }
3244
3245 // Return the number of definitions.
3246 size_t
3247 size_definitions() const
3248 { return this->named_objects_.size(); }
3249
3250 // Return whether there are no definitions.
3251 bool
3252 empty_definitions() const
3253 { return this->named_objects_.empty(); }
3254
3255 // Iterate over declarations. This is everything that has been
3256 // declared, which includes everything which has been defined.
3257
3258 typedef Contour::const_iterator const_declarations_iterator;
3259
3260 const_declarations_iterator
3261 begin_declarations() const
3262 { return this->bindings_.begin(); }
3263
3264 const_declarations_iterator
3265 end_declarations() const
3266 { return this->bindings_.end(); }
3267
3268 // Return the number of declarations.
3269 size_t
3270 size_declarations() const
3271 { return this->bindings_.size(); }
3272
3273 // Return whether there are no declarations.
3274 bool
3275 empty_declarations() const
3276 { return this->bindings_.empty(); }
3277
3278 // Return the first declaration.
3279 Named_object*
3280 first_declaration()
3281 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
3282
3283 // Dump to stderr for debugging
3284 void debug_dump();
3285
3286 private:
3287 Named_object*
3288 add_named_object_to_contour(Contour*, Named_object*);
3289
3290 Named_object*
3291 new_definition(Named_object*, Named_object*);
3292
3293 // Enclosing bindings.
3294 Bindings* enclosing_;
3295 // The list of objects.
3296 std::vector<Named_object*> named_objects_;
3297 // The mapping from names to objects.
3298 Contour bindings_;
3299 };
3300
3301 // A label.
3302
3303 class Label
3304 {
3305 public:
3306 Label(const std::string& name)
3307 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
3308 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
3309 { }
3310
3311 // Return the label's name.
3312 const std::string&
3313 name() const
3314 { return this->name_; }
3315
3316 // Return whether the label has been defined.
3317 bool
3318 is_defined() const
3319 { return !Linemap::is_unknown_location(this->location_); }
3320
3321 // Return whether the label has been used.
3322 bool
3323 is_used() const
3324 { return this->is_used_; }
3325
3326 // Record that the label is used.
3327 void
3328 set_is_used()
3329 { this->is_used_ = true; }
3330
3331 // Return whether this label is looping.
3332 bool
3333 looping() const
3334 { return this->depth_ == DEPTH_LOOPING; }
3335
3336 // Set this label as looping.
3337 void
3338 set_looping()
3339 { this->depth_ = DEPTH_LOOPING; }
3340
3341 // Return whether this label is nonlooping.
3342 bool
3343 nonlooping() const
3344 { return this->depth_ == DEPTH_NONLOOPING; }
3345
3346 // Set this label as nonlooping.
3347 void
3348 set_nonlooping()
3349 { this->depth_ = DEPTH_NONLOOPING; }
3350
3351 // Return the location of the definition.
3352 Location
3353 location() const
3354 { return this->location_; }
3355
3356 // Return the bindings snapshot.
3357 Bindings_snapshot*
3358 snapshot() const
3359 { return this->snapshot_; }
3360
3361 // Add a snapshot of a goto which refers to this label.
3362 void
3363 add_snapshot_ref(Bindings_snapshot* snapshot)
3364 {
3365 go_assert(Linemap::is_unknown_location(this->location_));
3366 this->refs_.push_back(snapshot);
3367 }
3368
3369 // Return the list of snapshots of goto statements which refer to
3370 // this label.
3371 const std::vector<Bindings_snapshot*>&
3372 refs() const
3373 { return this->refs_; }
3374
3375 // Clear the references.
3376 void
3377 clear_refs();
3378
3379 // Define the label at LOCATION with the given bindings snapshot.
3380 void
3381 define(Location location, Bindings_snapshot* snapshot)
3382 {
3383 if (this->is_dummy_label())
3384 return;
3385 go_assert(Linemap::is_unknown_location(this->location_)
3386 && this->snapshot_ == NULL);
3387 this->location_ = location;
3388 this->snapshot_ = snapshot;
3389 }
3390
3391 // Return the backend representation for this label.
3392 Blabel*
3393 get_backend_label(Translate_context*);
3394
3395 // Return an expression for the address of this label. This is used
3396 // to get the return address of a deferred function to see whether
3397 // the function may call recover.
3398 Bexpression*
3399 get_addr(Translate_context*, Location location);
3400
3401 // Return a dummy label, representing any instance of the blank label.
3402 static Label*
3403 create_dummy_label();
3404
3405 // Return TRUE if this is a dummy label.
3406 bool
3407 is_dummy_label() const
3408 { return this->name_ == "_"; }
3409
3410 // A classification of a label's looping depth.
3411 enum Loop_depth
3412 {
3413 DEPTH_UNKNOWN,
3414 // A label never jumped to.
3415 DEPTH_NONLOOPING,
3416 // A label jumped to.
3417 DEPTH_LOOPING
3418 };
3419
3420 private:
3421 // The name of the label.
3422 std::string name_;
3423 // The location of the definition. This is 0 if the label has not
3424 // yet been defined.
3425 Location location_;
3426 // A snapshot of the set of bindings defined at this label, used to
3427 // issue errors about invalid goto statements.
3428 Bindings_snapshot* snapshot_;
3429 // A list of snapshots of goto statements which refer to this label.
3430 std::vector<Bindings_snapshot*> refs_;
3431 // Whether the label has been used.
3432 bool is_used_;
3433 // The backend representation.
3434 Blabel* blabel_;
3435 // The looping depth of this label, for escape analysis.
3436 Loop_depth depth_;
3437 };
3438
3439 // An unnamed label. These are used when lowering loops.
3440
3441 class Unnamed_label
3442 {
3443 public:
3444 Unnamed_label(Location location)
3445 : location_(location), derived_from_(NULL), blabel_(NULL)
3446 { }
3447
3448 // Get the location where the label is defined.
3449 Location
3450 location() const
3451 { return this->location_; }
3452
3453 // Set the location where the label is defined.
3454 void
3455 set_location(Location location)
3456 { this->location_ = location; }
3457
3458 // Get the top level statement this unnamed label is derived from.
3459 Statement*
3460 derived_from() const
3461 { return this->derived_from_; }
3462
3463 // Set the top level statement this unnamed label is derived from.
3464 void
3465 set_derived_from(Statement* s)
3466 { this->derived_from_ = s; }
3467
3468 // Return a statement which defines this label.
3469 Bstatement*
3470 get_definition(Translate_context*);
3471
3472 // Return a goto to this label from LOCATION.
3473 Bstatement*
3474 get_goto(Translate_context*, Location location);
3475
3476 private:
3477 // Return the backend representation.
3478 Blabel*
3479 get_blabel(Translate_context*);
3480
3481 // The location where the label is defined.
3482 Location location_;
3483 // The top-level statement this unnamed label was derived/lowered from.
3484 // This is NULL is this label is not the top-level of a lowered statement.
3485 Statement* derived_from_;
3486 // The backend representation of this label.
3487 Blabel* blabel_;
3488 };
3489
3490 // An alias for an imported package.
3491
3492 class Package_alias
3493 {
3494 public:
3495 Package_alias(Location location)
3496 : location_(location), used_(0)
3497 { }
3498
3499 // The location of the import statement.
3500 Location
3501 location()
3502 { return this->location_; }
3503
3504 // How many symbols from the package were used under this alias.
3505 size_t
3506 used() const
3507 { return this->used_; }
3508
3509 // Note that some symbol was used under this alias.
3510 void
3511 note_usage()
3512 { this->used_++; }
3513
3514 private:
3515 // The location of the import statement.
3516 Location location_;
3517 // The amount of times some name from this package was used under this alias.
3518 size_t used_;
3519 };
3520
3521 // An imported package.
3522
3523 class Package
3524 {
3525 public:
3526 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3527 Location location);
3528
3529 // Get the package path used for all symbols exported from this
3530 // package.
3531 const std::string&
3532 pkgpath() const
3533 { return this->pkgpath_; }
3534
3535 // Return the package path to use for a symbol name.
3536 std::string
3537 pkgpath_symbol() const;
3538
3539 // Set the package path symbol.
3540 void
3541 set_pkgpath_symbol(const std::string&);
3542
3543 // Return the location of the most recent import statement.
3544 Location
3545 location() const
3546 { return this->location_; }
3547
3548 // Return whether we know the name of this package yet.
3549 bool
3550 has_package_name() const
3551 { return !this->package_name_.empty(); }
3552
3553 // The name that this package uses in its package clause. This may
3554 // be different from the name in the associated Named_object if the
3555 // import statement used an alias.
3556 const std::string&
3557 package_name() const
3558 {
3559 go_assert(!this->package_name_.empty());
3560 return this->package_name_;
3561 }
3562
3563 // Return the bindings.
3564 Bindings*
3565 bindings() const
3566 { return this->bindings_; }
3567
3568 // Type used to map import names to package aliases.
3569 typedef std::map<std::string, Package_alias*> Aliases;
3570
3571 // Return the set of package aliases.
3572 const Aliases&
3573 aliases() const
3574 { return this->aliases_; }
3575
3576 // Note that some symbol from this package was used and qualified by ALIAS.
3577 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3578 void
3579 note_usage(const std::string& alias) const;
3580
3581 // Note that USAGE might be a fake usage of this package.
3582 void
3583 note_fake_usage(Expression* usage) const
3584 { this->fake_uses_.insert(usage); }
3585
3586 // Forget a given USAGE of this package.
3587 void
3588 forget_usage(Expression* usage) const;
3589
3590 // Clear the used field for the next file.
3591 void
3592 clear_used();
3593
3594 // Look up a name in the package. Returns NULL if the name is not
3595 // found.
3596 Named_object*
3597 lookup(const std::string& name) const
3598 { return this->bindings_->lookup(name); }
3599
3600 // Set the name of the package.
3601 void
3602 set_package_name(const std::string& name, Location);
3603
3604 // Set the location of the package. This is used to record the most
3605 // recent import location.
3606 void
3607 set_location(Location location)
3608 { this->location_ = location; }
3609
3610 // Add a package name as an ALIAS for this package.
3611 Package_alias*
3612 add_alias(const std::string& alias, Location);
3613
3614 // Add a constant to the package.
3615 Named_object*
3616 add_constant(const Typed_identifier& tid, Expression* expr)
3617 { return this->bindings_->add_constant(tid, this, expr, 0); }
3618
3619 // Add a type to the package.
3620 Named_object*
3621 add_type(const std::string& name, Type* type, Location location)
3622 { return this->bindings_->add_type(name, this, type, location); }
3623
3624 // Add a type declaration to the package.
3625 Named_object*
3626 add_type_declaration(const std::string& name, Location location)
3627 { return this->bindings_->add_type_declaration(name, this, location); }
3628
3629 // Add a variable to the package.
3630 Named_object*
3631 add_variable(const std::string& name, Variable* variable)
3632 { return this->bindings_->add_variable(name, this, variable); }
3633
3634 // Add a function declaration to the package.
3635 Named_object*
3636 add_function_declaration(const std::string& name, Function_type* type,
3637 Location loc)
3638 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3639
3640 // Determine types of constants.
3641 void
3642 determine_types();
3643
3644 private:
3645 // The package path for type reflection data.
3646 std::string pkgpath_;
3647 // The package path for symbol names.
3648 std::string pkgpath_symbol_;
3649 // The name that this package uses in the package clause. This may
3650 // be the empty string if it is not yet known.
3651 std::string package_name_;
3652 // The names in this package.
3653 Bindings* bindings_;
3654 // The location of the most recent import statement.
3655 Location location_;
3656 // The set of aliases associated with this package.
3657 Aliases aliases_;
3658 // A set of possibly fake uses of this package. This is mutable because we
3659 // can track fake uses of a package even if we have a const pointer to it.
3660 mutable std::set<Expression*> fake_uses_;
3661 };
3662
3663 // Return codes for the traversal functions. This is not an enum
3664 // because we want to be able to declare traversal functions in other
3665 // header files without including this one.
3666
3667 // Continue traversal as usual.
3668 const int TRAVERSE_CONTINUE = -1;
3669
3670 // Exit traversal.
3671 const int TRAVERSE_EXIT = 0;
3672
3673 // Continue traversal, but skip components of the current object.
3674 // E.g., if this is returned by Traverse::statement, we do not
3675 // traverse the expressions in the statement even if
3676 // traverse_expressions is set in the traverse_mask.
3677 const int TRAVERSE_SKIP_COMPONENTS = 1;
3678
3679 // This class is used when traversing the parse tree. The caller uses
3680 // a subclass which overrides functions as desired.
3681
3682 class Traverse
3683 {
3684 public:
3685 // These bitmasks say what to traverse.
3686 static const unsigned int traverse_variables = 0x1;
3687 static const unsigned int traverse_constants = 0x2;
3688 static const unsigned int traverse_functions = 0x4;
3689 static const unsigned int traverse_blocks = 0x8;
3690 static const unsigned int traverse_statements = 0x10;
3691 static const unsigned int traverse_expressions = 0x20;
3692 static const unsigned int traverse_types = 0x40;
3693 static const unsigned int traverse_func_declarations = 0x80;
3694
3695 Traverse(unsigned int traverse_mask)
3696 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3697 { }
3698
3699 virtual ~Traverse();
3700
3701 // The bitmask of what to traverse.
3702 unsigned int
3703 traverse_mask() const
3704 { return this->traverse_mask_; }
3705
3706 // Record that we are going to traverse a type. This returns true
3707 // if the type has already been seen in this traversal. This is
3708 // required because types, unlike expressions, can form a circular
3709 // graph.
3710 bool
3711 remember_type(const Type*);
3712
3713 // Record that we are going to see an expression. This returns true
3714 // if the expression has already been seen in this traversal. This
3715 // is only needed for cases where multiple expressions can point to
3716 // a single one.
3717 bool
3718 remember_expression(const Expression*);
3719
3720 // These functions return one of the TRAVERSE codes defined above.
3721
3722 // If traverse_variables is set in the mask, this is called for
3723 // every variable in the tree.
3724 virtual int
3725 variable(Named_object*);
3726
3727 // If traverse_constants is set in the mask, this is called for
3728 // every named constant in the tree. The bool parameter is true for
3729 // a global constant.
3730 virtual int
3731 constant(Named_object*, bool);
3732
3733 // If traverse_functions is set in the mask, this is called for
3734 // every function in the tree.
3735 virtual int
3736 function(Named_object*);
3737
3738 // If traverse_blocks is set in the mask, this is called for every
3739 // block in the tree.
3740 virtual int
3741 block(Block*);
3742
3743 // If traverse_statements is set in the mask, this is called for
3744 // every statement in the tree.
3745 virtual int
3746 statement(Block*, size_t* index, Statement*);
3747
3748 // If traverse_expressions is set in the mask, this is called for
3749 // every expression in the tree.
3750 virtual int
3751 expression(Expression**);
3752
3753 // If traverse_types is set in the mask, this is called for every
3754 // type in the tree.
3755 virtual int
3756 type(Type*);
3757
3758 // If traverse_func_declarations is set in the mask, this is called
3759 // for every function declarations in the tree.
3760 virtual int
3761 function_declaration(Named_object*);
3762
3763 private:
3764 // A hash table for types we have seen during this traversal. Note
3765 // that this uses the default hash functions for pointers rather
3766 // than Type_hash_identical and Type_identical. This is because for
3767 // traversal we care about seeing a specific type structure. If
3768 // there are two separate instances of identical types, we want to
3769 // traverse both.
3770 typedef Unordered_set(const Type*) Types_seen;
3771
3772 typedef Unordered_set(const Expression*) Expressions_seen;
3773
3774 // Bitmask of what sort of objects to traverse.
3775 unsigned int traverse_mask_;
3776 // Types which have been seen in this traversal.
3777 Types_seen* types_seen_;
3778 // Expressions which have been seen in this traversal.
3779 Expressions_seen* expressions_seen_;
3780 };
3781
3782 // This class looks for interface types to finalize methods of inherited
3783 // interfaces.
3784
3785 class Finalize_methods : public Traverse
3786 {
3787 public:
3788 Finalize_methods(Gogo* gogo)
3789 : Traverse(traverse_types),
3790 gogo_(gogo)
3791 { }
3792
3793 int
3794 type(Type*);
3795
3796 private:
3797 Gogo* gogo_;
3798 };
3799
3800 // A class which makes it easier to insert new statements before the
3801 // current statement during a traversal.
3802
3803 class Statement_inserter
3804 {
3805 public:
3806 typedef Unordered_set(Statement*) Statements;
3807
3808 // Empty constructor.
3809 Statement_inserter()
3810 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL),
3811 statements_added_(NULL)
3812 { }
3813
3814 // Constructor for a statement in a block.
3815 Statement_inserter(Block* block, size_t *pindex, Statements *added = NULL)
3816 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL),
3817 statements_added_(added)
3818 { }
3819
3820 // Constructor for a global variable.
3821 Statement_inserter(Gogo* gogo, Variable* var, Statements *added = NULL)
3822 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var),
3823 statements_added_(added)
3824 { go_assert(var->is_global()); }
3825
3826 // We use the default copy constructor and assignment operator.
3827
3828 // Insert S before the statement we are traversing, or before the
3829 // initialization expression of a global variable.
3830 void
3831 insert(Statement* s);
3832
3833 private:
3834 // The block that the statement is in.
3835 Block* block_;
3836 // The index of the statement that we are traversing.
3837 size_t* pindex_;
3838 // The IR, needed when looking at an initializer expression for a
3839 // global variable.
3840 Gogo* gogo_;
3841 // The global variable, when looking at an initializer expression.
3842 Variable* var_;
3843 // If non-null, a set to record new statements inserted (non-owned).
3844 Statements* statements_added_;
3845 };
3846
3847 // When translating the gogo IR into the backend data structure, this
3848 // is the context we pass down the blocks and statements.
3849
3850 class Translate_context
3851 {
3852 public:
3853 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3854 Bblock* bblock)
3855 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3856 block_(block), bblock_(bblock), is_const_(false)
3857 { }
3858
3859 // Accessors.
3860
3861 Gogo*
3862 gogo()
3863 { return this->gogo_; }
3864
3865 Backend*
3866 backend()
3867 { return this->backend_; }
3868
3869 Named_object*
3870 function()
3871 { return this->function_; }
3872
3873 Block*
3874 block()
3875 { return this->block_; }
3876
3877 Bblock*
3878 bblock()
3879 { return this->bblock_; }
3880
3881 bool
3882 is_const()
3883 { return this->is_const_; }
3884
3885 // Make a constant context.
3886 void
3887 set_is_const()
3888 { this->is_const_ = true; }
3889
3890 private:
3891 // The IR for the entire compilation unit.
3892 Gogo* gogo_;
3893 // The generator for the backend data structures.
3894 Backend* backend_;
3895 // The function we are currently translating. NULL if not in a
3896 // function, e.g., the initializer of a global variable.
3897 Named_object* function_;
3898 // The block we are currently translating. NULL if not in a
3899 // function.
3900 Block *block_;
3901 // The backend representation of the current block. NULL if block_
3902 // is NULL.
3903 Bblock* bblock_;
3904 // Whether this is being evaluated in a constant context. This is
3905 // used for type descriptor initializers.
3906 bool is_const_;
3907 };
3908
3909 // This is used by some of the langhooks.
3910 extern Gogo* go_get_gogo();
3911
3912 // Whether we have seen any errors. FIXME: Replace with a backend
3913 // interface.
3914 extern bool saw_errors();
3915
3916 // For use in the debugger
3917 extern void debug_go_gogo(Gogo*);
3918 extern void debug_go_named_object(Named_object*);
3919 extern void debug_go_bindings(Bindings*);
3920
3921
3922 #endif // !defined(GO_GOGO_H)