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