compiler: make top-level decl for address-taken non-escaping locals
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob65762727989d9a7fca83d2657335aeeed723de8a
1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
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.
7 #ifndef GO_GOGO_H
8 #define GO_GOGO_H
10 #include "go-linemap.h"
12 class Traverse;
13 class Statement_inserter;
14 class Type;
15 class Type_hash_identical;
16 class Type_equal;
17 class Type_identical;
18 class Typed_identifier;
19 class Typed_identifier_list;
20 class Function_type;
21 class Expression;
22 class Expression_list;
23 class Statement;
24 class Temporary_statement;
25 class Block;
26 class Function;
27 class Bindings;
28 class Bindings_snapshot;
29 class Package;
30 class Variable;
31 class Pointer_type;
32 class Struct_type;
33 class Struct_field;
34 class Struct_field_list;
35 class Array_type;
36 class Map_type;
37 class Channel_type;
38 class Interface_type;
39 class Named_type;
40 class Forward_declaration_type;
41 class Named_object;
42 class Label;
43 class Translate_context;
44 class Backend;
45 class Export;
46 class Import;
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;
57 // This file declares the basic classes used to hold the internal
58 // representation of Go which is built by the parser.
60 // An initialization function for an imported package. This is a
61 // magic function which initializes variables and runs the "init"
62 // function.
64 class Import_init
66 public:
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 { }
72 // The name of the package being imported.
73 const std::string&
74 package_name() const
75 { return this->package_name_; }
77 // The name of the package's init function.
78 const std::string&
79 init_name() const
80 { return this->init_name_; }
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
88 priority() const
89 { return this->priority_; }
91 // Reset priority.
92 void
93 set_priority(int new_priority)
94 { this->priority_ = new_priority; }
96 // Record the fact that some other init fcn must be run before this init fcn.
97 void
98 record_precursor_fcn(std::string init_fcn_name)
99 { this->precursor_functions_.insert(init_fcn_name); }
101 // Return the list of precursor fcns for this fcn (must be run before it).
102 const std::set<std::string>&
103 precursors() const
104 { return this->precursor_functions_; }
106 private:
107 // The name of the package being imported.
108 std::string package_name_;
109 // The name of the package's init function.
110 std::string init_name_;
111 // Names of init functions that must be run before this fcn.
112 std::set<std::string> precursor_functions_;
113 // Priority for this function. See note above on obsolescence.
114 int priority_;
117 // For sorting purposes.
119 struct Import_init_lt {
120 bool operator()(const Import_init* i1, const Import_init* i2) const
122 return i1->init_name() < i2->init_name();
126 // Set of import init objects.
127 class Import_init_set : public std::set<Import_init*, Import_init_lt> {
130 inline bool
131 priority_compare(const Import_init* i1, const Import_init* i2)
133 if (i1->priority() < i2->priority())
134 return true;
135 if (i1->priority() > i2->priority())
136 return false;
137 if (i1->package_name() != i2->package_name())
138 return i1->package_name() < i2->package_name();
139 return i1->init_name() < i2->init_name();
142 // The holder for the internal representation of the entire
143 // compilation unit.
145 class Gogo
147 public:
148 // Create the IR, passing in the sizes of the types "int" and
149 // "uintptr" in bits.
150 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
152 // Get the backend generator.
153 Backend*
154 backend()
155 { return this->backend_; }
157 // Get the Location generator.
158 Linemap*
159 linemap()
160 { return this->linemap_; }
162 // Get the package name.
163 const std::string&
164 package_name() const;
166 // Set the package name.
167 void
168 set_package_name(const std::string&, Location);
170 // Return whether this is the "main" package.
171 bool
172 is_main_package() const;
174 // If necessary, adjust the name to use for a hidden symbol. We add
175 // the package name, so that hidden symbols in different packages do
176 // not collide.
177 std::string
178 pack_hidden_name(const std::string& name, bool is_exported) const
180 return (is_exported
181 ? name
182 : '.' + this->pkgpath() + '.' + name);
185 // Unpack a name which may have been hidden. Returns the
186 // user-visible name of the object.
187 static std::string
188 unpack_hidden_name(const std::string& name)
189 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
191 // Return whether a possibly packed name is hidden.
192 static bool
193 is_hidden_name(const std::string& name)
194 { return name[0] == '.'; }
196 // Return the package path of a hidden name.
197 static std::string
198 hidden_name_pkgpath(const std::string& name)
200 go_assert(Gogo::is_hidden_name(name));
201 return name.substr(1, name.rfind('.') - 1);
204 // Given a name which may or may not have been hidden, return the
205 // name to use within a mangled symbol name.
206 static std::string
207 mangle_possibly_hidden_name(const std::string& name)
209 // FIXME: This adds in pkgpath twice for hidden symbols, which is
210 // less than ideal.
211 std::string n;
212 if (!Gogo::is_hidden_name(name))
213 n = name;
214 else
216 n = ".";
217 std::string pkgpath = Gogo::hidden_name_pkgpath(name);
218 n.append(Gogo::pkgpath_for_symbol(pkgpath));
219 n.append(1, '.');
220 n.append(Gogo::unpack_hidden_name(name));
222 return n;
225 // Given a name which may or may not have been hidden, return the
226 // name to use in an error message.
227 static std::string
228 message_name(const std::string& name);
230 // Return whether a name is the blank identifier _.
231 static bool
232 is_sink_name(const std::string& name)
234 return (name[0] == '.'
235 && name[name.length() - 1] == '_'
236 && name[name.length() - 2] == '.');
239 // Convert a pkgpath into a string suitable for a symbol
240 static std::string
241 pkgpath_for_symbol(const std::string& pkgpath);
243 // Return the package path to use for reflect.Type.PkgPath.
244 const std::string&
245 pkgpath() const;
247 // Return the package path to use for a symbol name.
248 const std::string&
249 pkgpath_symbol() const;
251 // Set the package path from a command line option.
252 void
253 set_pkgpath(const std::string&);
255 // Set the prefix from a command line option.
256 void
257 set_prefix(const std::string&);
259 // Return whether pkgpath was set from a command line option.
260 bool
261 pkgpath_from_option() const
262 { return this->pkgpath_from_option_; }
264 // Return the relative import path as set from the command line.
265 // Returns an empty string if it was not set.
266 const std::string&
267 relative_import_path() const
268 { return this->relative_import_path_; }
270 // Set the relative import path from a command line option.
271 void
272 set_relative_import_path(const std::string& s)
273 { this->relative_import_path_ = s; }
275 // Set the C header file to write. This is used for the runtime
276 // package.
277 void
278 set_c_header(const std::string& s)
279 { this->c_header_ = s; }
281 // Return whether to check for division by zero in binary operations.
282 bool
283 check_divide_by_zero() const
284 { return this->check_divide_by_zero_; }
286 // Set the option to check division by zero from a command line option.
287 void
288 set_check_divide_by_zero(bool b)
289 { this->check_divide_by_zero_ = b; }
291 // Return whether to check for division overflow in binary operations.
292 bool
293 check_divide_overflow() const
294 { return this->check_divide_overflow_; }
296 // Set the option to check division overflow from a command line option.
297 void
298 set_check_divide_overflow(bool b)
299 { this->check_divide_overflow_ = b; }
301 // Return whether we are compiling the runtime package.
302 bool
303 compiling_runtime() const
304 { return this->compiling_runtime_; }
306 // Set whether we are compiling the runtime package.
307 void
308 set_compiling_runtime(bool b)
309 { this->compiling_runtime_ = b; }
311 // Return the level of escape analysis debug information to emit.
313 debug_escape_level() const
314 { return this->debug_escape_level_; }
316 // Set the level of escape analysis debugging from a command line option.
317 void
318 set_debug_escape_level(int level)
319 { this->debug_escape_level_ = level; }
321 // Return the hash for debug escape analysis.
322 std::string
323 debug_escape_hash() const
324 { return this->debug_escape_hash_; }
326 // Set the hash value for debug escape analysis.
327 void
328 set_debug_escape_hash(const std::string& s)
329 { this->debug_escape_hash_ = s; }
331 // Return the size threshold used to determine whether to issue
332 // a nil-check for a given pointer dereference. A threshold of -1
333 // implies that all potentially faulting dereference ops should
334 // be nil-checked. A positive threshold of N implies that a deref
335 // of *P where P has size less than N doesn't need a nil check.
336 int64_t
337 nil_check_size_threshold() const
338 { return this->nil_check_size_threshold_; }
340 // Set the nil-check size threshold, as described above.
341 void
342 set_nil_check_size_threshold(int64_t bytes)
343 { this->nil_check_size_threshold_ = bytes; }
345 // Import a package. FILENAME is the file name argument, LOCAL_NAME
346 // is the local name to give to the package. If LOCAL_NAME is empty
347 // the declarations are added to the global scope.
348 void
349 import_package(const std::string& filename, const std::string& local_name,
350 bool is_local_name_exported, bool must_exist, Location);
352 // Whether we are the global binding level.
353 bool
354 in_global_scope() const;
356 // Look up a name in the current binding contours.
357 Named_object*
358 lookup(const std::string&, Named_object** pfunction) const;
360 // Look up a name in the current block.
361 Named_object*
362 lookup_in_block(const std::string&) const;
364 // Look up a name in the global namespace--the universal scope.
365 Named_object*
366 lookup_global(const char*) const;
368 // Add a new imported package. REAL_NAME is the real name of the
369 // package. ALIAS is the alias of the package; this may be the same
370 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
371 // this package should be added to the global namespace; this is
372 // true if the alias is ".". LOCATION is the location of the import
373 // statement. This returns the new package, or NULL on error.
374 Package*
375 add_imported_package(const std::string& real_name, const std::string& alias,
376 bool is_alias_exported,
377 const std::string& pkgpath,
378 const std::string& pkgpath_symbol,
379 Location location,
380 bool* padd_to_globals);
382 // Register a package. This package may or may not be imported.
383 // This returns the Package structure for the package, creating if
384 // it necessary.
385 Package*
386 register_package(const std::string& pkgpath,
387 const std::string& pkgpath_symbol, Location);
389 // Look up a package by pkgpath, and return its pkgpath_symbol.
390 std::string
391 pkgpath_symbol_for_package(const std::string&);
393 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
394 // method function should be added to the type of its receiver.
395 Named_object*
396 start_function(const std::string& name, Function_type* type,
397 bool add_method_to_type, Location);
399 // Finish compiling a function.
400 void
401 finish_function(Location);
403 // Return the current function.
404 Named_object*
405 current_function() const;
407 // Return the current block.
408 Block*
409 current_block();
411 // Start a new block. This is not initially associated with a
412 // function.
413 void
414 start_block(Location);
416 // Finish the current block and return it.
417 Block*
418 finish_block(Location);
420 // Declare an erroneous name. This is used to avoid knock-on errors
421 // after a parsing error.
422 Named_object*
423 add_erroneous_name(const std::string& name);
425 // Declare an unknown name. This is used while parsing. The name
426 // must be resolved by the end of the parse. Unknown names are
427 // always added at the package level.
428 Named_object*
429 add_unknown_name(const std::string& name, Location);
431 // Declare a function.
432 Named_object*
433 declare_function(const std::string&, Function_type*, Location);
435 // Declare a function at the package level. This is used for
436 // functions generated for a type.
437 Named_object*
438 declare_package_function(const std::string&, Function_type*, Location);
440 // Add a label.
441 Label*
442 add_label_definition(const std::string&, Location);
444 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
445 // report errors for a goto from the current location to the label
446 // location.
447 Label*
448 add_label_reference(const std::string&, Location,
449 bool issue_goto_errors);
451 // An analysis set is a list of functions paired with a boolean that indicates
452 // whether the list of functions are recursive.
453 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
455 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
456 // package.
457 void
458 add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
459 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
461 // Return a snapshot of the current binding state.
462 Bindings_snapshot*
463 bindings_snapshot(Location);
465 // Add a statement to the current block.
466 void
467 add_statement(Statement*);
469 // Add a block to the current block.
470 void
471 add_block(Block*, Location);
473 // Add a constant.
474 Named_object*
475 add_constant(const Typed_identifier&, Expression*, int iota_value);
477 // Add a type.
478 void
479 add_type(const std::string&, Type*, Location);
481 // Add a named type. This is used for builtin types, and to add an
482 // imported type to the global scope.
483 void
484 add_named_type(Named_type*);
486 // Declare a type.
487 Named_object*
488 declare_type(const std::string&, Location);
490 // Declare a type at the package level. This is used when the
491 // parser sees an unknown name where a type name is required.
492 Named_object*
493 declare_package_type(const std::string&, Location);
495 // Define a type which was already declared.
496 void
497 define_type(Named_object*, Named_type*);
499 // Add a variable.
500 Named_object*
501 add_variable(const std::string&, Variable*);
503 // Add a sink--a reference to the blank identifier _.
504 Named_object*
505 add_sink();
507 // Add a type which needs to be verified. This is used for sink
508 // types, just to give appropriate error messages.
509 void
510 add_type_to_verify(Type* type);
512 // Add a named object to the current namespace. This is used for
513 // import . "package".
514 void
515 add_dot_import_object(Named_object*);
517 // Add an identifier to the list of names seen in the file block.
518 void
519 add_file_block_name(const std::string& name, Location location)
520 { this->file_block_names_[name] = location; }
522 // Add a linkname, from the go:linkname compiler directive. This
523 // changes the externally visible name of go_name to be ext_name.
524 void
525 add_linkname(const std::string& go_name, bool is_exported,
526 const std::string& ext_name, Location location);
528 // Mark all local variables in current bindings as used. This is
529 // used when there is a parse error to avoid useless errors.
530 void
531 mark_locals_used();
533 // Note that we've seen an interface type. This is used to build
534 // all required interface method tables.
535 void
536 record_interface_type(Interface_type*);
538 // Note that we need an initialization function.
539 void
540 set_need_init_fn()
541 { this->need_init_fn_ = true; }
543 // Return whether the current file imported the unsafe package.
544 bool
545 current_file_imported_unsafe() const
546 { return this->current_file_imported_unsafe_; }
548 // Clear out all names in file scope. This is called when we start
549 // parsing a new file.
550 void
551 clear_file_scope();
553 // Record that VAR1 must be initialized after VAR2. This is used
554 // when VAR2 does not appear in VAR1's INIT or PREINIT.
555 void
556 record_var_depends_on(Variable* var1, Named_object* var2)
558 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
559 this->var_deps_[var1] = var2;
562 // Return the variable that VAR depends on, or NULL if none.
563 Named_object*
564 var_depends_on(Variable* var) const
566 Var_deps::const_iterator p = this->var_deps_.find(var);
567 return p != this->var_deps_.end() ? p->second : NULL;
570 // Queue up a type-specific function to be written out. This is
571 // used when a type-specific function is needed when not at the top
572 // level.
573 void
574 queue_specific_type_function(Type* type, Named_type* name, int64_t size,
575 const std::string& hash_name,
576 Function_type* hash_fntype,
577 const std::string& equal_name,
578 Function_type* equal_fntype);
580 // Write out queued specific type functions.
581 void
582 write_specific_type_functions();
584 // Whether we are done writing out specific type functions.
585 bool
586 specific_type_functions_are_written() const
587 { return this->specific_type_functions_are_written_; }
589 // Add a pointer that needs to be added to the list of objects
590 // traversed by the garbage collector. This should be an expression
591 // of pointer type that points to static storage. It's not
592 // necessary to add global variables to this list, just global
593 // variable initializers that would otherwise not be seen.
594 void
595 add_gc_root(Expression* expr)
597 this->set_need_init_fn();
598 this->gc_roots_.push_back(expr);
601 // Traverse the tree. See the Traverse class.
602 void
603 traverse(Traverse*);
605 // Define the predeclared global names.
606 void
607 define_global_names();
609 // Verify and complete all types.
610 void
611 verify_types();
613 // Lower the parse tree.
614 void
615 lower_parse_tree();
617 // Lower all the statements in a block.
618 void
619 lower_block(Named_object* function, Block*);
621 // Lower an expression.
622 void
623 lower_expression(Named_object* function, Statement_inserter*, Expression**);
625 // Lower a constant.
626 void
627 lower_constant(Named_object*);
629 // Flatten all the statements in a block.
630 void
631 flatten_block(Named_object* function, Block*);
633 // Flatten an expression.
634 void
635 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
637 // Create all necessary function descriptors.
638 void
639 create_function_descriptors();
641 // Finalize the method lists and build stub methods for named types.
642 void
643 finalize_methods();
645 // Work out the types to use for unspecified variables and
646 // constants.
647 void
648 determine_types();
650 // Type check the program.
651 void
652 check_types();
654 // Check the types in a single block. This is used for complicated
655 // go statements.
656 void
657 check_types_in_block(Block*);
659 // Check for return statements.
660 void
661 check_return_statements();
663 // Analyze the program flow for escape information.
664 void
665 analyze_escape();
667 // Discover the groups of possibly recursive functions in this package.
668 void
669 discover_analysis_sets();
671 // Build a connectivity graph between the objects in each analyzed function.
672 void
673 assign_connectivity(Escape_context*, Named_object*);
675 // Traverse the objects in the connecitivty graph from the sink, adjusting the
676 // escape levels of each object.
677 void
678 propagate_escape(Escape_context*, Node*);
680 // Add notes about the escape level of a function's input and output
681 // parameters for exporting and importing top level functions.
682 void
683 tag_function(Escape_context*, Named_object*);
685 // Do all exports.
686 void
687 do_exports();
689 // Add an import control function for an imported package to the
690 // list.
691 void
692 add_import_init_fn(const std::string& package_name,
693 const std::string& init_name, int prio);
695 // Return the Import_init for a given init name.
696 Import_init*
697 lookup_init(const std::string& init_name);
699 // Turn short-cut operators (&&, ||) into explicit if statements.
700 void
701 remove_shortcuts();
703 // Use temporary variables to force order of evaluation.
704 void
705 order_evaluations();
707 // Add write barriers as needed.
708 void
709 add_write_barriers();
711 // Return whether an assignment that sets LHS to RHS needs a write
712 // barrier.
713 bool
714 assign_needs_write_barrier(Expression* lhs);
716 // Return an assignment that sets LHS to RHS using a write barrier.
717 // This returns an if statement that checks whether write barriers
718 // are enabled. If not, it does LHS = RHS, otherwise it calls the
719 // appropriate write barrier function.
720 Statement*
721 assign_with_write_barrier(Function*, Block*, Statement_inserter*,
722 Expression* lhs, Expression* rhs, Location);
724 // Flatten parse tree.
725 void
726 flatten();
728 // Build thunks for functions which call recover.
729 void
730 build_recover_thunks();
732 // Return a declaration for __builtin_return_address or
733 // __builtin_frame_address.
734 static Named_object*
735 declare_builtin_rf_address(const char* name);
737 // Simplify statements which might use thunks: go and defer
738 // statements.
739 void
740 simplify_thunk_statements();
742 // Dump AST if -fgo-dump-ast is set
743 void
744 dump_ast(const char* basename);
746 // Dump Call Graph if -fgo-dump-calls is set.
747 void
748 dump_call_graph(const char* basename);
750 // Dump Connection Graphs if -fgo-dump-connections is set.
751 void
752 dump_connection_graphs(const char* basename);
754 // Convert named types to the backend representation.
755 void
756 convert_named_types();
758 // Convert named types in a list of bindings.
759 void
760 convert_named_types_in_bindings(Bindings*);
762 // True if named types have been converted to the backend
763 // representation.
764 bool
765 named_types_are_converted() const
766 { return this->named_types_are_converted_; }
768 // Give an error if the initialization of VAR depends on itself.
769 void
770 check_self_dep(Named_object*);
772 // Write out the global values.
773 void
774 write_globals();
776 // Build a call to the runtime error function.
777 Expression*
778 runtime_error(int code, Location);
780 // Build required interface method tables.
781 void
782 build_interface_method_tables();
784 // Return an expression which allocates memory to hold values of type TYPE.
785 Expression*
786 allocate_memory(Type *type, Location);
788 // Return the assembler name to use for an exported function, a
789 // method, or a function/method declaration.
790 std::string
791 function_asm_name(const std::string& go_name, const Package*,
792 const Type* receiver);
794 // Return the name to use for a function descriptor.
795 std::string
796 function_descriptor_name(Named_object*);
798 // Return the name to use for a generated stub method.
799 std::string
800 stub_method_name(const std::string& method_name);
802 // Return the names of the hash and equality functions for TYPE.
803 void
804 specific_type_function_names(const Type*, const Named_type*,
805 std::string* hash_name,
806 std::string* equal_name);
808 // Return the assembler name to use for a global variable.
809 std::string
810 global_var_asm_name(const std::string& go_name, const Package*);
812 // Return a name to use for an error case. This should only be used
813 // after reporting an error, and is used to avoid useless knockon
814 // errors.
815 static std::string
816 erroneous_name();
818 // Return whether the name indicates an error.
819 static bool
820 is_erroneous_name(const std::string&);
822 // Return a name to use for a thunk function. A thunk function is
823 // one we create during the compilation, for a go statement or a
824 // defer statement or a method expression.
825 static std::string
826 thunk_name();
828 // Return whether an object is a thunk.
829 static bool
830 is_thunk(const Named_object*);
832 // Return the name to use for an init function.
833 std::string
834 init_function_name();
836 // Return the name to use for a nested function.
837 static std::string
838 nested_function_name();
840 // Return the name to use for a sink funciton.
841 std::string
842 sink_function_name();
844 // Return the name to use for an (erroneous) redefined function.
845 std::string
846 redefined_function_name();
848 // Return the name for use for a recover thunk.
849 std::string
850 recover_thunk_name(const std::string& name, const Type* rtype);
852 // Return the name to use for the GC root variable.
853 std::string
854 gc_root_name();
856 // Return the name to use for a composite literal or string
857 // initializer.
858 std::string
859 initializer_name();
861 // Return the name of the variable used to represent the zero value
862 // of a map.
863 std::string
864 map_zero_value_name();
866 // Get the name of the magic initialization function.
867 const std::string&
868 get_init_fn_name();
870 // Return the name for a type descriptor symbol.
871 std::string
872 type_descriptor_name(Type*, Named_type*);
874 // Return the assembler name for the GC symbol for a type.
875 std::string
876 gc_symbol_name(Type*);
878 // Return the assembler name for a ptrmask variable.
879 std::string
880 ptrmask_symbol_name(const std::string& ptrmask_sym_name);
882 // Return the name to use for an interface method table.
883 std::string
884 interface_method_table_name(Interface_type*, Type*, bool is_pointer);
886 private:
887 // During parsing, we keep a stack of functions. Each function on
888 // the stack is one that we are currently parsing. For each
889 // function, we keep track of the current stack of blocks.
890 struct Open_function
892 // The function.
893 Named_object* function;
894 // The stack of active blocks in the function.
895 std::vector<Block*> blocks;
898 // The stack of functions.
899 typedef std::vector<Open_function> Open_functions;
901 // Set up the built-in unsafe package.
902 void
903 import_unsafe(const std::string&, bool is_exported, Location);
905 // Return the current binding contour.
906 Bindings*
907 current_bindings();
909 const Bindings*
910 current_bindings() const;
912 void
913 write_c_header();
915 // Get the decl for the magic initialization function.
916 Named_object*
917 initialization_function_decl();
919 // Create the magic initialization function.
920 Named_object*
921 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
923 // Initialize imported packages. BFUNCTION is the function
924 // into which the package init calls will be placed.
925 void
926 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
928 // Register variables with the garbage collector.
929 void
930 register_gc_vars(const std::vector<Named_object*>&,
931 std::vector<Bstatement*>&,
932 Bfunction* init_bfunction);
934 Named_object*
935 write_barrier_variable();
937 Statement*
938 check_write_barrier(Block*, Statement*, Statement*);
940 // Type used to map import names to packages.
941 typedef std::map<std::string, Package*> Imports;
943 // Type used to map package names to packages.
944 typedef std::map<std::string, Package*> Packages;
946 // Type used to map variables to the function calls that set them.
947 // This is used for initialization dependency analysis.
948 typedef std::map<Variable*, Named_object*> Var_deps;
950 // Type used to map identifiers in the file block to the location
951 // where they were defined.
952 typedef Unordered_map(std::string, Location) File_block_names;
954 // Type used to queue writing a type specific function.
955 struct Specific_type_function
957 Type* type;
958 Named_type* name;
959 int64_t size;
960 std::string hash_name;
961 Function_type* hash_fntype;
962 std::string equal_name;
963 Function_type* equal_fntype;
965 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
966 const std::string& ahash_name,
967 Function_type* ahash_fntype,
968 const std::string& aequal_name,
969 Function_type* aequal_fntype)
970 : type(atype), name(aname), size(asize), hash_name(ahash_name),
971 hash_fntype(ahash_fntype), equal_name(aequal_name),
972 equal_fntype(aequal_fntype)
976 // Recompute init priorities.
977 void
978 recompute_init_priorities();
980 // Recursive helper used by the routine above.
981 void
982 update_init_priority(Import_init* ii,
983 std::set<const Import_init *>* visited);
985 // The backend generator.
986 Backend* backend_;
987 // The object used to keep track of file names and line numbers.
988 Linemap* linemap_;
989 // The package we are compiling.
990 Package* package_;
991 // The list of currently open functions during parsing.
992 Open_functions functions_;
993 // The global binding contour. This includes the builtin functions
994 // and the package we are compiling.
995 Bindings* globals_;
996 // The list of names we have seen in the file block.
997 File_block_names file_block_names_;
998 // Mapping from import file names to packages.
999 Imports imports_;
1000 // Whether the magic unsafe package was imported.
1001 bool imported_unsafe_;
1002 // Whether the magic unsafe package was imported by the current file.
1003 bool current_file_imported_unsafe_;
1004 // Mapping from package names we have seen to packages. This does
1005 // not include the package we are compiling.
1006 Packages packages_;
1007 // The functions named "init", if there are any.
1008 std::vector<Named_object*> init_functions_;
1009 // A mapping from variables to the function calls that initialize
1010 // them, if it is not stored in the variable's init or preinit.
1011 // This is used for dependency analysis.
1012 Var_deps var_deps_;
1013 // Whether we need a magic initialization function.
1014 bool need_init_fn_;
1015 // The name of the magic initialization function.
1016 std::string init_fn_name_;
1017 // A list of import control variables for packages that we import.
1018 Import_init_set imported_init_fns_;
1019 // The package path used for reflection data.
1020 std::string pkgpath_;
1021 // The package path to use for a symbol name.
1022 std::string pkgpath_symbol_;
1023 // The prefix to use for symbols, from the -fgo-prefix option.
1024 std::string prefix_;
1025 // Whether pkgpath_ has been set.
1026 bool pkgpath_set_;
1027 // Whether an explicit package path was set by -fgo-pkgpath.
1028 bool pkgpath_from_option_;
1029 // Whether an explicit prefix was set by -fgo-prefix.
1030 bool prefix_from_option_;
1031 // The relative import path, from the -fgo-relative-import-path
1032 // option.
1033 std::string relative_import_path_;
1034 // The C header file to write, from the -fgo-c-header option.
1035 std::string c_header_;
1036 // Whether or not to check for division by zero, from the
1037 // -fgo-check-divide-zero option.
1038 bool check_divide_by_zero_;
1039 // Whether or not to check for division overflow, from the
1040 // -fgo-check-divide-overflow option.
1041 bool check_divide_overflow_;
1042 // Whether we are compiling the runtime package, from the
1043 // -fgo-compiling-runtime option.
1044 bool compiling_runtime_;
1045 // The level of escape analysis debug information to emit, from the
1046 // -fgo-debug-escape option.
1047 int debug_escape_level_;
1048 // A hash value for debug escape analysis, from the
1049 // -fgo-debug-escape-hash option. The analysis is run only on
1050 // functions with names that hash to the matching value.
1051 std::string debug_escape_hash_;
1052 // Nil-check size threshhold.
1053 int64_t nil_check_size_threshold_;
1054 // A list of types to verify.
1055 std::vector<Type*> verify_types_;
1056 // A list of interface types defined while parsing.
1057 std::vector<Interface_type*> interface_types_;
1058 // Type specific functions to write out.
1059 std::vector<Specific_type_function*> specific_type_functions_;
1060 // Whether we are done writing out specific type functions.
1061 bool specific_type_functions_are_written_;
1062 // Whether named types have been converted.
1063 bool named_types_are_converted_;
1064 // A list containing groups of possibly mutually recursive functions to be
1065 // considered during escape analysis.
1066 std::vector<Analysis_set> analysis_sets_;
1067 // A list of objects to add to the GC roots.
1068 std::vector<Expression*> gc_roots_;
1071 // A block of statements.
1073 class Block
1075 public:
1076 Block(Block* enclosing, Location);
1078 // Return the enclosing block.
1079 const Block*
1080 enclosing() const
1081 { return this->enclosing_; }
1083 // Return the bindings of the block.
1084 Bindings*
1085 bindings()
1086 { return this->bindings_; }
1088 const Bindings*
1089 bindings() const
1090 { return this->bindings_; }
1092 // Look at the block's statements.
1093 const std::vector<Statement*>*
1094 statements() const
1095 { return &this->statements_; }
1097 // Return the start location. This is normally the location of the
1098 // left curly brace which starts the block.
1099 Location
1100 start_location() const
1101 { return this->start_location_; }
1103 // Return the end location. This is normally the location of the
1104 // right curly brace which ends the block.
1105 Location
1106 end_location() const
1107 { return this->end_location_; }
1109 // Add a statement to the block.
1110 void
1111 add_statement(Statement*);
1113 // Add a statement to the front of the block.
1114 void
1115 add_statement_at_front(Statement*);
1117 // Replace a statement in a block.
1118 void
1119 replace_statement(size_t index, Statement*);
1121 // Add a Statement before statement number INDEX.
1122 void
1123 insert_statement_before(size_t index, Statement*);
1125 // Add a Statement after statement number INDEX.
1126 void
1127 insert_statement_after(size_t index, Statement*);
1129 // Set the end location of the block.
1130 void
1131 set_end_location(Location location)
1132 { this->end_location_ = location; }
1134 // Traverse the tree.
1136 traverse(Traverse*);
1138 // Set final types for unspecified variables and constants.
1139 void
1140 determine_types();
1142 // Return true if execution of this block may fall through to the
1143 // next block.
1144 bool
1145 may_fall_through() const;
1147 // Convert the block to the backend representation.
1148 Bblock*
1149 get_backend(Translate_context*);
1151 // Iterate over statements.
1153 typedef std::vector<Statement*>::iterator iterator;
1155 iterator
1156 begin()
1157 { return this->statements_.begin(); }
1159 iterator
1160 end()
1161 { return this->statements_.end(); }
1163 private:
1164 // Enclosing block.
1165 Block* enclosing_;
1166 // Statements in the block.
1167 std::vector<Statement*> statements_;
1168 // Binding contour.
1169 Bindings* bindings_;
1170 // Location of start of block.
1171 Location start_location_;
1172 // Location of end of block.
1173 Location end_location_;
1176 // A function.
1178 class Function
1180 public:
1181 Function(Function_type* type, Named_object*, Block*, Location);
1183 // Return the function's type.
1184 Function_type*
1185 type() const
1186 { return this->type_; }
1188 // Return the enclosing function if there is one.
1189 Named_object*
1190 enclosing() const
1191 { return this->enclosing_; }
1193 // Set the enclosing function. This is used when building thunks
1194 // for functions which call recover.
1195 void
1196 set_enclosing(Named_object* enclosing)
1198 go_assert(this->enclosing_ == NULL);
1199 this->enclosing_ = enclosing;
1202 // The result variables.
1203 typedef std::vector<Named_object*> Results;
1205 // Create the result variables in the outer block.
1206 void
1207 create_result_variables(Gogo*);
1209 // Update the named result variables when cloning a function which
1210 // calls recover.
1211 void
1212 update_result_variables();
1214 // Return the result variables.
1215 Results*
1216 result_variables()
1217 { return this->results_; }
1219 bool
1220 is_sink() const
1221 { return this->is_sink_; }
1223 void
1224 set_is_sink()
1225 { this->is_sink_ = true; }
1227 // Whether the result variables have names.
1228 bool
1229 results_are_named() const
1230 { return this->results_are_named_; }
1232 // Set the assembler name.
1233 void
1234 set_asm_name(const std::string& asm_name)
1235 { this->asm_name_ = asm_name; }
1237 // Return the pragmas for this function.
1238 unsigned int
1239 pragmas() const
1240 { return this->pragmas_; }
1242 // Set the pragmas for this function.
1243 void
1244 set_pragmas(unsigned int pragmas)
1246 this->pragmas_ = pragmas;
1249 // Whether this method should not be included in the type
1250 // descriptor.
1251 bool
1252 nointerface() const;
1254 // Record that this method should not be included in the type
1255 // descriptor.
1256 void
1257 set_nointerface();
1259 // Record that this function is a stub method created for an unnamed
1260 // type.
1261 void
1262 set_is_unnamed_type_stub_method()
1264 go_assert(this->is_method());
1265 this->is_unnamed_type_stub_method_ = true;
1268 // Return the amount of enclosed variables in this closure.
1269 size_t
1270 closure_field_count() const
1271 { return this->closure_fields_.size(); }
1273 // Add a new field to the closure variable.
1274 void
1275 add_closure_field(Named_object* var, Location loc)
1276 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1278 // Whether this function needs a closure.
1279 bool
1280 needs_closure() const
1281 { return !this->closure_fields_.empty(); }
1283 // Return the closure variable, creating it if necessary. This is
1284 // passed to the function as a static chain parameter.
1285 Named_object*
1286 closure_var();
1288 // Set the closure variable. This is used when building thunks for
1289 // functions which call recover.
1290 void
1291 set_closure_var(Named_object* v)
1293 go_assert(this->closure_var_ == NULL);
1294 this->closure_var_ = v;
1297 // Return the variable for a reference to field INDEX in the closure
1298 // variable.
1299 Named_object*
1300 enclosing_var(unsigned int index)
1302 go_assert(index < this->closure_fields_.size());
1303 return closure_fields_[index].first;
1306 // Set the type of the closure variable if there is one.
1307 void
1308 set_closure_type();
1310 // Get the block of statements associated with the function.
1311 Block*
1312 block() const
1313 { return this->block_; }
1315 // Get the location of the start of the function.
1316 Location
1317 location() const
1318 { return this->location_; }
1320 // Return whether this function is actually a method.
1321 bool
1322 is_method() const;
1324 // Add a label definition to the function.
1325 Label*
1326 add_label_definition(Gogo*, const std::string& label_name, Location);
1328 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1329 // if we should report errors for a goto from the current location
1330 // to the label location.
1331 Label*
1332 add_label_reference(Gogo*, const std::string& label_name,
1333 Location, bool issue_goto_errors);
1335 // Warn about labels that are defined but not used.
1336 void
1337 check_labels() const;
1339 // Note that a new local type has been added. Return its index.
1340 unsigned int
1341 new_local_type_index()
1342 { return this->local_type_count_++; }
1344 // Whether this function calls the predeclared recover function.
1345 bool
1346 calls_recover() const
1347 { return this->calls_recover_; }
1349 // Record that this function calls the predeclared recover function.
1350 // This is set during the lowering pass.
1351 void
1352 set_calls_recover()
1353 { this->calls_recover_ = true; }
1355 // Whether this is a recover thunk function.
1356 bool
1357 is_recover_thunk() const
1358 { return this->is_recover_thunk_; }
1360 // Record that this is a thunk built for a function which calls
1361 // recover.
1362 void
1363 set_is_recover_thunk()
1364 { this->is_recover_thunk_ = true; }
1366 // Whether this function already has a recover thunk.
1367 bool
1368 has_recover_thunk() const
1369 { return this->has_recover_thunk_; }
1371 // Record that this function already has a recover thunk.
1372 void
1373 set_has_recover_thunk()
1374 { this->has_recover_thunk_ = true; }
1376 // Record that this function is a thunk created for a defer
1377 // statement that calls the __go_set_defer_retaddr runtime function.
1378 void
1379 set_calls_defer_retaddr()
1380 { this->calls_defer_retaddr_ = true; }
1382 // Whether this is a type hash or equality function created by the
1383 // compiler.
1384 bool
1385 is_type_specific_function()
1386 { return this->is_type_specific_function_; }
1388 // Record that this function is a type hash or equality function
1389 // created by the compiler.
1390 void
1391 set_is_type_specific_function()
1392 { this->is_type_specific_function_ = true; }
1394 // Mark the function as going into a unique section.
1395 void
1396 set_in_unique_section()
1397 { this->in_unique_section_ = true; }
1399 // Swap with another function. Used only for the thunk which calls
1400 // recover.
1401 void
1402 swap_for_recover(Function *);
1404 // Traverse the tree.
1406 traverse(Traverse*);
1408 // Determine types in the function.
1409 void
1410 determine_types();
1412 // Return an expression for the function descriptor, given the named
1413 // object for this function. This may only be called for functions
1414 // without a closure. This will be an immutable struct with one
1415 // field that points to the function's code.
1416 Expression*
1417 descriptor(Gogo*, Named_object*);
1419 // Set the descriptor for this function. This is used when a
1420 // function declaration is followed by a function definition.
1421 void
1422 set_descriptor(Expression* descriptor)
1424 go_assert(this->descriptor_ == NULL);
1425 this->descriptor_ = descriptor;
1428 // Return the backend representation.
1429 Bfunction*
1430 get_or_make_decl(Gogo*, Named_object*);
1432 // Return the function's decl after it has been built.
1433 Bfunction*
1434 get_decl() const;
1436 // Set the function decl to hold a backend representation of the function
1437 // code.
1438 void
1439 build(Gogo*, Named_object*);
1441 // Get the statement that assigns values to this function's result struct.
1442 Bstatement*
1443 return_value(Gogo*, Named_object*, Location) const;
1445 // Get an expression for the variable holding the defer stack.
1446 Expression*
1447 defer_stack(Location);
1449 // Export the function.
1450 void
1451 export_func(Export*, const std::string& name) const;
1453 // Export a function with a type.
1454 static void
1455 export_func_with_type(Export*, const std::string& name,
1456 const Function_type*);
1458 // Import a function.
1459 static void
1460 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1461 Typed_identifier_list** pparameters,
1462 Typed_identifier_list** presults, bool* is_varargs);
1464 private:
1465 // Type for mapping from label names to Label objects.
1466 typedef Unordered_map(std::string, Label*) Labels;
1468 void
1469 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1471 typedef std::vector<std::pair<Named_object*,
1472 Location> > Closure_fields;
1474 // The function's type.
1475 Function_type* type_;
1476 // The enclosing function. This is NULL when there isn't one, which
1477 // is the normal case.
1478 Named_object* enclosing_;
1479 // The result variables, if any.
1480 Results* results_;
1481 // If there is a closure, this is the list of variables which appear
1482 // in the closure. This is created by the parser, and then resolved
1483 // to a real type when we lower parse trees.
1484 Closure_fields closure_fields_;
1485 // The closure variable, passed as a parameter using the static
1486 // chain parameter. Normally NULL.
1487 Named_object* closure_var_;
1488 // The outer block of statements in the function.
1489 Block* block_;
1490 // The source location of the start of the function.
1491 Location location_;
1492 // Labels defined or referenced in the function.
1493 Labels labels_;
1494 // The number of local types defined in this function.
1495 unsigned int local_type_count_;
1496 // The assembler name: this is the name that will be put in the object file.
1497 // Set by the go:linkname compiler directive. This is normally empty.
1498 std::string asm_name_;
1499 // The function descriptor, if any.
1500 Expression* descriptor_;
1501 // The function decl.
1502 Bfunction* fndecl_;
1503 // The defer stack variable. A pointer to this variable is used to
1504 // distinguish the defer stack for one function from another. This
1505 // is NULL unless we actually need a defer stack.
1506 Temporary_statement* defer_stack_;
1507 // Pragmas for this function. This is a set of GOPRAGMA bits.
1508 unsigned int pragmas_;
1509 // True if this function is sink-named. No code is generated.
1510 bool is_sink_ : 1;
1511 // True if the result variables are named.
1512 bool results_are_named_ : 1;
1513 // True if this function is a stub method created for an unnamed
1514 // type.
1515 bool is_unnamed_type_stub_method_ : 1;
1516 // True if this function calls the predeclared recover function.
1517 bool calls_recover_ : 1;
1518 // True if this a thunk built for a function which calls recover.
1519 bool is_recover_thunk_ : 1;
1520 // True if this function already has a recover thunk.
1521 bool has_recover_thunk_ : 1;
1522 // True if this is a thunk built for a defer statement that calls
1523 // the __go_set_defer_retaddr runtime function.
1524 bool calls_defer_retaddr_ : 1;
1525 // True if this is a function built by the compiler to as a hash or
1526 // equality function for some type.
1527 bool is_type_specific_function_ : 1;
1528 // True if this function should be put in a unique section. This is
1529 // turned on for field tracking.
1530 bool in_unique_section_ : 1;
1533 // A snapshot of the current binding state.
1535 class Bindings_snapshot
1537 public:
1538 Bindings_snapshot(const Block*, Location);
1540 // Report any errors appropriate for a goto from the current binding
1541 // state of B to this one.
1542 void
1543 check_goto_from(const Block* b, Location);
1545 // Report any errors appropriate for a goto from this binding state
1546 // to the current state of B.
1547 void
1548 check_goto_to(const Block* b);
1550 private:
1551 bool
1552 check_goto_block(Location, const Block*, const Block*, size_t*);
1554 void
1555 check_goto_defs(Location, const Block*, size_t, size_t);
1557 // The current block.
1558 const Block* block_;
1559 // The number of names currently defined in each open block.
1560 // Element 0 is this->block_, element 1 is
1561 // this->block_->enclosing(), etc.
1562 std::vector<size_t> counts_;
1563 // The location where this snapshot was taken.
1564 Location location_;
1567 // A function declaration.
1569 class Function_declaration
1571 public:
1572 Function_declaration(Function_type* fntype, Location location)
1573 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1574 fndecl_(NULL), pragmas_(0)
1577 Function_type*
1578 type() const
1579 { return this->fntype_; }
1581 Location
1582 location() const
1583 { return this->location_; }
1585 const std::string&
1586 asm_name() const
1587 { return this->asm_name_; }
1589 // Set the assembler name.
1590 void
1591 set_asm_name(const std::string& asm_name)
1592 { this->asm_name_ = asm_name; }
1594 // Return the pragmas for this function.
1595 unsigned int
1596 pragmas() const
1597 { return this->pragmas_; }
1599 // Set the pragmas for this function.
1600 void
1601 set_pragmas(unsigned int pragmas)
1603 this->pragmas_ = pragmas;
1606 // Return an expression for the function descriptor, given the named
1607 // object for this function. This may only be called for functions
1608 // without a closure. This will be an immutable struct with one
1609 // field that points to the function's code.
1610 Expression*
1611 descriptor(Gogo*, Named_object*);
1613 // Return true if we have created a descriptor for this declaration.
1614 bool
1615 has_descriptor() const
1616 { return this->descriptor_ != NULL; }
1618 // Return a backend representation.
1619 Bfunction*
1620 get_or_make_decl(Gogo*, Named_object*);
1622 // If there is a descriptor, build it into the backend
1623 // representation.
1624 void
1625 build_backend_descriptor(Gogo*);
1627 // Export a function declaration.
1628 void
1629 export_func(Export* exp, const std::string& name) const
1630 { Function::export_func_with_type(exp, name, this->fntype_); }
1632 // Check that the types used in this declaration's signature are defined.
1633 void
1634 check_types() const;
1636 private:
1637 // The type of the function.
1638 Function_type* fntype_;
1639 // The location of the declaration.
1640 Location location_;
1641 // The assembler name: this is the name to use in references to the
1642 // function. This is normally empty.
1643 std::string asm_name_;
1644 // The function descriptor, if any.
1645 Expression* descriptor_;
1646 // The function decl if needed.
1647 Bfunction* fndecl_;
1648 // Pragmas for this function. This is a set of GOPRAGMA bits.
1649 unsigned int pragmas_;
1652 // A variable.
1654 class Variable
1656 public:
1657 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1658 bool is_receiver, Location);
1660 // Get the type of the variable.
1661 Type*
1662 type();
1664 Type*
1665 type() const;
1667 // Return whether the type is defined yet.
1668 bool
1669 has_type() const;
1671 // Get the initial value.
1672 Expression*
1673 init() const
1674 { return this->init_; }
1676 // Return whether there are any preinit statements.
1677 bool
1678 has_pre_init() const
1679 { return this->preinit_ != NULL; }
1681 // Return the preinit statements if any.
1682 Block*
1683 preinit() const
1684 { return this->preinit_; }
1686 // Return whether this is a global variable.
1687 bool
1688 is_global() const
1689 { return this->is_global_; }
1691 // Return whether this is a function parameter.
1692 bool
1693 is_parameter() const
1694 { return this->is_parameter_; }
1696 // Return whether this is a closure (static chain) parameter.
1697 bool
1698 is_closure() const
1699 { return this->is_closure_; }
1701 // Change this parameter to be a closure.
1702 void
1703 set_is_closure()
1705 this->is_closure_ = true;
1708 // Return whether this is the receiver parameter of a method.
1709 bool
1710 is_receiver() const
1711 { return this->is_receiver_; }
1713 // Change this parameter to be a receiver. This is used when
1714 // creating the thunks created for functions which call recover.
1715 void
1716 set_is_receiver()
1718 go_assert(this->is_parameter_);
1719 this->is_receiver_ = true;
1722 // Change this parameter to not be a receiver. This is used when
1723 // creating the thunks created for functions which call recover.
1724 void
1725 set_is_not_receiver()
1727 go_assert(this->is_parameter_);
1728 this->is_receiver_ = false;
1731 // Return whether this is the varargs parameter of a function.
1732 bool
1733 is_varargs_parameter() const
1734 { return this->is_varargs_parameter_; }
1736 // Whether this variable's address is taken.
1737 bool
1738 is_address_taken() const
1739 { return this->is_address_taken_; }
1741 // Whether this variable should live in the heap.
1742 bool
1743 is_in_heap() const
1745 return this->is_address_taken_
1746 && this->escapes_
1747 && !this->is_global_;
1750 // Note that something takes the address of this variable.
1751 void
1752 set_address_taken()
1753 { this->is_address_taken_ = true; }
1755 // Return whether the address is taken but does not escape.
1756 bool
1757 is_non_escaping_address_taken() const
1758 { return this->is_non_escaping_address_taken_; }
1760 // Note that something takes the address of this variable such that
1761 // the address does not escape the function.
1762 void
1763 set_non_escaping_address_taken()
1764 { this->is_non_escaping_address_taken_ = true; }
1766 // Return whether this variable escapes the function it is declared in.
1767 bool
1768 escapes()
1769 { return this->escapes_; }
1771 // Note that this variable does not escape the function it is declared in.
1772 void
1773 set_does_not_escape()
1774 { this->escapes_ = false; }
1776 // Get the source location of the variable's declaration.
1777 Location
1778 location() const
1779 { return this->location_; }
1781 // Record that this is the varargs parameter of a function.
1782 void
1783 set_is_varargs_parameter()
1785 go_assert(this->is_parameter_);
1786 this->is_varargs_parameter_ = true;
1789 // Return whether the variable has been used.
1790 bool
1791 is_used() const
1792 { return this->is_used_; }
1794 // Mark that the variable has been used.
1795 void
1796 set_is_used()
1797 { this->is_used_ = true; }
1799 // Clear the initial value; used for error handling and write barriers.
1800 void
1801 clear_init()
1802 { this->init_ = NULL; }
1804 // Set the initial value; used for converting shortcuts.
1805 void
1806 set_init(Expression* init)
1807 { this->init_ = init; }
1809 // Get the preinit block, a block of statements to be run before the
1810 // initialization expression.
1811 Block*
1812 preinit_block(Gogo*);
1814 // Add a statement to be run before the initialization expression.
1815 // This is only used for global variables.
1816 void
1817 add_preinit_statement(Gogo*, Statement*);
1819 // Lower the initialization expression after parsing is complete.
1820 void
1821 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1823 // Flatten the initialization expression after ordering evaluations.
1824 void
1825 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1827 // A special case: the init value is used only to determine the
1828 // type. This is used if the variable is defined using := with the
1829 // comma-ok form of a map index or a receive expression. The init
1830 // value is actually the map index expression or receive expression.
1831 // We use this because we may not know the right type at parse time.
1832 void
1833 set_type_from_init_tuple()
1834 { this->type_from_init_tuple_ = true; }
1836 // Another special case: the init value is used only to determine
1837 // the type. This is used if the variable is defined using := with
1838 // a range clause. The init value is the range expression. The
1839 // type of the variable is the index type of the range expression
1840 // (i.e., the first value returned by a range).
1841 void
1842 set_type_from_range_index()
1843 { this->type_from_range_index_ = true; }
1845 // Another special case: like set_type_from_range_index, but the
1846 // type is the value type of the range expression (i.e., the second
1847 // value returned by a range).
1848 void
1849 set_type_from_range_value()
1850 { this->type_from_range_value_ = true; }
1852 // Another special case: the init value is used only to determine
1853 // the type. This is used if the variable is defined using := with
1854 // a case in a select statement. The init value is the channel.
1855 // The type of the variable is the channel's element type.
1856 void
1857 set_type_from_chan_element()
1858 { this->type_from_chan_element_ = true; }
1860 // After we lower the select statement, we once again set the type
1861 // from the initialization expression.
1862 void
1863 clear_type_from_chan_element()
1865 go_assert(this->type_from_chan_element_);
1866 this->type_from_chan_element_ = false;
1869 // TRUE if this variable was created for a type switch clause.
1870 bool
1871 is_type_switch_var() const
1872 { return this->is_type_switch_var_; }
1874 // Note that this variable was created for a type switch clause.
1875 void
1876 set_is_type_switch_var()
1877 { this->is_type_switch_var_ = true; }
1879 // Mark the variable as going into a unique section.
1880 void
1881 set_in_unique_section()
1883 go_assert(this->is_global_);
1884 this->in_unique_section_ = true;
1887 // Return the top-level declaration for this variable.
1888 Statement*
1889 toplevel_decl()
1890 { return this->toplevel_decl_; }
1892 // Set the top-level declaration for this variable. Only used for local
1893 // variables
1894 void
1895 set_toplevel_decl(Statement* s)
1897 go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_);
1898 this->toplevel_decl_ = s;
1901 // Traverse the initializer expression.
1903 traverse_expression(Traverse*, unsigned int traverse_mask);
1905 // Determine the type of the variable if necessary.
1906 void
1907 determine_type();
1909 // Get the backend representation of the variable.
1910 Bvariable*
1911 get_backend_variable(Gogo*, Named_object*, const Package*,
1912 const std::string&);
1914 // Get the initial value of the variable. This may only
1915 // be called if has_pre_init() returns false.
1916 Bexpression*
1917 get_init(Gogo*, Named_object* function);
1919 // Return a series of statements which sets the value of the
1920 // variable in DECL. This should only be called is has_pre_init()
1921 // returns true. DECL may be NULL for a sink variable.
1922 Bstatement*
1923 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1925 // Export the variable.
1926 void
1927 export_var(Export*, const std::string& name) const;
1929 // Import a variable.
1930 static void
1931 import_var(Import*, std::string* pname, Type** ptype);
1933 private:
1934 // The type of a tuple.
1935 Type*
1936 type_from_tuple(Expression*, bool) const;
1938 // The type of a range.
1939 Type*
1940 type_from_range(Expression*, bool, bool) const;
1942 // The element type of a channel.
1943 Type*
1944 type_from_chan_element(Expression*, bool) const;
1946 // The variable's type. This may be NULL if the type is set from
1947 // the expression.
1948 Type* type_;
1949 // The initial value. This may be NULL if the variable should be
1950 // initialized to the default value for the type.
1951 Expression* init_;
1952 // Statements to run before the init statement.
1953 Block* preinit_;
1954 // Location of variable definition.
1955 Location location_;
1956 // Backend representation.
1957 Bvariable* backend_;
1958 // Whether this is a global variable.
1959 bool is_global_ : 1;
1960 // Whether this is a function parameter.
1961 bool is_parameter_ : 1;
1962 // Whether this is a closure parameter.
1963 bool is_closure_ : 1;
1964 // Whether this is the receiver parameter of a method.
1965 bool is_receiver_ : 1;
1966 // Whether this is the varargs parameter of a function.
1967 bool is_varargs_parameter_ : 1;
1968 // Whether this variable is ever referenced.
1969 bool is_used_ : 1;
1970 // Whether something takes the address of this variable. For a
1971 // local variable this implies that the variable has to be on the
1972 // heap if it escapes from its function.
1973 bool is_address_taken_ : 1;
1974 // Whether something takes the address of this variable such that
1975 // the address does not escape the function.
1976 bool is_non_escaping_address_taken_ : 1;
1977 // True if we have seen this variable in a traversal.
1978 bool seen_ : 1;
1979 // True if we have lowered the initialization expression.
1980 bool init_is_lowered_ : 1;
1981 // True if we have flattened the initialization expression.
1982 bool init_is_flattened_ : 1;
1983 // True if init is a tuple used to set the type.
1984 bool type_from_init_tuple_ : 1;
1985 // True if init is a range clause and the type is the index type.
1986 bool type_from_range_index_ : 1;
1987 // True if init is a range clause and the type is the value type.
1988 bool type_from_range_value_ : 1;
1989 // True if init is a channel and the type is the channel's element type.
1990 bool type_from_chan_element_ : 1;
1991 // True if this is a variable created for a type switch case.
1992 bool is_type_switch_var_ : 1;
1993 // True if we have determined types.
1994 bool determined_type_ : 1;
1995 // True if this variable should be put in a unique section. This is
1996 // used for field tracking.
1997 bool in_unique_section_ : 1;
1998 // Whether this variable escapes the function it is created in. This is
1999 // true until shown otherwise.
2000 bool escapes_ : 1;
2001 // The top-level declaration for this variable. Only used for local
2002 // variables. Must be a Temporary_statement if not NULL.
2003 Statement* toplevel_decl_;
2006 // A variable which is really the name for a function return value, or
2007 // part of one.
2009 class Result_variable
2011 public:
2012 Result_variable(Type* type, Function* function, int index,
2013 Location location)
2014 : type_(type), function_(function), index_(index), location_(location),
2015 backend_(NULL), is_address_taken_(false),
2016 is_non_escaping_address_taken_(false), escapes_(true)
2019 // Get the type of the result variable.
2020 Type*
2021 type() const
2022 { return this->type_; }
2024 // Get the function that this is associated with.
2025 Function*
2026 function() const
2027 { return this->function_; }
2029 // Index in the list of function results.
2031 index() const
2032 { return this->index_; }
2034 // The location of the variable definition.
2035 Location
2036 location() const
2037 { return this->location_; }
2039 // Whether this variable's address is taken.
2040 bool
2041 is_address_taken() const
2042 { return this->is_address_taken_; }
2044 // Note that something takes the address of this variable.
2045 void
2046 set_address_taken()
2047 { this->is_address_taken_ = true; }
2049 // Return whether the address is taken but does not escape.
2050 bool
2051 is_non_escaping_address_taken() const
2052 { return this->is_non_escaping_address_taken_; }
2054 // Note that something takes the address of this variable such that
2055 // the address does not escape the function.
2056 void
2057 set_non_escaping_address_taken()
2058 { this->is_non_escaping_address_taken_ = true; }
2060 // Return whether this variable escapes the function it is declared in.
2061 bool
2062 escapes()
2063 { return this->escapes_; }
2065 // Note that this variable does not escape the function it is declared in.
2066 void
2067 set_does_not_escape()
2068 { this->escapes_ = false; }
2070 // Whether this variable should live in the heap.
2071 bool
2072 is_in_heap() const
2074 return this->is_address_taken_
2075 && this->escapes_;
2078 // Set the function. This is used when cloning functions which call
2079 // recover.
2080 void
2081 set_function(Function* function)
2082 { this->function_ = function; }
2084 // Get the backend representation of the variable.
2085 Bvariable*
2086 get_backend_variable(Gogo*, Named_object*, const std::string&);
2088 private:
2089 // Type of result variable.
2090 Type* type_;
2091 // Function with which this is associated.
2092 Function* function_;
2093 // Index in list of results.
2094 int index_;
2095 // Where the result variable is defined.
2096 Location location_;
2097 // Backend representation.
2098 Bvariable* backend_;
2099 // Whether something takes the address of this variable.
2100 bool is_address_taken_;
2101 // Whether something takes the address of this variable such that
2102 // the address does not escape the function.
2103 bool is_non_escaping_address_taken_;
2104 // Whether this variable escapes the function it is created in. This is
2105 // true until shown otherwise.
2106 bool escapes_;
2109 // The value we keep for a named constant. This lets us hold a type
2110 // and an expression.
2112 class Named_constant
2114 public:
2115 Named_constant(Type* type, Expression* expr, int iota_value,
2116 Location location)
2117 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2118 lowering_(false), is_sink_(false), bconst_(NULL)
2121 Type*
2122 type() const
2123 { return this->type_; }
2125 Expression*
2126 expr() const
2127 { return this->expr_; }
2130 iota_value() const
2131 { return this->iota_value_; }
2133 Location
2134 location() const
2135 { return this->location_; }
2137 // Whether we are lowering.
2138 bool
2139 lowering() const
2140 { return this->lowering_; }
2142 // Set that we are lowering.
2143 void
2144 set_lowering()
2145 { this->lowering_ = true; }
2147 // We are no longer lowering.
2148 void
2149 clear_lowering()
2150 { this->lowering_ = false; }
2152 bool
2153 is_sink() const
2154 { return this->is_sink_; }
2156 void
2157 set_is_sink()
2158 { this->is_sink_ = true; }
2160 // Traverse the expression.
2162 traverse_expression(Traverse*);
2164 // Determine the type of the constant if necessary.
2165 void
2166 determine_type();
2168 // Indicate that we found and reported an error for this constant.
2169 void
2170 set_error();
2172 // Export the constant.
2173 void
2174 export_const(Export*, const std::string& name) const;
2176 // Import a constant.
2177 static void
2178 import_const(Import*, std::string*, Type**, Expression**);
2180 // Get the backend representation of the constant value.
2181 Bexpression*
2182 get_backend(Gogo*, Named_object*);
2184 private:
2185 // The type of the constant.
2186 Type* type_;
2187 // The expression for the constant.
2188 Expression* expr_;
2189 // If the predeclared constant iota is used in EXPR_, this is the
2190 // value it will have. We do this because at parse time we don't
2191 // know whether the name "iota" will refer to the predeclared
2192 // constant or to something else. We put in the right value in when
2193 // we lower.
2194 int iota_value_;
2195 // The location of the definition.
2196 Location location_;
2197 // Whether we are currently lowering this constant.
2198 bool lowering_;
2199 // Whether this constant is blank named and needs only type checking.
2200 bool is_sink_;
2201 // The backend representation of the constant value.
2202 Bexpression* bconst_;
2205 // A type declaration.
2207 class Type_declaration
2209 public:
2210 Type_declaration(Location location)
2211 : location_(location), in_function_(NULL), in_function_index_(0),
2212 methods_(), issued_warning_(false)
2215 // Return the location.
2216 Location
2217 location() const
2218 { return this->location_; }
2220 // Return the function in which this type is declared. This will
2221 // return NULL for a type declared in global scope.
2222 Named_object*
2223 in_function(unsigned int* pindex)
2225 *pindex = this->in_function_index_;
2226 return this->in_function_;
2229 // Set the function in which this type is declared.
2230 void
2231 set_in_function(Named_object* f, unsigned int index)
2233 this->in_function_ = f;
2234 this->in_function_index_ = index;
2237 // Add a method to this type. This is used when methods are defined
2238 // before the type.
2239 Named_object*
2240 add_method(const std::string& name, Function* function);
2242 // Add a method declaration to this type.
2243 Named_object*
2244 add_method_declaration(const std::string& name, Package*,
2245 Function_type* type, Location location);
2247 // Add an already created object as a method.
2248 void
2249 add_existing_method(Named_object* no)
2250 { this->methods_.push_back(no); }
2252 // Return whether any methods were defined.
2253 bool
2254 has_methods() const;
2256 // Return the methods.
2257 const std::vector<Named_object*>*
2258 methods() const
2259 { return &this->methods_; }
2261 // Define methods when the real type is known.
2262 void
2263 define_methods(Named_type*);
2265 // This is called if we are trying to use this type. It returns
2266 // true if we should issue a warning.
2267 bool
2268 using_type();
2270 private:
2271 // The location of the type declaration.
2272 Location location_;
2273 // If this type is declared in a function, a pointer back to the
2274 // function in which it is defined.
2275 Named_object* in_function_;
2276 // The index of this type in IN_FUNCTION_.
2277 unsigned int in_function_index_;
2278 // Methods defined before the type is defined.
2279 std::vector<Named_object*> methods_;
2280 // True if we have issued a warning about a use of this type
2281 // declaration when it is undefined.
2282 bool issued_warning_;
2285 // An unknown object. These are created by the parser for forward
2286 // references to names which have not been seen before. In a correct
2287 // program, these will always point to a real definition by the end of
2288 // the parse. Because they point to another Named_object, these may
2289 // only be referenced by Unknown_expression objects.
2291 class Unknown_name
2293 public:
2294 Unknown_name(Location location)
2295 : location_(location), real_named_object_(NULL)
2298 // Return the location where this name was first seen.
2299 Location
2300 location() const
2301 { return this->location_; }
2303 // Return the real named object that this points to, or NULL if it
2304 // was never resolved.
2305 Named_object*
2306 real_named_object() const
2307 { return this->real_named_object_; }
2309 // Set the real named object that this points to.
2310 void
2311 set_real_named_object(Named_object* no);
2313 private:
2314 // The location where this name was first seen.
2315 Location location_;
2316 // The real named object when it is known.
2317 Named_object*
2318 real_named_object_;
2321 // A named object named. This is the result of a declaration. We
2322 // don't use a superclass because they all have to be handled
2323 // differently.
2325 class Named_object
2327 public:
2328 enum Classification
2330 // An uninitialized Named_object. We should never see this.
2331 NAMED_OBJECT_UNINITIALIZED,
2332 // An erroneous name. This indicates a parse error, to avoid
2333 // later errors about undefined references.
2334 NAMED_OBJECT_ERRONEOUS,
2335 // An unknown name. This is used for forward references. In a
2336 // correct program, these will all be resolved by the end of the
2337 // parse.
2338 NAMED_OBJECT_UNKNOWN,
2339 // A const.
2340 NAMED_OBJECT_CONST,
2341 // A type.
2342 NAMED_OBJECT_TYPE,
2343 // A forward type declaration.
2344 NAMED_OBJECT_TYPE_DECLARATION,
2345 // A var.
2346 NAMED_OBJECT_VAR,
2347 // A result variable in a function.
2348 NAMED_OBJECT_RESULT_VAR,
2349 // The blank identifier--the special variable named _.
2350 NAMED_OBJECT_SINK,
2351 // A func.
2352 NAMED_OBJECT_FUNC,
2353 // A forward func declaration.
2354 NAMED_OBJECT_FUNC_DECLARATION,
2355 // A package.
2356 NAMED_OBJECT_PACKAGE
2359 // Return the classification.
2360 Classification
2361 classification() const
2362 { return this->classification_; }
2364 // Classifiers.
2366 bool
2367 is_erroneous() const
2368 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2370 bool
2371 is_unknown() const
2372 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2374 bool
2375 is_const() const
2376 { return this->classification_ == NAMED_OBJECT_CONST; }
2378 bool
2379 is_type() const
2380 { return this->classification_ == NAMED_OBJECT_TYPE; }
2382 bool
2383 is_type_declaration() const
2384 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2386 bool
2387 is_variable() const
2388 { return this->classification_ == NAMED_OBJECT_VAR; }
2390 bool
2391 is_result_variable() const
2392 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2394 bool
2395 is_sink() const
2396 { return this->classification_ == NAMED_OBJECT_SINK; }
2398 bool
2399 is_function() const
2400 { return this->classification_ == NAMED_OBJECT_FUNC; }
2402 bool
2403 is_function_declaration() const
2404 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2406 bool
2407 is_package() const
2408 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2410 // Creators.
2412 static Named_object*
2413 make_erroneous_name(const std::string& name)
2414 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2416 static Named_object*
2417 make_unknown_name(const std::string& name, Location);
2419 static Named_object*
2420 make_constant(const Typed_identifier&, const Package*, Expression*,
2421 int iota_value);
2423 static Named_object*
2424 make_type(const std::string&, const Package*, Type*, Location);
2426 static Named_object*
2427 make_type_declaration(const std::string&, const Package*, Location);
2429 static Named_object*
2430 make_variable(const std::string&, const Package*, Variable*);
2432 static Named_object*
2433 make_result_variable(const std::string&, Result_variable*);
2435 static Named_object*
2436 make_sink();
2438 static Named_object*
2439 make_function(const std::string&, const Package*, Function*);
2441 static Named_object*
2442 make_function_declaration(const std::string&, const Package*, Function_type*,
2443 Location);
2445 static Named_object*
2446 make_package(const std::string& alias, Package* package);
2448 // Getters.
2450 Unknown_name*
2451 unknown_value()
2453 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2454 return this->u_.unknown_value;
2457 const Unknown_name*
2458 unknown_value() const
2460 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2461 return this->u_.unknown_value;
2464 Named_constant*
2465 const_value()
2467 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2468 return this->u_.const_value;
2471 const Named_constant*
2472 const_value() const
2474 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2475 return this->u_.const_value;
2478 Named_type*
2479 type_value()
2481 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2482 return this->u_.type_value;
2485 const Named_type*
2486 type_value() const
2488 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2489 return this->u_.type_value;
2492 Type_declaration*
2493 type_declaration_value()
2495 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2496 return this->u_.type_declaration;
2499 const Type_declaration*
2500 type_declaration_value() const
2502 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2503 return this->u_.type_declaration;
2506 Variable*
2507 var_value()
2509 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2510 return this->u_.var_value;
2513 const Variable*
2514 var_value() const
2516 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2517 return this->u_.var_value;
2520 Result_variable*
2521 result_var_value()
2523 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2524 return this->u_.result_var_value;
2527 const Result_variable*
2528 result_var_value() const
2530 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2531 return this->u_.result_var_value;
2534 Function*
2535 func_value()
2537 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2538 return this->u_.func_value;
2541 const Function*
2542 func_value() const
2544 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2545 return this->u_.func_value;
2548 Function_declaration*
2549 func_declaration_value()
2551 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2552 return this->u_.func_declaration_value;
2555 const Function_declaration*
2556 func_declaration_value() const
2558 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2559 return this->u_.func_declaration_value;
2562 Package*
2563 package_value()
2565 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2566 return this->u_.package_value;
2569 const Package*
2570 package_value() const
2572 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2573 return this->u_.package_value;
2576 const std::string&
2577 name() const
2578 { return this->name_; }
2580 // Return the name to use in an error message. The difference is
2581 // that if this Named_object is defined in a different package, this
2582 // will return PACKAGE.NAME.
2583 std::string
2584 message_name() const;
2586 const Package*
2587 package() const
2588 { return this->package_; }
2590 // Resolve an unknown value if possible. This returns the same
2591 // Named_object or a new one.
2592 Named_object*
2593 resolve()
2595 Named_object* ret = this;
2596 if (this->is_unknown())
2598 Named_object* r = this->unknown_value()->real_named_object();
2599 if (r != NULL)
2600 ret = r;
2602 return ret;
2605 const Named_object*
2606 resolve() const
2608 const Named_object* ret = this;
2609 if (this->is_unknown())
2611 const Named_object* r = this->unknown_value()->real_named_object();
2612 if (r != NULL)
2613 ret = r;
2615 return ret;
2618 // The location where this object was defined or referenced.
2619 Location
2620 location() const;
2622 // Convert a variable to the backend representation.
2623 Bvariable*
2624 get_backend_variable(Gogo*, Named_object* function);
2626 // Return the external identifier for this object.
2627 std::string
2628 get_id(Gogo*);
2630 // Get the backend representation of this object.
2631 void
2632 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2633 std::vector<Bfunction*>&);
2635 // Define a type declaration.
2636 void
2637 set_type_value(Named_type*);
2639 // Define a function declaration.
2640 void
2641 set_function_value(Function*);
2643 // Declare an unknown name as a type declaration.
2644 void
2645 declare_as_type();
2647 // Export this object.
2648 void
2649 export_named_object(Export*) const;
2651 // Mark this named object as an invalid redefinition of another object.
2652 void
2653 set_is_redefinition()
2654 { this->is_redefinition_ = true; }
2656 // Return whether or not this object is a invalid redefinition of another
2657 // object.
2658 bool
2659 is_redefinition() const
2660 { return this->is_redefinition_; }
2662 private:
2663 Named_object(const std::string&, const Package*, Classification);
2665 // The name of the object.
2666 std::string name_;
2667 // The package that this object is in. This is NULL if it is in the
2668 // file we are compiling.
2669 const Package* package_;
2670 // The type of object this is.
2671 Classification classification_;
2672 // The real data.
2673 union
2675 Unknown_name* unknown_value;
2676 Named_constant* const_value;
2677 Named_type* type_value;
2678 Type_declaration* type_declaration;
2679 Variable* var_value;
2680 Result_variable* result_var_value;
2681 Function* func_value;
2682 Function_declaration* func_declaration_value;
2683 Package* package_value;
2684 } u_;
2685 // True if this object is an invalid redefinition of another object.
2686 bool is_redefinition_;
2689 // A binding contour. This binds names to objects.
2691 class Bindings
2693 public:
2694 // Type for mapping from names to objects.
2695 typedef Unordered_map(std::string, Named_object*) Contour;
2697 Bindings(Bindings* enclosing);
2699 // Add an erroneous name.
2700 Named_object*
2701 add_erroneous_name(const std::string& name)
2702 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2704 // Add an unknown name.
2705 Named_object*
2706 add_unknown_name(const std::string& name, Location location)
2708 return this->add_named_object(Named_object::make_unknown_name(name,
2709 location));
2712 // Add a constant.
2713 Named_object*
2714 add_constant(const Typed_identifier& tid, const Package* package,
2715 Expression* expr, int iota_value)
2717 return this->add_named_object(Named_object::make_constant(tid, package,
2718 expr,
2719 iota_value));
2722 // Add a type.
2723 Named_object*
2724 add_type(const std::string& name, const Package* package, Type* type,
2725 Location location)
2727 return this->add_named_object(Named_object::make_type(name, package, type,
2728 location));
2731 // Add a named type. This is used for builtin types, and to add an
2732 // imported type to the global scope.
2733 Named_object*
2734 add_named_type(Named_type* named_type);
2736 // Add a type declaration.
2737 Named_object*
2738 add_type_declaration(const std::string& name, const Package* package,
2739 Location location)
2741 Named_object* no = Named_object::make_type_declaration(name, package,
2742 location);
2743 return this->add_named_object(no);
2746 // Add a variable.
2747 Named_object*
2748 add_variable(const std::string& name, const Package* package,
2749 Variable* variable)
2751 return this->add_named_object(Named_object::make_variable(name, package,
2752 variable));
2755 // Add a result variable.
2756 Named_object*
2757 add_result_variable(const std::string& name, Result_variable* result)
2759 return this->add_named_object(Named_object::make_result_variable(name,
2760 result));
2763 // Add a function.
2764 Named_object*
2765 add_function(const std::string& name, const Package*, Function* function);
2767 // Add a function declaration.
2768 Named_object*
2769 add_function_declaration(const std::string& name, const Package* package,
2770 Function_type* type, Location location);
2772 // Add a package. The location is the location of the import
2773 // statement.
2774 Named_object*
2775 add_package(const std::string& alias, Package* package)
2777 Named_object* no = Named_object::make_package(alias, package);
2778 return this->add_named_object(no);
2781 // Define a type which was already declared.
2782 void
2783 define_type(Named_object*, Named_type*);
2785 // Add a method to the list of objects. This is not added to the
2786 // lookup table.
2787 void
2788 add_method(Named_object*);
2790 // Add a named object to this binding.
2791 Named_object*
2792 add_named_object(Named_object* no)
2793 { return this->add_named_object_to_contour(&this->bindings_, no); }
2795 // Clear all names in file scope from the bindings.
2796 void
2797 clear_file_scope(Gogo*);
2799 // Look up a name in this binding contour and in any enclosing
2800 // binding contours. This returns NULL if the name is not found.
2801 Named_object*
2802 lookup(const std::string&) const;
2804 // Look up a name in this binding contour without looking in any
2805 // enclosing binding contours. Returns NULL if the name is not found.
2806 Named_object*
2807 lookup_local(const std::string&) const;
2809 // Remove a name.
2810 void
2811 remove_binding(Named_object*);
2813 // Mark all variables as used. This is used for some types of parse
2814 // error.
2815 void
2816 mark_locals_used();
2818 // Traverse the tree. See the Traverse class.
2820 traverse(Traverse*, bool is_global);
2822 // Iterate over definitions. This does not include things which
2823 // were only declared.
2825 typedef std::vector<Named_object*>::const_iterator
2826 const_definitions_iterator;
2828 const_definitions_iterator
2829 begin_definitions() const
2830 { return this->named_objects_.begin(); }
2832 const_definitions_iterator
2833 end_definitions() const
2834 { return this->named_objects_.end(); }
2836 // Return the number of definitions.
2837 size_t
2838 size_definitions() const
2839 { return this->named_objects_.size(); }
2841 // Return whether there are no definitions.
2842 bool
2843 empty_definitions() const
2844 { return this->named_objects_.empty(); }
2846 // Iterate over declarations. This is everything that has been
2847 // declared, which includes everything which has been defined.
2849 typedef Contour::const_iterator const_declarations_iterator;
2851 const_declarations_iterator
2852 begin_declarations() const
2853 { return this->bindings_.begin(); }
2855 const_declarations_iterator
2856 end_declarations() const
2857 { return this->bindings_.end(); }
2859 // Return the number of declarations.
2860 size_t
2861 size_declarations() const
2862 { return this->bindings_.size(); }
2864 // Return whether there are no declarations.
2865 bool
2866 empty_declarations() const
2867 { return this->bindings_.empty(); }
2869 // Return the first declaration.
2870 Named_object*
2871 first_declaration()
2872 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2874 private:
2875 Named_object*
2876 add_named_object_to_contour(Contour*, Named_object*);
2878 Named_object*
2879 new_definition(Named_object*, Named_object*);
2881 // Enclosing bindings.
2882 Bindings* enclosing_;
2883 // The list of objects.
2884 std::vector<Named_object*> named_objects_;
2885 // The mapping from names to objects.
2886 Contour bindings_;
2889 // A label.
2891 class Label
2893 public:
2894 Label(const std::string& name)
2895 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2896 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
2899 // Return the label's name.
2900 const std::string&
2901 name() const
2902 { return this->name_; }
2904 // Return whether the label has been defined.
2905 bool
2906 is_defined() const
2907 { return !Linemap::is_unknown_location(this->location_); }
2909 // Return whether the label has been used.
2910 bool
2911 is_used() const
2912 { return this->is_used_; }
2914 // Record that the label is used.
2915 void
2916 set_is_used()
2917 { this->is_used_ = true; }
2919 // Return whether this label is looping.
2920 bool
2921 looping() const
2922 { return this->depth_ == DEPTH_LOOPING; }
2924 // Set this label as looping.
2925 void
2926 set_looping()
2927 { this->depth_ = DEPTH_LOOPING; }
2929 // Return whether this label is nonlooping.
2930 bool
2931 nonlooping() const
2932 { return this->depth_ == DEPTH_NONLOOPING; }
2934 // Set this label as nonlooping.
2935 void
2936 set_nonlooping()
2937 { this->depth_ = DEPTH_NONLOOPING; }
2939 // Return the location of the definition.
2940 Location
2941 location() const
2942 { return this->location_; }
2944 // Return the bindings snapshot.
2945 Bindings_snapshot*
2946 snapshot() const
2947 { return this->snapshot_; }
2949 // Add a snapshot of a goto which refers to this label.
2950 void
2951 add_snapshot_ref(Bindings_snapshot* snapshot)
2953 go_assert(Linemap::is_unknown_location(this->location_));
2954 this->refs_.push_back(snapshot);
2957 // Return the list of snapshots of goto statements which refer to
2958 // this label.
2959 const std::vector<Bindings_snapshot*>&
2960 refs() const
2961 { return this->refs_; }
2963 // Clear the references.
2964 void
2965 clear_refs();
2967 // Define the label at LOCATION with the given bindings snapshot.
2968 void
2969 define(Location location, Bindings_snapshot* snapshot)
2971 if (this->is_dummy_label())
2972 return;
2973 go_assert(Linemap::is_unknown_location(this->location_)
2974 && this->snapshot_ == NULL);
2975 this->location_ = location;
2976 this->snapshot_ = snapshot;
2979 // Return the backend representation for this label.
2980 Blabel*
2981 get_backend_label(Translate_context*);
2983 // Return an expression for the address of this label. This is used
2984 // to get the return address of a deferred function to see whether
2985 // the function may call recover.
2986 Bexpression*
2987 get_addr(Translate_context*, Location location);
2989 // Return a dummy label, representing any instance of the blank label.
2990 static Label*
2991 create_dummy_label();
2993 // Return TRUE if this is a dummy label.
2994 bool
2995 is_dummy_label() const
2996 { return this->name_ == "_"; }
2998 // A classification of a label's looping depth.
2999 enum Loop_depth
3001 DEPTH_UNKNOWN,
3002 // A label never jumped to.
3003 DEPTH_NONLOOPING,
3004 // A label jumped to.
3005 DEPTH_LOOPING
3008 private:
3009 // The name of the label.
3010 std::string name_;
3011 // The location of the definition. This is 0 if the label has not
3012 // yet been defined.
3013 Location location_;
3014 // A snapshot of the set of bindings defined at this label, used to
3015 // issue errors about invalid goto statements.
3016 Bindings_snapshot* snapshot_;
3017 // A list of snapshots of goto statements which refer to this label.
3018 std::vector<Bindings_snapshot*> refs_;
3019 // Whether the label has been used.
3020 bool is_used_;
3021 // The backend representation.
3022 Blabel* blabel_;
3023 // The looping depth of this label, for escape analysis.
3024 Loop_depth depth_;
3027 // An unnamed label. These are used when lowering loops.
3029 class Unnamed_label
3031 public:
3032 Unnamed_label(Location location)
3033 : location_(location), derived_from_(NULL), blabel_(NULL)
3036 // Get the location where the label is defined.
3037 Location
3038 location() const
3039 { return this->location_; }
3041 // Set the location where the label is defined.
3042 void
3043 set_location(Location location)
3044 { this->location_ = location; }
3046 // Get the top level statement this unnamed label is derived from.
3047 Statement*
3048 derived_from() const
3049 { return this->derived_from_; }
3051 // Set the top level statement this unnamed label is derived from.
3052 void
3053 set_derived_from(Statement* s)
3054 { this->derived_from_ = s; }
3056 // Return a statement which defines this label.
3057 Bstatement*
3058 get_definition(Translate_context*);
3060 // Return a goto to this label from LOCATION.
3061 Bstatement*
3062 get_goto(Translate_context*, Location location);
3064 private:
3065 // Return the backend representation.
3066 Blabel*
3067 get_blabel(Translate_context*);
3069 // The location where the label is defined.
3070 Location location_;
3071 // The top-level statement this unnamed label was derived/lowered from.
3072 // This is NULL is this label is not the top-level of a lowered statement.
3073 Statement* derived_from_;
3074 // The backend representation of this label.
3075 Blabel* blabel_;
3078 // An alias for an imported package.
3080 class Package_alias
3082 public:
3083 Package_alias(Location location)
3084 : location_(location), used_(0)
3087 // The location of the import statement.
3088 Location
3089 location()
3090 { return this->location_; }
3092 // How many symbols from the package were used under this alias.
3093 size_t
3094 used() const
3095 { return this->used_; }
3097 // Note that some symbol was used under this alias.
3098 void
3099 note_usage()
3100 { this->used_++; }
3102 private:
3103 // The location of the import statement.
3104 Location location_;
3105 // The amount of times some name from this package was used under this alias.
3106 size_t used_;
3109 // An imported package.
3111 class Package
3113 public:
3114 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3115 Location location);
3117 // Get the package path used for all symbols exported from this
3118 // package.
3119 const std::string&
3120 pkgpath() const
3121 { return this->pkgpath_; }
3123 // Return the package path to use for a symbol name.
3124 std::string
3125 pkgpath_symbol() const;
3127 // Set the package path symbol.
3128 void
3129 set_pkgpath_symbol(const std::string&);
3131 // Return the location of the most recent import statement.
3132 Location
3133 location() const
3134 { return this->location_; }
3136 // Return whether we know the name of this package yet.
3137 bool
3138 has_package_name() const
3139 { return !this->package_name_.empty(); }
3141 // The name that this package uses in its package clause. This may
3142 // be different from the name in the associated Named_object if the
3143 // import statement used an alias.
3144 const std::string&
3145 package_name() const
3147 go_assert(!this->package_name_.empty());
3148 return this->package_name_;
3151 // Return the bindings.
3152 Bindings*
3153 bindings()
3154 { return this->bindings_; }
3156 // Type used to map import names to package aliases.
3157 typedef std::map<std::string, Package_alias*> Aliases;
3159 // Return the set of package aliases.
3160 const Aliases&
3161 aliases() const
3162 { return this->aliases_; }
3164 // Note that some symbol from this package was used and qualified by ALIAS.
3165 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3166 void
3167 note_usage(const std::string& alias) const;
3169 // Note that USAGE might be a fake usage of this package.
3170 void
3171 note_fake_usage(Expression* usage) const
3172 { this->fake_uses_.insert(usage); }
3174 // Forget a given USAGE of this package.
3175 void
3176 forget_usage(Expression* usage) const;
3178 // Clear the used field for the next file.
3179 void
3180 clear_used();
3182 // Look up a name in the package. Returns NULL if the name is not
3183 // found.
3184 Named_object*
3185 lookup(const std::string& name) const
3186 { return this->bindings_->lookup(name); }
3188 // Set the name of the package.
3189 void
3190 set_package_name(const std::string& name, Location);
3192 // Set the location of the package. This is used to record the most
3193 // recent import location.
3194 void
3195 set_location(Location location)
3196 { this->location_ = location; }
3198 // Add a package name as an ALIAS for this package.
3199 Package_alias*
3200 add_alias(const std::string& alias, Location);
3202 // Add a constant to the package.
3203 Named_object*
3204 add_constant(const Typed_identifier& tid, Expression* expr)
3205 { return this->bindings_->add_constant(tid, this, expr, 0); }
3207 // Add a type to the package.
3208 Named_object*
3209 add_type(const std::string& name, Type* type, Location location)
3210 { return this->bindings_->add_type(name, this, type, location); }
3212 // Add a type declaration to the package.
3213 Named_object*
3214 add_type_declaration(const std::string& name, Location location)
3215 { return this->bindings_->add_type_declaration(name, this, location); }
3217 // Add a variable to the package.
3218 Named_object*
3219 add_variable(const std::string& name, Variable* variable)
3220 { return this->bindings_->add_variable(name, this, variable); }
3222 // Add a function declaration to the package.
3223 Named_object*
3224 add_function_declaration(const std::string& name, Function_type* type,
3225 Location loc)
3226 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3228 // Determine types of constants.
3229 void
3230 determine_types();
3232 private:
3233 // The package path for type reflection data.
3234 std::string pkgpath_;
3235 // The package path for symbol names.
3236 std::string pkgpath_symbol_;
3237 // The name that this package uses in the package clause. This may
3238 // be the empty string if it is not yet known.
3239 std::string package_name_;
3240 // The names in this package.
3241 Bindings* bindings_;
3242 // The location of the most recent import statement.
3243 Location location_;
3244 // The set of aliases associated with this package.
3245 Aliases aliases_;
3246 // A set of possibly fake uses of this package. This is mutable because we
3247 // can track fake uses of a package even if we have a const pointer to it.
3248 mutable std::set<Expression*> fake_uses_;
3251 // Return codes for the traversal functions. This is not an enum
3252 // because we want to be able to declare traversal functions in other
3253 // header files without including this one.
3255 // Continue traversal as usual.
3256 const int TRAVERSE_CONTINUE = -1;
3258 // Exit traversal.
3259 const int TRAVERSE_EXIT = 0;
3261 // Continue traversal, but skip components of the current object.
3262 // E.g., if this is returned by Traverse::statement, we do not
3263 // traverse the expressions in the statement even if
3264 // traverse_expressions is set in the traverse_mask.
3265 const int TRAVERSE_SKIP_COMPONENTS = 1;
3267 // This class is used when traversing the parse tree. The caller uses
3268 // a subclass which overrides functions as desired.
3270 class Traverse
3272 public:
3273 // These bitmasks say what to traverse.
3274 static const unsigned int traverse_variables = 0x1;
3275 static const unsigned int traverse_constants = 0x2;
3276 static const unsigned int traverse_functions = 0x4;
3277 static const unsigned int traverse_blocks = 0x8;
3278 static const unsigned int traverse_statements = 0x10;
3279 static const unsigned int traverse_expressions = 0x20;
3280 static const unsigned int traverse_types = 0x40;
3282 Traverse(unsigned int traverse_mask)
3283 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3286 virtual ~Traverse();
3288 // The bitmask of what to traverse.
3289 unsigned int
3290 traverse_mask() const
3291 { return this->traverse_mask_; }
3293 // Record that we are going to traverse a type. This returns true
3294 // if the type has already been seen in this traversal. This is
3295 // required because types, unlike expressions, can form a circular
3296 // graph.
3297 bool
3298 remember_type(const Type*);
3300 // Record that we are going to see an expression. This returns true
3301 // if the expression has already been seen in this traversal. This
3302 // is only needed for cases where multiple expressions can point to
3303 // a single one.
3304 bool
3305 remember_expression(const Expression*);
3307 // These functions return one of the TRAVERSE codes defined above.
3309 // If traverse_variables is set in the mask, this is called for
3310 // every variable in the tree.
3311 virtual int
3312 variable(Named_object*);
3314 // If traverse_constants is set in the mask, this is called for
3315 // every named constant in the tree. The bool parameter is true for
3316 // a global constant.
3317 virtual int
3318 constant(Named_object*, bool);
3320 // If traverse_functions is set in the mask, this is called for
3321 // every function in the tree.
3322 virtual int
3323 function(Named_object*);
3325 // If traverse_blocks is set in the mask, this is called for every
3326 // block in the tree.
3327 virtual int
3328 block(Block*);
3330 // If traverse_statements is set in the mask, this is called for
3331 // every statement in the tree.
3332 virtual int
3333 statement(Block*, size_t* index, Statement*);
3335 // If traverse_expressions is set in the mask, this is called for
3336 // every expression in the tree.
3337 virtual int
3338 expression(Expression**);
3340 // If traverse_types is set in the mask, this is called for every
3341 // type in the tree.
3342 virtual int
3343 type(Type*);
3345 private:
3346 // A hash table for types we have seen during this traversal. Note
3347 // that this uses the default hash functions for pointers rather
3348 // than Type_hash_identical and Type_identical. This is because for
3349 // traversal we care about seeing a specific type structure. If
3350 // there are two separate instances of identical types, we want to
3351 // traverse both.
3352 typedef Unordered_set(const Type*) Types_seen;
3354 typedef Unordered_set(const Expression*) Expressions_seen;
3356 // Bitmask of what sort of objects to traverse.
3357 unsigned int traverse_mask_;
3358 // Types which have been seen in this traversal.
3359 Types_seen* types_seen_;
3360 // Expressions which have been seen in this traversal.
3361 Expressions_seen* expressions_seen_;
3364 // A class which makes it easier to insert new statements before the
3365 // current statement during a traversal.
3367 class Statement_inserter
3369 public:
3370 // Empty constructor.
3371 Statement_inserter()
3372 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
3375 // Constructor for a statement in a block.
3376 Statement_inserter(Block* block, size_t *pindex)
3377 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
3380 // Constructor for a global variable.
3381 Statement_inserter(Gogo* gogo, Variable* var)
3382 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
3383 { go_assert(var->is_global()); }
3385 // We use the default copy constructor and assignment operator.
3387 // Insert S before the statement we are traversing, or before the
3388 // initialization expression of a global variable.
3389 void
3390 insert(Statement* s);
3392 private:
3393 // The block that the statement is in.
3394 Block* block_;
3395 // The index of the statement that we are traversing.
3396 size_t* pindex_;
3397 // The IR, needed when looking at an initializer expression for a
3398 // global variable.
3399 Gogo* gogo_;
3400 // The global variable, when looking at an initializer expression.
3401 Variable* var_;
3404 // When translating the gogo IR into the backend data structure, this
3405 // is the context we pass down the blocks and statements.
3407 class Translate_context
3409 public:
3410 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3411 Bblock* bblock)
3412 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3413 block_(block), bblock_(bblock), is_const_(false)
3416 // Accessors.
3418 Gogo*
3419 gogo()
3420 { return this->gogo_; }
3422 Backend*
3423 backend()
3424 { return this->backend_; }
3426 Named_object*
3427 function()
3428 { return this->function_; }
3430 Block*
3431 block()
3432 { return this->block_; }
3434 Bblock*
3435 bblock()
3436 { return this->bblock_; }
3438 bool
3439 is_const()
3440 { return this->is_const_; }
3442 // Make a constant context.
3443 void
3444 set_is_const()
3445 { this->is_const_ = true; }
3447 private:
3448 // The IR for the entire compilation unit.
3449 Gogo* gogo_;
3450 // The generator for the backend data structures.
3451 Backend* backend_;
3452 // The function we are currently translating. NULL if not in a
3453 // function, e.g., the initializer of a global variable.
3454 Named_object* function_;
3455 // The block we are currently translating. NULL if not in a
3456 // function.
3457 Block *block_;
3458 // The backend representation of the current block. NULL if block_
3459 // is NULL.
3460 Bblock* bblock_;
3461 // Whether this is being evaluated in a constant context. This is
3462 // used for type descriptor initializers.
3463 bool is_const_;
3466 // Runtime error codes. These must match the values in
3467 // libgo/runtime/go-runtime-error.c.
3469 // Slice index out of bounds: negative or larger than the length of
3470 // the slice.
3471 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3473 // Array index out of bounds.
3474 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3476 // String index out of bounds.
3477 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3479 // Slice slice out of bounds: negative or larger than the length of
3480 // the slice or high bound less than low bound.
3481 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3483 // Array slice out of bounds.
3484 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3486 // String slice out of bounds.
3487 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3489 // Dereference of nil pointer. This is used when there is a
3490 // dereference of a pointer to a very large struct or array, to ensure
3491 // that a gigantic array is not used a proxy to access random memory
3492 // locations.
3493 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3495 // Slice length or capacity out of bounds in make: negative or
3496 // overflow or length greater than capacity.
3497 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3499 // Map capacity out of bounds in make: negative or overflow.
3500 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3502 // Channel capacity out of bounds in make: negative or overflow.
3503 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3505 // Division by zero.
3506 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3508 // Go statement with nil function.
3509 static const int RUNTIME_ERROR_GO_NIL = 11;
3511 // This is used by some of the langhooks.
3512 extern Gogo* go_get_gogo();
3514 // Whether we have seen any errors. FIXME: Replace with a backend
3515 // interface.
3516 extern bool saw_errors();
3518 #endif // !defined(GO_GOGO_H)