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