compiler: reclaim memory of escape analysis Nodes
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blobf7f8d602bd169dc262861cb9a4bf7dab0cd70b7a
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 // Reclaim memory of escape analysis Nodes.
686 void
687 reclaim_escape_nodes();
689 // Do all exports.
690 void
691 do_exports();
693 // Add an import control function for an imported package to the
694 // list.
695 void
696 add_import_init_fn(const std::string& package_name,
697 const std::string& init_name, int prio);
699 // Return the Import_init for a given init name.
700 Import_init*
701 lookup_init(const std::string& init_name);
703 // Turn short-cut operators (&&, ||) into explicit if statements.
704 void
705 remove_shortcuts();
707 // Use temporary variables to force order of evaluation.
708 void
709 order_evaluations();
711 // Add write barriers as needed.
712 void
713 add_write_barriers();
715 // Return whether an assignment that sets LHS to RHS needs a write
716 // barrier.
717 bool
718 assign_needs_write_barrier(Expression* lhs);
720 // Return an assignment that sets LHS to RHS using a write barrier.
721 // This returns an if statement that checks whether write barriers
722 // are enabled. If not, it does LHS = RHS, otherwise it calls the
723 // appropriate write barrier function.
724 Statement*
725 assign_with_write_barrier(Function*, Block*, Statement_inserter*,
726 Expression* lhs, Expression* rhs, Location);
728 // Flatten parse tree.
729 void
730 flatten();
732 // Build thunks for functions which call recover.
733 void
734 build_recover_thunks();
736 // Return a declaration for __builtin_return_address or
737 // __builtin_frame_address.
738 static Named_object*
739 declare_builtin_rf_address(const char* name);
741 // Simplify statements which might use thunks: go and defer
742 // statements.
743 void
744 simplify_thunk_statements();
746 // Dump AST if -fgo-dump-ast is set
747 void
748 dump_ast(const char* basename);
750 // Dump Call Graph if -fgo-dump-calls is set.
751 void
752 dump_call_graph(const char* basename);
754 // Dump Connection Graphs if -fgo-dump-connections is set.
755 void
756 dump_connection_graphs(const char* basename);
758 // Convert named types to the backend representation.
759 void
760 convert_named_types();
762 // Convert named types in a list of bindings.
763 void
764 convert_named_types_in_bindings(Bindings*);
766 // True if named types have been converted to the backend
767 // representation.
768 bool
769 named_types_are_converted() const
770 { return this->named_types_are_converted_; }
772 // Give an error if the initialization of VAR depends on itself.
773 void
774 check_self_dep(Named_object*);
776 // Write out the global values.
777 void
778 write_globals();
780 // Build a call to the runtime error function.
781 Expression*
782 runtime_error(int code, Location);
784 // Build required interface method tables.
785 void
786 build_interface_method_tables();
788 // Return an expression which allocates memory to hold values of type TYPE.
789 Expression*
790 allocate_memory(Type *type, Location);
792 // Return the assembler name to use for an exported function, a
793 // method, or a function/method declaration.
794 std::string
795 function_asm_name(const std::string& go_name, const Package*,
796 const Type* receiver);
798 // Return the name to use for a function descriptor.
799 std::string
800 function_descriptor_name(Named_object*);
802 // Return the name to use for a generated stub method.
803 std::string
804 stub_method_name(const std::string& method_name);
806 // Return the names of the hash and equality functions for TYPE.
807 void
808 specific_type_function_names(const Type*, const Named_type*,
809 std::string* hash_name,
810 std::string* equal_name);
812 // Return the assembler name to use for a global variable.
813 std::string
814 global_var_asm_name(const std::string& go_name, const Package*);
816 // Return a name to use for an error case. This should only be used
817 // after reporting an error, and is used to avoid useless knockon
818 // errors.
819 static std::string
820 erroneous_name();
822 // Return whether the name indicates an error.
823 static bool
824 is_erroneous_name(const std::string&);
826 // Return a name to use for a thunk function. A thunk function is
827 // one we create during the compilation, for a go statement or a
828 // defer statement or a method expression.
829 static std::string
830 thunk_name();
832 // Return whether an object is a thunk.
833 static bool
834 is_thunk(const Named_object*);
836 // Return the name to use for an init function.
837 std::string
838 init_function_name();
840 // Return the name to use for a nested function.
841 static std::string
842 nested_function_name();
844 // Return the name to use for a sink funciton.
845 std::string
846 sink_function_name();
848 // Return the name to use for an (erroneous) redefined function.
849 std::string
850 redefined_function_name();
852 // Return the name for use for a recover thunk.
853 std::string
854 recover_thunk_name(const std::string& name, const Type* rtype);
856 // Return the name to use for the GC root variable.
857 std::string
858 gc_root_name();
860 // Return the name to use for a composite literal or string
861 // initializer.
862 std::string
863 initializer_name();
865 // Return the name of the variable used to represent the zero value
866 // of a map.
867 std::string
868 map_zero_value_name();
870 // Get the name of the magic initialization function.
871 const std::string&
872 get_init_fn_name();
874 // Return the name for a type descriptor symbol.
875 std::string
876 type_descriptor_name(Type*, Named_type*);
878 // Return the assembler name for the GC symbol for a type.
879 std::string
880 gc_symbol_name(Type*);
882 // Return the assembler name for a ptrmask variable.
883 std::string
884 ptrmask_symbol_name(const std::string& ptrmask_sym_name);
886 // Return the name to use for an interface method table.
887 std::string
888 interface_method_table_name(Interface_type*, Type*, bool is_pointer);
890 private:
891 // During parsing, we keep a stack of functions. Each function on
892 // the stack is one that we are currently parsing. For each
893 // function, we keep track of the current stack of blocks.
894 struct Open_function
896 // The function.
897 Named_object* function;
898 // The stack of active blocks in the function.
899 std::vector<Block*> blocks;
902 // The stack of functions.
903 typedef std::vector<Open_function> Open_functions;
905 // Set up the built-in unsafe package.
906 void
907 import_unsafe(const std::string&, bool is_exported, Location);
909 // Return the current binding contour.
910 Bindings*
911 current_bindings();
913 const Bindings*
914 current_bindings() const;
916 void
917 write_c_header();
919 // Get the decl for the magic initialization function.
920 Named_object*
921 initialization_function_decl();
923 // Create the magic initialization function.
924 Named_object*
925 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
927 // Initialize imported packages. BFUNCTION is the function
928 // into which the package init calls will be placed.
929 void
930 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
932 // Register variables with the garbage collector.
933 void
934 register_gc_vars(const std::vector<Named_object*>&,
935 std::vector<Bstatement*>&,
936 Bfunction* init_bfunction);
938 Named_object*
939 write_barrier_variable();
941 Statement*
942 check_write_barrier(Block*, Statement*, Statement*);
944 // Type used to map import names to packages.
945 typedef std::map<std::string, Package*> Imports;
947 // Type used to map package names to packages.
948 typedef std::map<std::string, Package*> Packages;
950 // Type used to map variables to the function calls that set them.
951 // This is used for initialization dependency analysis.
952 typedef std::map<Variable*, Named_object*> Var_deps;
954 // Type used to map identifiers in the file block to the location
955 // where they were defined.
956 typedef Unordered_map(std::string, Location) File_block_names;
958 // Type used to queue writing a type specific function.
959 struct Specific_type_function
961 Type* type;
962 Named_type* name;
963 int64_t size;
964 std::string hash_name;
965 Function_type* hash_fntype;
966 std::string equal_name;
967 Function_type* equal_fntype;
969 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
970 const std::string& ahash_name,
971 Function_type* ahash_fntype,
972 const std::string& aequal_name,
973 Function_type* aequal_fntype)
974 : type(atype), name(aname), size(asize), hash_name(ahash_name),
975 hash_fntype(ahash_fntype), equal_name(aequal_name),
976 equal_fntype(aequal_fntype)
980 // Recompute init priorities.
981 void
982 recompute_init_priorities();
984 // Recursive helper used by the routine above.
985 void
986 update_init_priority(Import_init* ii,
987 std::set<const Import_init *>* visited);
989 // The backend generator.
990 Backend* backend_;
991 // The object used to keep track of file names and line numbers.
992 Linemap* linemap_;
993 // The package we are compiling.
994 Package* package_;
995 // The list of currently open functions during parsing.
996 Open_functions functions_;
997 // The global binding contour. This includes the builtin functions
998 // and the package we are compiling.
999 Bindings* globals_;
1000 // The list of names we have seen in the file block.
1001 File_block_names file_block_names_;
1002 // Mapping from import file names to packages.
1003 Imports imports_;
1004 // Whether the magic unsafe package was imported.
1005 bool imported_unsafe_;
1006 // Whether the magic unsafe package was imported by the current file.
1007 bool current_file_imported_unsafe_;
1008 // Mapping from package names we have seen to packages. This does
1009 // not include the package we are compiling.
1010 Packages packages_;
1011 // The functions named "init", if there are any.
1012 std::vector<Named_object*> init_functions_;
1013 // A mapping from variables to the function calls that initialize
1014 // them, if it is not stored in the variable's init or preinit.
1015 // This is used for dependency analysis.
1016 Var_deps var_deps_;
1017 // Whether we need a magic initialization function.
1018 bool need_init_fn_;
1019 // The name of the magic initialization function.
1020 std::string init_fn_name_;
1021 // A list of import control variables for packages that we import.
1022 Import_init_set imported_init_fns_;
1023 // The package path used for reflection data.
1024 std::string pkgpath_;
1025 // The package path to use for a symbol name.
1026 std::string pkgpath_symbol_;
1027 // The prefix to use for symbols, from the -fgo-prefix option.
1028 std::string prefix_;
1029 // Whether pkgpath_ has been set.
1030 bool pkgpath_set_;
1031 // Whether an explicit package path was set by -fgo-pkgpath.
1032 bool pkgpath_from_option_;
1033 // Whether an explicit prefix was set by -fgo-prefix.
1034 bool prefix_from_option_;
1035 // The relative import path, from the -fgo-relative-import-path
1036 // option.
1037 std::string relative_import_path_;
1038 // The C header file to write, from the -fgo-c-header option.
1039 std::string c_header_;
1040 // Whether or not to check for division by zero, from the
1041 // -fgo-check-divide-zero option.
1042 bool check_divide_by_zero_;
1043 // Whether or not to check for division overflow, from the
1044 // -fgo-check-divide-overflow option.
1045 bool check_divide_overflow_;
1046 // Whether we are compiling the runtime package, from the
1047 // -fgo-compiling-runtime option.
1048 bool compiling_runtime_;
1049 // The level of escape analysis debug information to emit, from the
1050 // -fgo-debug-escape option.
1051 int debug_escape_level_;
1052 // A hash value for debug escape analysis, from the
1053 // -fgo-debug-escape-hash option. The analysis is run only on
1054 // functions with names that hash to the matching value.
1055 std::string debug_escape_hash_;
1056 // Nil-check size threshhold.
1057 int64_t nil_check_size_threshold_;
1058 // A list of types to verify.
1059 std::vector<Type*> verify_types_;
1060 // A list of interface types defined while parsing.
1061 std::vector<Interface_type*> interface_types_;
1062 // Type specific functions to write out.
1063 std::vector<Specific_type_function*> specific_type_functions_;
1064 // Whether we are done writing out specific type functions.
1065 bool specific_type_functions_are_written_;
1066 // Whether named types have been converted.
1067 bool named_types_are_converted_;
1068 // A list containing groups of possibly mutually recursive functions to be
1069 // considered during escape analysis.
1070 std::vector<Analysis_set> analysis_sets_;
1071 // A list of objects to add to the GC roots.
1072 std::vector<Expression*> gc_roots_;
1075 // A block of statements.
1077 class Block
1079 public:
1080 Block(Block* enclosing, Location);
1082 // Return the enclosing block.
1083 const Block*
1084 enclosing() const
1085 { return this->enclosing_; }
1087 // Return the bindings of the block.
1088 Bindings*
1089 bindings()
1090 { return this->bindings_; }
1092 const Bindings*
1093 bindings() const
1094 { return this->bindings_; }
1096 // Look at the block's statements.
1097 const std::vector<Statement*>*
1098 statements() const
1099 { return &this->statements_; }
1101 // Return the start location. This is normally the location of the
1102 // left curly brace which starts the block.
1103 Location
1104 start_location() const
1105 { return this->start_location_; }
1107 // Return the end location. This is normally the location of the
1108 // right curly brace which ends the block.
1109 Location
1110 end_location() const
1111 { return this->end_location_; }
1113 // Add a statement to the block.
1114 void
1115 add_statement(Statement*);
1117 // Add a statement to the front of the block.
1118 void
1119 add_statement_at_front(Statement*);
1121 // Replace a statement in a block.
1122 void
1123 replace_statement(size_t index, Statement*);
1125 // Add a Statement before statement number INDEX.
1126 void
1127 insert_statement_before(size_t index, Statement*);
1129 // Add a Statement after statement number INDEX.
1130 void
1131 insert_statement_after(size_t index, Statement*);
1133 // Set the end location of the block.
1134 void
1135 set_end_location(Location location)
1136 { this->end_location_ = location; }
1138 // Traverse the tree.
1140 traverse(Traverse*);
1142 // Set final types for unspecified variables and constants.
1143 void
1144 determine_types();
1146 // Return true if execution of this block may fall through to the
1147 // next block.
1148 bool
1149 may_fall_through() const;
1151 // Convert the block to the backend representation.
1152 Bblock*
1153 get_backend(Translate_context*);
1155 // Iterate over statements.
1157 typedef std::vector<Statement*>::iterator iterator;
1159 iterator
1160 begin()
1161 { return this->statements_.begin(); }
1163 iterator
1164 end()
1165 { return this->statements_.end(); }
1167 private:
1168 // Enclosing block.
1169 Block* enclosing_;
1170 // Statements in the block.
1171 std::vector<Statement*> statements_;
1172 // Binding contour.
1173 Bindings* bindings_;
1174 // Location of start of block.
1175 Location start_location_;
1176 // Location of end of block.
1177 Location end_location_;
1180 // A function.
1182 class Function
1184 public:
1185 Function(Function_type* type, Named_object*, Block*, Location);
1187 // Return the function's type.
1188 Function_type*
1189 type() const
1190 { return this->type_; }
1192 // Return the enclosing function if there is one.
1193 Named_object*
1194 enclosing() const
1195 { return this->enclosing_; }
1197 // Set the enclosing function. This is used when building thunks
1198 // for functions which call recover.
1199 void
1200 set_enclosing(Named_object* enclosing)
1202 go_assert(this->enclosing_ == NULL);
1203 this->enclosing_ = enclosing;
1206 // The result variables.
1207 typedef std::vector<Named_object*> Results;
1209 // Create the result variables in the outer block.
1210 void
1211 create_result_variables(Gogo*);
1213 // Update the named result variables when cloning a function which
1214 // calls recover.
1215 void
1216 update_result_variables();
1218 // Return the result variables.
1219 Results*
1220 result_variables()
1221 { return this->results_; }
1223 bool
1224 is_sink() const
1225 { return this->is_sink_; }
1227 void
1228 set_is_sink()
1229 { this->is_sink_ = true; }
1231 // Whether the result variables have names.
1232 bool
1233 results_are_named() const
1234 { return this->results_are_named_; }
1236 // Set the assembler name.
1237 void
1238 set_asm_name(const std::string& asm_name)
1239 { this->asm_name_ = asm_name; }
1241 // Return the pragmas for this function.
1242 unsigned int
1243 pragmas() const
1244 { return this->pragmas_; }
1246 // Set the pragmas for this function.
1247 void
1248 set_pragmas(unsigned int pragmas)
1250 this->pragmas_ = pragmas;
1253 // Whether this method should not be included in the type
1254 // descriptor.
1255 bool
1256 nointerface() const;
1258 // Record that this method should not be included in the type
1259 // descriptor.
1260 void
1261 set_nointerface();
1263 // Record that this function is a stub method created for an unnamed
1264 // type.
1265 void
1266 set_is_unnamed_type_stub_method()
1268 go_assert(this->is_method());
1269 this->is_unnamed_type_stub_method_ = true;
1272 // Return the amount of enclosed variables in this closure.
1273 size_t
1274 closure_field_count() const
1275 { return this->closure_fields_.size(); }
1277 // Add a new field to the closure variable.
1278 void
1279 add_closure_field(Named_object* var, Location loc)
1280 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1282 // Whether this function needs a closure.
1283 bool
1284 needs_closure() const
1285 { return !this->closure_fields_.empty(); }
1287 // Return the closure variable, creating it if necessary. This is
1288 // passed to the function as a static chain parameter.
1289 Named_object*
1290 closure_var();
1292 // Set the closure variable. This is used when building thunks for
1293 // functions which call recover.
1294 void
1295 set_closure_var(Named_object* v)
1297 go_assert(this->closure_var_ == NULL);
1298 this->closure_var_ = v;
1301 // Return the variable for a reference to field INDEX in the closure
1302 // variable.
1303 Named_object*
1304 enclosing_var(unsigned int index)
1306 go_assert(index < this->closure_fields_.size());
1307 return closure_fields_[index].first;
1310 // Set the type of the closure variable if there is one.
1311 void
1312 set_closure_type();
1314 // Get the block of statements associated with the function.
1315 Block*
1316 block() const
1317 { return this->block_; }
1319 // Get the location of the start of the function.
1320 Location
1321 location() const
1322 { return this->location_; }
1324 // Return whether this function is actually a method.
1325 bool
1326 is_method() const;
1328 // Add a label definition to the function.
1329 Label*
1330 add_label_definition(Gogo*, const std::string& label_name, Location);
1332 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1333 // if we should report errors for a goto from the current location
1334 // to the label location.
1335 Label*
1336 add_label_reference(Gogo*, const std::string& label_name,
1337 Location, bool issue_goto_errors);
1339 // Warn about labels that are defined but not used.
1340 void
1341 check_labels() const;
1343 // Note that a new local type has been added. Return its index.
1344 unsigned int
1345 new_local_type_index()
1346 { return this->local_type_count_++; }
1348 // Whether this function calls the predeclared recover function.
1349 bool
1350 calls_recover() const
1351 { return this->calls_recover_; }
1353 // Record that this function calls the predeclared recover function.
1354 // This is set during the lowering pass.
1355 void
1356 set_calls_recover()
1357 { this->calls_recover_ = true; }
1359 // Whether this is a recover thunk function.
1360 bool
1361 is_recover_thunk() const
1362 { return this->is_recover_thunk_; }
1364 // Record that this is a thunk built for a function which calls
1365 // recover.
1366 void
1367 set_is_recover_thunk()
1368 { this->is_recover_thunk_ = true; }
1370 // Whether this function already has a recover thunk.
1371 bool
1372 has_recover_thunk() const
1373 { return this->has_recover_thunk_; }
1375 // Record that this function already has a recover thunk.
1376 void
1377 set_has_recover_thunk()
1378 { this->has_recover_thunk_ = true; }
1380 // Record that this function is a thunk created for a defer
1381 // statement that calls the __go_set_defer_retaddr runtime function.
1382 void
1383 set_calls_defer_retaddr()
1384 { this->calls_defer_retaddr_ = true; }
1386 // Whether this is a type hash or equality function created by the
1387 // compiler.
1388 bool
1389 is_type_specific_function()
1390 { return this->is_type_specific_function_; }
1392 // Record that this function is a type hash or equality function
1393 // created by the compiler.
1394 void
1395 set_is_type_specific_function()
1396 { this->is_type_specific_function_ = true; }
1398 // Mark the function as going into a unique section.
1399 void
1400 set_in_unique_section()
1401 { this->in_unique_section_ = true; }
1403 // Swap with another function. Used only for the thunk which calls
1404 // recover.
1405 void
1406 swap_for_recover(Function *);
1408 // Traverse the tree.
1410 traverse(Traverse*);
1412 // Determine types in the function.
1413 void
1414 determine_types();
1416 // Return an expression for the function descriptor, given the named
1417 // object for this function. This may only be called for functions
1418 // without a closure. This will be an immutable struct with one
1419 // field that points to the function's code.
1420 Expression*
1421 descriptor(Gogo*, Named_object*);
1423 // Set the descriptor for this function. This is used when a
1424 // function declaration is followed by a function definition.
1425 void
1426 set_descriptor(Expression* descriptor)
1428 go_assert(this->descriptor_ == NULL);
1429 this->descriptor_ = descriptor;
1432 // Return the backend representation.
1433 Bfunction*
1434 get_or_make_decl(Gogo*, Named_object*);
1436 // Return the function's decl after it has been built.
1437 Bfunction*
1438 get_decl() const;
1440 // Set the function decl to hold a backend representation of the function
1441 // code.
1442 void
1443 build(Gogo*, Named_object*);
1445 // Get the statement that assigns values to this function's result struct.
1446 Bstatement*
1447 return_value(Gogo*, Named_object*, Location) const;
1449 // Get an expression for the variable holding the defer stack.
1450 Expression*
1451 defer_stack(Location);
1453 // Export the function.
1454 void
1455 export_func(Export*, const std::string& name) const;
1457 // Export a function with a type.
1458 static void
1459 export_func_with_type(Export*, const std::string& name,
1460 const Function_type*);
1462 // Import a function.
1463 static void
1464 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1465 Typed_identifier_list** pparameters,
1466 Typed_identifier_list** presults, bool* is_varargs);
1468 private:
1469 // Type for mapping from label names to Label objects.
1470 typedef Unordered_map(std::string, Label*) Labels;
1472 void
1473 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1475 typedef std::vector<std::pair<Named_object*,
1476 Location> > Closure_fields;
1478 // The function's type.
1479 Function_type* type_;
1480 // The enclosing function. This is NULL when there isn't one, which
1481 // is the normal case.
1482 Named_object* enclosing_;
1483 // The result variables, if any.
1484 Results* results_;
1485 // If there is a closure, this is the list of variables which appear
1486 // in the closure. This is created by the parser, and then resolved
1487 // to a real type when we lower parse trees.
1488 Closure_fields closure_fields_;
1489 // The closure variable, passed as a parameter using the static
1490 // chain parameter. Normally NULL.
1491 Named_object* closure_var_;
1492 // The outer block of statements in the function.
1493 Block* block_;
1494 // The source location of the start of the function.
1495 Location location_;
1496 // Labels defined or referenced in the function.
1497 Labels labels_;
1498 // The number of local types defined in this function.
1499 unsigned int local_type_count_;
1500 // The assembler name: this is the name that will be put in the object file.
1501 // Set by the go:linkname compiler directive. This is normally empty.
1502 std::string asm_name_;
1503 // The function descriptor, if any.
1504 Expression* descriptor_;
1505 // The function decl.
1506 Bfunction* fndecl_;
1507 // The defer stack variable. A pointer to this variable is used to
1508 // distinguish the defer stack for one function from another. This
1509 // is NULL unless we actually need a defer stack.
1510 Temporary_statement* defer_stack_;
1511 // Pragmas for this function. This is a set of GOPRAGMA bits.
1512 unsigned int pragmas_;
1513 // True if this function is sink-named. No code is generated.
1514 bool is_sink_ : 1;
1515 // True if the result variables are named.
1516 bool results_are_named_ : 1;
1517 // True if this function is a stub method created for an unnamed
1518 // type.
1519 bool is_unnamed_type_stub_method_ : 1;
1520 // True if this function calls the predeclared recover function.
1521 bool calls_recover_ : 1;
1522 // True if this a thunk built for a function which calls recover.
1523 bool is_recover_thunk_ : 1;
1524 // True if this function already has a recover thunk.
1525 bool has_recover_thunk_ : 1;
1526 // True if this is a thunk built for a defer statement that calls
1527 // the __go_set_defer_retaddr runtime function.
1528 bool calls_defer_retaddr_ : 1;
1529 // True if this is a function built by the compiler to as a hash or
1530 // equality function for some type.
1531 bool is_type_specific_function_ : 1;
1532 // True if this function should be put in a unique section. This is
1533 // turned on for field tracking.
1534 bool in_unique_section_ : 1;
1537 // A snapshot of the current binding state.
1539 class Bindings_snapshot
1541 public:
1542 Bindings_snapshot(const Block*, Location);
1544 // Report any errors appropriate for a goto from the current binding
1545 // state of B to this one.
1546 void
1547 check_goto_from(const Block* b, Location);
1549 // Report any errors appropriate for a goto from this binding state
1550 // to the current state of B.
1551 void
1552 check_goto_to(const Block* b);
1554 private:
1555 bool
1556 check_goto_block(Location, const Block*, const Block*, size_t*);
1558 void
1559 check_goto_defs(Location, const Block*, size_t, size_t);
1561 // The current block.
1562 const Block* block_;
1563 // The number of names currently defined in each open block.
1564 // Element 0 is this->block_, element 1 is
1565 // this->block_->enclosing(), etc.
1566 std::vector<size_t> counts_;
1567 // The location where this snapshot was taken.
1568 Location location_;
1571 // A function declaration.
1573 class Function_declaration
1575 public:
1576 Function_declaration(Function_type* fntype, Location location)
1577 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1578 fndecl_(NULL), pragmas_(0)
1581 Function_type*
1582 type() const
1583 { return this->fntype_; }
1585 Location
1586 location() const
1587 { return this->location_; }
1589 const std::string&
1590 asm_name() const
1591 { return this->asm_name_; }
1593 // Set the assembler name.
1594 void
1595 set_asm_name(const std::string& asm_name)
1596 { this->asm_name_ = asm_name; }
1598 // Return the pragmas for this function.
1599 unsigned int
1600 pragmas() const
1601 { return this->pragmas_; }
1603 // Set the pragmas for this function.
1604 void
1605 set_pragmas(unsigned int pragmas)
1607 this->pragmas_ = pragmas;
1610 // Return an expression for the function descriptor, given the named
1611 // object for this function. This may only be called for functions
1612 // without a closure. This will be an immutable struct with one
1613 // field that points to the function's code.
1614 Expression*
1615 descriptor(Gogo*, Named_object*);
1617 // Return true if we have created a descriptor for this declaration.
1618 bool
1619 has_descriptor() const
1620 { return this->descriptor_ != NULL; }
1622 // Return a backend representation.
1623 Bfunction*
1624 get_or_make_decl(Gogo*, Named_object*);
1626 // If there is a descriptor, build it into the backend
1627 // representation.
1628 void
1629 build_backend_descriptor(Gogo*);
1631 // Export a function declaration.
1632 void
1633 export_func(Export* exp, const std::string& name) const
1634 { Function::export_func_with_type(exp, name, this->fntype_); }
1636 // Check that the types used in this declaration's signature are defined.
1637 void
1638 check_types() const;
1640 private:
1641 // The type of the function.
1642 Function_type* fntype_;
1643 // The location of the declaration.
1644 Location location_;
1645 // The assembler name: this is the name to use in references to the
1646 // function. This is normally empty.
1647 std::string asm_name_;
1648 // The function descriptor, if any.
1649 Expression* descriptor_;
1650 // The function decl if needed.
1651 Bfunction* fndecl_;
1652 // Pragmas for this function. This is a set of GOPRAGMA bits.
1653 unsigned int pragmas_;
1656 // A variable.
1658 class Variable
1660 public:
1661 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1662 bool is_receiver, Location);
1664 // Get the type of the variable.
1665 Type*
1666 type();
1668 Type*
1669 type() const;
1671 // Return whether the type is defined yet.
1672 bool
1673 has_type() const;
1675 // Get the initial value.
1676 Expression*
1677 init() const
1678 { return this->init_; }
1680 // Return whether there are any preinit statements.
1681 bool
1682 has_pre_init() const
1683 { return this->preinit_ != NULL; }
1685 // Return the preinit statements if any.
1686 Block*
1687 preinit() const
1688 { return this->preinit_; }
1690 // Return whether this is a global variable.
1691 bool
1692 is_global() const
1693 { return this->is_global_; }
1695 // Return whether this is a function parameter.
1696 bool
1697 is_parameter() const
1698 { return this->is_parameter_; }
1700 // Return whether this is a closure (static chain) parameter.
1701 bool
1702 is_closure() const
1703 { return this->is_closure_; }
1705 // Change this parameter to be a closure.
1706 void
1707 set_is_closure()
1709 this->is_closure_ = true;
1712 // Return whether this is the receiver parameter of a method.
1713 bool
1714 is_receiver() const
1715 { return this->is_receiver_; }
1717 // Change this parameter to be a receiver. This is used when
1718 // creating the thunks created for functions which call recover.
1719 void
1720 set_is_receiver()
1722 go_assert(this->is_parameter_);
1723 this->is_receiver_ = true;
1726 // Change this parameter to not be a receiver. This is used when
1727 // creating the thunks created for functions which call recover.
1728 void
1729 set_is_not_receiver()
1731 go_assert(this->is_parameter_);
1732 this->is_receiver_ = false;
1735 // Return whether this is the varargs parameter of a function.
1736 bool
1737 is_varargs_parameter() const
1738 { return this->is_varargs_parameter_; }
1740 // Whether this variable's address is taken.
1741 bool
1742 is_address_taken() const
1743 { return this->is_address_taken_; }
1745 // Whether this variable should live in the heap.
1746 bool
1747 is_in_heap() const
1749 return this->is_address_taken_
1750 && this->escapes_
1751 && !this->is_global_;
1754 // Note that something takes the address of this variable.
1755 void
1756 set_address_taken()
1757 { this->is_address_taken_ = true; }
1759 // Return whether the address is taken but does not escape.
1760 bool
1761 is_non_escaping_address_taken() const
1762 { return this->is_non_escaping_address_taken_; }
1764 // Note that something takes the address of this variable such that
1765 // the address does not escape the function.
1766 void
1767 set_non_escaping_address_taken()
1768 { this->is_non_escaping_address_taken_ = true; }
1770 // Return whether this variable escapes the function it is declared in.
1771 bool
1772 escapes()
1773 { return this->escapes_; }
1775 // Note that this variable does not escape the function it is declared in.
1776 void
1777 set_does_not_escape()
1778 { this->escapes_ = false; }
1780 // Get the source location of the variable's declaration.
1781 Location
1782 location() const
1783 { return this->location_; }
1785 // Record that this is the varargs parameter of a function.
1786 void
1787 set_is_varargs_parameter()
1789 go_assert(this->is_parameter_);
1790 this->is_varargs_parameter_ = true;
1793 // Return whether the variable has been used.
1794 bool
1795 is_used() const
1796 { return this->is_used_; }
1798 // Mark that the variable has been used.
1799 void
1800 set_is_used()
1801 { this->is_used_ = true; }
1803 // Clear the initial value; used for error handling and write barriers.
1804 void
1805 clear_init()
1806 { this->init_ = NULL; }
1808 // Set the initial value; used for converting shortcuts.
1809 void
1810 set_init(Expression* init)
1811 { this->init_ = init; }
1813 // Get the preinit block, a block of statements to be run before the
1814 // initialization expression.
1815 Block*
1816 preinit_block(Gogo*);
1818 // Add a statement to be run before the initialization expression.
1819 // This is only used for global variables.
1820 void
1821 add_preinit_statement(Gogo*, Statement*);
1823 // Lower the initialization expression after parsing is complete.
1824 void
1825 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1827 // Flatten the initialization expression after ordering evaluations.
1828 void
1829 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1831 // A special case: the init value is used only to determine the
1832 // type. This is used if the variable is defined using := with the
1833 // comma-ok form of a map index or a receive expression. The init
1834 // value is actually the map index expression or receive expression.
1835 // We use this because we may not know the right type at parse time.
1836 void
1837 set_type_from_init_tuple()
1838 { this->type_from_init_tuple_ = true; }
1840 // Another special case: the init value is used only to determine
1841 // the type. This is used if the variable is defined using := with
1842 // a range clause. The init value is the range expression. The
1843 // type of the variable is the index type of the range expression
1844 // (i.e., the first value returned by a range).
1845 void
1846 set_type_from_range_index()
1847 { this->type_from_range_index_ = true; }
1849 // Another special case: like set_type_from_range_index, but the
1850 // type is the value type of the range expression (i.e., the second
1851 // value returned by a range).
1852 void
1853 set_type_from_range_value()
1854 { this->type_from_range_value_ = true; }
1856 // Another special case: the init value is used only to determine
1857 // the type. This is used if the variable is defined using := with
1858 // a case in a select statement. The init value is the channel.
1859 // The type of the variable is the channel's element type.
1860 void
1861 set_type_from_chan_element()
1862 { this->type_from_chan_element_ = true; }
1864 // After we lower the select statement, we once again set the type
1865 // from the initialization expression.
1866 void
1867 clear_type_from_chan_element()
1869 go_assert(this->type_from_chan_element_);
1870 this->type_from_chan_element_ = false;
1873 // TRUE if this variable was created for a type switch clause.
1874 bool
1875 is_type_switch_var() const
1876 { return this->is_type_switch_var_; }
1878 // Note that this variable was created for a type switch clause.
1879 void
1880 set_is_type_switch_var()
1881 { this->is_type_switch_var_ = true; }
1883 // Mark the variable as going into a unique section.
1884 void
1885 set_in_unique_section()
1887 go_assert(this->is_global_);
1888 this->in_unique_section_ = true;
1891 // Return the top-level declaration for this variable.
1892 Statement*
1893 toplevel_decl()
1894 { return this->toplevel_decl_; }
1896 // Set the top-level declaration for this variable. Only used for local
1897 // variables
1898 void
1899 set_toplevel_decl(Statement* s)
1901 go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_);
1902 this->toplevel_decl_ = s;
1905 // Traverse the initializer expression.
1907 traverse_expression(Traverse*, unsigned int traverse_mask);
1909 // Determine the type of the variable if necessary.
1910 void
1911 determine_type();
1913 // Get the backend representation of the variable.
1914 Bvariable*
1915 get_backend_variable(Gogo*, Named_object*, const Package*,
1916 const std::string&);
1918 // Get the initial value of the variable. This may only
1919 // be called if has_pre_init() returns false.
1920 Bexpression*
1921 get_init(Gogo*, Named_object* function);
1923 // Return a series of statements which sets the value of the
1924 // variable in DECL. This should only be called is has_pre_init()
1925 // returns true. DECL may be NULL for a sink variable.
1926 Bstatement*
1927 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1929 // Export the variable.
1930 void
1931 export_var(Export*, const std::string& name) const;
1933 // Import a variable.
1934 static void
1935 import_var(Import*, std::string* pname, Type** ptype);
1937 private:
1938 // The type of a tuple.
1939 Type*
1940 type_from_tuple(Expression*, bool) const;
1942 // The type of a range.
1943 Type*
1944 type_from_range(Expression*, bool, bool) const;
1946 // The element type of a channel.
1947 Type*
1948 type_from_chan_element(Expression*, bool) const;
1950 // The variable's type. This may be NULL if the type is set from
1951 // the expression.
1952 Type* type_;
1953 // The initial value. This may be NULL if the variable should be
1954 // initialized to the default value for the type.
1955 Expression* init_;
1956 // Statements to run before the init statement.
1957 Block* preinit_;
1958 // Location of variable definition.
1959 Location location_;
1960 // Backend representation.
1961 Bvariable* backend_;
1962 // Whether this is a global variable.
1963 bool is_global_ : 1;
1964 // Whether this is a function parameter.
1965 bool is_parameter_ : 1;
1966 // Whether this is a closure parameter.
1967 bool is_closure_ : 1;
1968 // Whether this is the receiver parameter of a method.
1969 bool is_receiver_ : 1;
1970 // Whether this is the varargs parameter of a function.
1971 bool is_varargs_parameter_ : 1;
1972 // Whether this variable is ever referenced.
1973 bool is_used_ : 1;
1974 // Whether something takes the address of this variable. For a
1975 // local variable this implies that the variable has to be on the
1976 // heap if it escapes from its function.
1977 bool is_address_taken_ : 1;
1978 // Whether something takes the address of this variable such that
1979 // the address does not escape the function.
1980 bool is_non_escaping_address_taken_ : 1;
1981 // True if we have seen this variable in a traversal.
1982 bool seen_ : 1;
1983 // True if we have lowered the initialization expression.
1984 bool init_is_lowered_ : 1;
1985 // True if we have flattened the initialization expression.
1986 bool init_is_flattened_ : 1;
1987 // True if init is a tuple used to set the type.
1988 bool type_from_init_tuple_ : 1;
1989 // True if init is a range clause and the type is the index type.
1990 bool type_from_range_index_ : 1;
1991 // True if init is a range clause and the type is the value type.
1992 bool type_from_range_value_ : 1;
1993 // True if init is a channel and the type is the channel's element type.
1994 bool type_from_chan_element_ : 1;
1995 // True if this is a variable created for a type switch case.
1996 bool is_type_switch_var_ : 1;
1997 // True if we have determined types.
1998 bool determined_type_ : 1;
1999 // True if this variable should be put in a unique section. This is
2000 // used for field tracking.
2001 bool in_unique_section_ : 1;
2002 // Whether this variable escapes the function it is created in. This is
2003 // true until shown otherwise.
2004 bool escapes_ : 1;
2005 // The top-level declaration for this variable. Only used for local
2006 // variables. Must be a Temporary_statement if not NULL.
2007 Statement* toplevel_decl_;
2010 // A variable which is really the name for a function return value, or
2011 // part of one.
2013 class Result_variable
2015 public:
2016 Result_variable(Type* type, Function* function, int index,
2017 Location location)
2018 : type_(type), function_(function), index_(index), location_(location),
2019 backend_(NULL), is_address_taken_(false),
2020 is_non_escaping_address_taken_(false), escapes_(true)
2023 // Get the type of the result variable.
2024 Type*
2025 type() const
2026 { return this->type_; }
2028 // Get the function that this is associated with.
2029 Function*
2030 function() const
2031 { return this->function_; }
2033 // Index in the list of function results.
2035 index() const
2036 { return this->index_; }
2038 // The location of the variable definition.
2039 Location
2040 location() const
2041 { return this->location_; }
2043 // Whether this variable's address is taken.
2044 bool
2045 is_address_taken() const
2046 { return this->is_address_taken_; }
2048 // Note that something takes the address of this variable.
2049 void
2050 set_address_taken()
2051 { this->is_address_taken_ = true; }
2053 // Return whether the address is taken but does not escape.
2054 bool
2055 is_non_escaping_address_taken() const
2056 { return this->is_non_escaping_address_taken_; }
2058 // Note that something takes the address of this variable such that
2059 // the address does not escape the function.
2060 void
2061 set_non_escaping_address_taken()
2062 { this->is_non_escaping_address_taken_ = true; }
2064 // Return whether this variable escapes the function it is declared in.
2065 bool
2066 escapes()
2067 { return this->escapes_; }
2069 // Note that this variable does not escape the function it is declared in.
2070 void
2071 set_does_not_escape()
2072 { this->escapes_ = false; }
2074 // Whether this variable should live in the heap.
2075 bool
2076 is_in_heap() const
2078 return this->is_address_taken_
2079 && this->escapes_;
2082 // Set the function. This is used when cloning functions which call
2083 // recover.
2084 void
2085 set_function(Function* function)
2086 { this->function_ = function; }
2088 // Get the backend representation of the variable.
2089 Bvariable*
2090 get_backend_variable(Gogo*, Named_object*, const std::string&);
2092 private:
2093 // Type of result variable.
2094 Type* type_;
2095 // Function with which this is associated.
2096 Function* function_;
2097 // Index in list of results.
2098 int index_;
2099 // Where the result variable is defined.
2100 Location location_;
2101 // Backend representation.
2102 Bvariable* backend_;
2103 // Whether something takes the address of this variable.
2104 bool is_address_taken_;
2105 // Whether something takes the address of this variable such that
2106 // the address does not escape the function.
2107 bool is_non_escaping_address_taken_;
2108 // Whether this variable escapes the function it is created in. This is
2109 // true until shown otherwise.
2110 bool escapes_;
2113 // The value we keep for a named constant. This lets us hold a type
2114 // and an expression.
2116 class Named_constant
2118 public:
2119 Named_constant(Type* type, Expression* expr, int iota_value,
2120 Location location)
2121 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2122 lowering_(false), is_sink_(false), bconst_(NULL)
2125 Type*
2126 type() const
2127 { return this->type_; }
2129 Expression*
2130 expr() const
2131 { return this->expr_; }
2134 iota_value() const
2135 { return this->iota_value_; }
2137 Location
2138 location() const
2139 { return this->location_; }
2141 // Whether we are lowering.
2142 bool
2143 lowering() const
2144 { return this->lowering_; }
2146 // Set that we are lowering.
2147 void
2148 set_lowering()
2149 { this->lowering_ = true; }
2151 // We are no longer lowering.
2152 void
2153 clear_lowering()
2154 { this->lowering_ = false; }
2156 bool
2157 is_sink() const
2158 { return this->is_sink_; }
2160 void
2161 set_is_sink()
2162 { this->is_sink_ = true; }
2164 // Traverse the expression.
2166 traverse_expression(Traverse*);
2168 // Determine the type of the constant if necessary.
2169 void
2170 determine_type();
2172 // Indicate that we found and reported an error for this constant.
2173 void
2174 set_error();
2176 // Export the constant.
2177 void
2178 export_const(Export*, const std::string& name) const;
2180 // Import a constant.
2181 static void
2182 import_const(Import*, std::string*, Type**, Expression**);
2184 // Get the backend representation of the constant value.
2185 Bexpression*
2186 get_backend(Gogo*, Named_object*);
2188 private:
2189 // The type of the constant.
2190 Type* type_;
2191 // The expression for the constant.
2192 Expression* expr_;
2193 // If the predeclared constant iota is used in EXPR_, this is the
2194 // value it will have. We do this because at parse time we don't
2195 // know whether the name "iota" will refer to the predeclared
2196 // constant or to something else. We put in the right value in when
2197 // we lower.
2198 int iota_value_;
2199 // The location of the definition.
2200 Location location_;
2201 // Whether we are currently lowering this constant.
2202 bool lowering_;
2203 // Whether this constant is blank named and needs only type checking.
2204 bool is_sink_;
2205 // The backend representation of the constant value.
2206 Bexpression* bconst_;
2209 // A type declaration.
2211 class Type_declaration
2213 public:
2214 Type_declaration(Location location)
2215 : location_(location), in_function_(NULL), in_function_index_(0),
2216 methods_(), issued_warning_(false)
2219 // Return the location.
2220 Location
2221 location() const
2222 { return this->location_; }
2224 // Return the function in which this type is declared. This will
2225 // return NULL for a type declared in global scope.
2226 Named_object*
2227 in_function(unsigned int* pindex)
2229 *pindex = this->in_function_index_;
2230 return this->in_function_;
2233 // Set the function in which this type is declared.
2234 void
2235 set_in_function(Named_object* f, unsigned int index)
2237 this->in_function_ = f;
2238 this->in_function_index_ = index;
2241 // Add a method to this type. This is used when methods are defined
2242 // before the type.
2243 Named_object*
2244 add_method(const std::string& name, Function* function);
2246 // Add a method declaration to this type.
2247 Named_object*
2248 add_method_declaration(const std::string& name, Package*,
2249 Function_type* type, Location location);
2251 // Add an already created object as a method.
2252 void
2253 add_existing_method(Named_object* no)
2254 { this->methods_.push_back(no); }
2256 // Return whether any methods were defined.
2257 bool
2258 has_methods() const;
2260 // Return the methods.
2261 const std::vector<Named_object*>*
2262 methods() const
2263 { return &this->methods_; }
2265 // Define methods when the real type is known.
2266 void
2267 define_methods(Named_type*);
2269 // This is called if we are trying to use this type. It returns
2270 // true if we should issue a warning.
2271 bool
2272 using_type();
2274 private:
2275 // The location of the type declaration.
2276 Location location_;
2277 // If this type is declared in a function, a pointer back to the
2278 // function in which it is defined.
2279 Named_object* in_function_;
2280 // The index of this type in IN_FUNCTION_.
2281 unsigned int in_function_index_;
2282 // Methods defined before the type is defined.
2283 std::vector<Named_object*> methods_;
2284 // True if we have issued a warning about a use of this type
2285 // declaration when it is undefined.
2286 bool issued_warning_;
2289 // An unknown object. These are created by the parser for forward
2290 // references to names which have not been seen before. In a correct
2291 // program, these will always point to a real definition by the end of
2292 // the parse. Because they point to another Named_object, these may
2293 // only be referenced by Unknown_expression objects.
2295 class Unknown_name
2297 public:
2298 Unknown_name(Location location)
2299 : location_(location), real_named_object_(NULL)
2302 // Return the location where this name was first seen.
2303 Location
2304 location() const
2305 { return this->location_; }
2307 // Return the real named object that this points to, or NULL if it
2308 // was never resolved.
2309 Named_object*
2310 real_named_object() const
2311 { return this->real_named_object_; }
2313 // Set the real named object that this points to.
2314 void
2315 set_real_named_object(Named_object* no);
2317 private:
2318 // The location where this name was first seen.
2319 Location location_;
2320 // The real named object when it is known.
2321 Named_object*
2322 real_named_object_;
2325 // A named object named. This is the result of a declaration. We
2326 // don't use a superclass because they all have to be handled
2327 // differently.
2329 class Named_object
2331 public:
2332 enum Classification
2334 // An uninitialized Named_object. We should never see this.
2335 NAMED_OBJECT_UNINITIALIZED,
2336 // An erroneous name. This indicates a parse error, to avoid
2337 // later errors about undefined references.
2338 NAMED_OBJECT_ERRONEOUS,
2339 // An unknown name. This is used for forward references. In a
2340 // correct program, these will all be resolved by the end of the
2341 // parse.
2342 NAMED_OBJECT_UNKNOWN,
2343 // A const.
2344 NAMED_OBJECT_CONST,
2345 // A type.
2346 NAMED_OBJECT_TYPE,
2347 // A forward type declaration.
2348 NAMED_OBJECT_TYPE_DECLARATION,
2349 // A var.
2350 NAMED_OBJECT_VAR,
2351 // A result variable in a function.
2352 NAMED_OBJECT_RESULT_VAR,
2353 // The blank identifier--the special variable named _.
2354 NAMED_OBJECT_SINK,
2355 // A func.
2356 NAMED_OBJECT_FUNC,
2357 // A forward func declaration.
2358 NAMED_OBJECT_FUNC_DECLARATION,
2359 // A package.
2360 NAMED_OBJECT_PACKAGE
2363 // Return the classification.
2364 Classification
2365 classification() const
2366 { return this->classification_; }
2368 // Classifiers.
2370 bool
2371 is_erroneous() const
2372 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2374 bool
2375 is_unknown() const
2376 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2378 bool
2379 is_const() const
2380 { return this->classification_ == NAMED_OBJECT_CONST; }
2382 bool
2383 is_type() const
2384 { return this->classification_ == NAMED_OBJECT_TYPE; }
2386 bool
2387 is_type_declaration() const
2388 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2390 bool
2391 is_variable() const
2392 { return this->classification_ == NAMED_OBJECT_VAR; }
2394 bool
2395 is_result_variable() const
2396 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2398 bool
2399 is_sink() const
2400 { return this->classification_ == NAMED_OBJECT_SINK; }
2402 bool
2403 is_function() const
2404 { return this->classification_ == NAMED_OBJECT_FUNC; }
2406 bool
2407 is_function_declaration() const
2408 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2410 bool
2411 is_package() const
2412 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2414 // Creators.
2416 static Named_object*
2417 make_erroneous_name(const std::string& name)
2418 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2420 static Named_object*
2421 make_unknown_name(const std::string& name, Location);
2423 static Named_object*
2424 make_constant(const Typed_identifier&, const Package*, Expression*,
2425 int iota_value);
2427 static Named_object*
2428 make_type(const std::string&, const Package*, Type*, Location);
2430 static Named_object*
2431 make_type_declaration(const std::string&, const Package*, Location);
2433 static Named_object*
2434 make_variable(const std::string&, const Package*, Variable*);
2436 static Named_object*
2437 make_result_variable(const std::string&, Result_variable*);
2439 static Named_object*
2440 make_sink();
2442 static Named_object*
2443 make_function(const std::string&, const Package*, Function*);
2445 static Named_object*
2446 make_function_declaration(const std::string&, const Package*, Function_type*,
2447 Location);
2449 static Named_object*
2450 make_package(const std::string& alias, Package* package);
2452 // Getters.
2454 Unknown_name*
2455 unknown_value()
2457 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2458 return this->u_.unknown_value;
2461 const Unknown_name*
2462 unknown_value() const
2464 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2465 return this->u_.unknown_value;
2468 Named_constant*
2469 const_value()
2471 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2472 return this->u_.const_value;
2475 const Named_constant*
2476 const_value() const
2478 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2479 return this->u_.const_value;
2482 Named_type*
2483 type_value()
2485 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2486 return this->u_.type_value;
2489 const Named_type*
2490 type_value() const
2492 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2493 return this->u_.type_value;
2496 Type_declaration*
2497 type_declaration_value()
2499 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2500 return this->u_.type_declaration;
2503 const Type_declaration*
2504 type_declaration_value() const
2506 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2507 return this->u_.type_declaration;
2510 Variable*
2511 var_value()
2513 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2514 return this->u_.var_value;
2517 const Variable*
2518 var_value() const
2520 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2521 return this->u_.var_value;
2524 Result_variable*
2525 result_var_value()
2527 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2528 return this->u_.result_var_value;
2531 const Result_variable*
2532 result_var_value() const
2534 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2535 return this->u_.result_var_value;
2538 Function*
2539 func_value()
2541 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2542 return this->u_.func_value;
2545 const Function*
2546 func_value() const
2548 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2549 return this->u_.func_value;
2552 Function_declaration*
2553 func_declaration_value()
2555 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2556 return this->u_.func_declaration_value;
2559 const Function_declaration*
2560 func_declaration_value() const
2562 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2563 return this->u_.func_declaration_value;
2566 Package*
2567 package_value()
2569 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2570 return this->u_.package_value;
2573 const Package*
2574 package_value() const
2576 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2577 return this->u_.package_value;
2580 const std::string&
2581 name() const
2582 { return this->name_; }
2584 // Return the name to use in an error message. The difference is
2585 // that if this Named_object is defined in a different package, this
2586 // will return PACKAGE.NAME.
2587 std::string
2588 message_name() const;
2590 const Package*
2591 package() const
2592 { return this->package_; }
2594 // Resolve an unknown value if possible. This returns the same
2595 // Named_object or a new one.
2596 Named_object*
2597 resolve()
2599 Named_object* ret = this;
2600 if (this->is_unknown())
2602 Named_object* r = this->unknown_value()->real_named_object();
2603 if (r != NULL)
2604 ret = r;
2606 return ret;
2609 const Named_object*
2610 resolve() const
2612 const Named_object* ret = this;
2613 if (this->is_unknown())
2615 const Named_object* r = this->unknown_value()->real_named_object();
2616 if (r != NULL)
2617 ret = r;
2619 return ret;
2622 // The location where this object was defined or referenced.
2623 Location
2624 location() const;
2626 // Convert a variable to the backend representation.
2627 Bvariable*
2628 get_backend_variable(Gogo*, Named_object* function);
2630 // Return the external identifier for this object.
2631 std::string
2632 get_id(Gogo*);
2634 // Get the backend representation of this object.
2635 void
2636 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2637 std::vector<Bfunction*>&);
2639 // Define a type declaration.
2640 void
2641 set_type_value(Named_type*);
2643 // Define a function declaration.
2644 void
2645 set_function_value(Function*);
2647 // Declare an unknown name as a type declaration.
2648 void
2649 declare_as_type();
2651 // Export this object.
2652 void
2653 export_named_object(Export*) const;
2655 // Mark this named object as an invalid redefinition of another object.
2656 void
2657 set_is_redefinition()
2658 { this->is_redefinition_ = true; }
2660 // Return whether or not this object is a invalid redefinition of another
2661 // object.
2662 bool
2663 is_redefinition() const
2664 { return this->is_redefinition_; }
2666 private:
2667 Named_object(const std::string&, const Package*, Classification);
2669 // The name of the object.
2670 std::string name_;
2671 // The package that this object is in. This is NULL if it is in the
2672 // file we are compiling.
2673 const Package* package_;
2674 // The type of object this is.
2675 Classification classification_;
2676 // The real data.
2677 union
2679 Unknown_name* unknown_value;
2680 Named_constant* const_value;
2681 Named_type* type_value;
2682 Type_declaration* type_declaration;
2683 Variable* var_value;
2684 Result_variable* result_var_value;
2685 Function* func_value;
2686 Function_declaration* func_declaration_value;
2687 Package* package_value;
2688 } u_;
2689 // True if this object is an invalid redefinition of another object.
2690 bool is_redefinition_;
2693 // A binding contour. This binds names to objects.
2695 class Bindings
2697 public:
2698 // Type for mapping from names to objects.
2699 typedef Unordered_map(std::string, Named_object*) Contour;
2701 Bindings(Bindings* enclosing);
2703 // Add an erroneous name.
2704 Named_object*
2705 add_erroneous_name(const std::string& name)
2706 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2708 // Add an unknown name.
2709 Named_object*
2710 add_unknown_name(const std::string& name, Location location)
2712 return this->add_named_object(Named_object::make_unknown_name(name,
2713 location));
2716 // Add a constant.
2717 Named_object*
2718 add_constant(const Typed_identifier& tid, const Package* package,
2719 Expression* expr, int iota_value)
2721 return this->add_named_object(Named_object::make_constant(tid, package,
2722 expr,
2723 iota_value));
2726 // Add a type.
2727 Named_object*
2728 add_type(const std::string& name, const Package* package, Type* type,
2729 Location location)
2731 return this->add_named_object(Named_object::make_type(name, package, type,
2732 location));
2735 // Add a named type. This is used for builtin types, and to add an
2736 // imported type to the global scope.
2737 Named_object*
2738 add_named_type(Named_type* named_type);
2740 // Add a type declaration.
2741 Named_object*
2742 add_type_declaration(const std::string& name, const Package* package,
2743 Location location)
2745 Named_object* no = Named_object::make_type_declaration(name, package,
2746 location);
2747 return this->add_named_object(no);
2750 // Add a variable.
2751 Named_object*
2752 add_variable(const std::string& name, const Package* package,
2753 Variable* variable)
2755 return this->add_named_object(Named_object::make_variable(name, package,
2756 variable));
2759 // Add a result variable.
2760 Named_object*
2761 add_result_variable(const std::string& name, Result_variable* result)
2763 return this->add_named_object(Named_object::make_result_variable(name,
2764 result));
2767 // Add a function.
2768 Named_object*
2769 add_function(const std::string& name, const Package*, Function* function);
2771 // Add a function declaration.
2772 Named_object*
2773 add_function_declaration(const std::string& name, const Package* package,
2774 Function_type* type, Location location);
2776 // Add a package. The location is the location of the import
2777 // statement.
2778 Named_object*
2779 add_package(const std::string& alias, Package* package)
2781 Named_object* no = Named_object::make_package(alias, package);
2782 return this->add_named_object(no);
2785 // Define a type which was already declared.
2786 void
2787 define_type(Named_object*, Named_type*);
2789 // Add a method to the list of objects. This is not added to the
2790 // lookup table.
2791 void
2792 add_method(Named_object*);
2794 // Add a named object to this binding.
2795 Named_object*
2796 add_named_object(Named_object* no)
2797 { return this->add_named_object_to_contour(&this->bindings_, no); }
2799 // Clear all names in file scope from the bindings.
2800 void
2801 clear_file_scope(Gogo*);
2803 // Look up a name in this binding contour and in any enclosing
2804 // binding contours. This returns NULL if the name is not found.
2805 Named_object*
2806 lookup(const std::string&) const;
2808 // Look up a name in this binding contour without looking in any
2809 // enclosing binding contours. Returns NULL if the name is not found.
2810 Named_object*
2811 lookup_local(const std::string&) const;
2813 // Remove a name.
2814 void
2815 remove_binding(Named_object*);
2817 // Mark all variables as used. This is used for some types of parse
2818 // error.
2819 void
2820 mark_locals_used();
2822 // Traverse the tree. See the Traverse class.
2824 traverse(Traverse*, bool is_global);
2826 // Iterate over definitions. This does not include things which
2827 // were only declared.
2829 typedef std::vector<Named_object*>::const_iterator
2830 const_definitions_iterator;
2832 const_definitions_iterator
2833 begin_definitions() const
2834 { return this->named_objects_.begin(); }
2836 const_definitions_iterator
2837 end_definitions() const
2838 { return this->named_objects_.end(); }
2840 // Return the number of definitions.
2841 size_t
2842 size_definitions() const
2843 { return this->named_objects_.size(); }
2845 // Return whether there are no definitions.
2846 bool
2847 empty_definitions() const
2848 { return this->named_objects_.empty(); }
2850 // Iterate over declarations. This is everything that has been
2851 // declared, which includes everything which has been defined.
2853 typedef Contour::const_iterator const_declarations_iterator;
2855 const_declarations_iterator
2856 begin_declarations() const
2857 { return this->bindings_.begin(); }
2859 const_declarations_iterator
2860 end_declarations() const
2861 { return this->bindings_.end(); }
2863 // Return the number of declarations.
2864 size_t
2865 size_declarations() const
2866 { return this->bindings_.size(); }
2868 // Return whether there are no declarations.
2869 bool
2870 empty_declarations() const
2871 { return this->bindings_.empty(); }
2873 // Return the first declaration.
2874 Named_object*
2875 first_declaration()
2876 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2878 private:
2879 Named_object*
2880 add_named_object_to_contour(Contour*, Named_object*);
2882 Named_object*
2883 new_definition(Named_object*, Named_object*);
2885 // Enclosing bindings.
2886 Bindings* enclosing_;
2887 // The list of objects.
2888 std::vector<Named_object*> named_objects_;
2889 // The mapping from names to objects.
2890 Contour bindings_;
2893 // A label.
2895 class Label
2897 public:
2898 Label(const std::string& name)
2899 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2900 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
2903 // Return the label's name.
2904 const std::string&
2905 name() const
2906 { return this->name_; }
2908 // Return whether the label has been defined.
2909 bool
2910 is_defined() const
2911 { return !Linemap::is_unknown_location(this->location_); }
2913 // Return whether the label has been used.
2914 bool
2915 is_used() const
2916 { return this->is_used_; }
2918 // Record that the label is used.
2919 void
2920 set_is_used()
2921 { this->is_used_ = true; }
2923 // Return whether this label is looping.
2924 bool
2925 looping() const
2926 { return this->depth_ == DEPTH_LOOPING; }
2928 // Set this label as looping.
2929 void
2930 set_looping()
2931 { this->depth_ = DEPTH_LOOPING; }
2933 // Return whether this label is nonlooping.
2934 bool
2935 nonlooping() const
2936 { return this->depth_ == DEPTH_NONLOOPING; }
2938 // Set this label as nonlooping.
2939 void
2940 set_nonlooping()
2941 { this->depth_ = DEPTH_NONLOOPING; }
2943 // Return the location of the definition.
2944 Location
2945 location() const
2946 { return this->location_; }
2948 // Return the bindings snapshot.
2949 Bindings_snapshot*
2950 snapshot() const
2951 { return this->snapshot_; }
2953 // Add a snapshot of a goto which refers to this label.
2954 void
2955 add_snapshot_ref(Bindings_snapshot* snapshot)
2957 go_assert(Linemap::is_unknown_location(this->location_));
2958 this->refs_.push_back(snapshot);
2961 // Return the list of snapshots of goto statements which refer to
2962 // this label.
2963 const std::vector<Bindings_snapshot*>&
2964 refs() const
2965 { return this->refs_; }
2967 // Clear the references.
2968 void
2969 clear_refs();
2971 // Define the label at LOCATION with the given bindings snapshot.
2972 void
2973 define(Location location, Bindings_snapshot* snapshot)
2975 if (this->is_dummy_label())
2976 return;
2977 go_assert(Linemap::is_unknown_location(this->location_)
2978 && this->snapshot_ == NULL);
2979 this->location_ = location;
2980 this->snapshot_ = snapshot;
2983 // Return the backend representation for this label.
2984 Blabel*
2985 get_backend_label(Translate_context*);
2987 // Return an expression for the address of this label. This is used
2988 // to get the return address of a deferred function to see whether
2989 // the function may call recover.
2990 Bexpression*
2991 get_addr(Translate_context*, Location location);
2993 // Return a dummy label, representing any instance of the blank label.
2994 static Label*
2995 create_dummy_label();
2997 // Return TRUE if this is a dummy label.
2998 bool
2999 is_dummy_label() const
3000 { return this->name_ == "_"; }
3002 // A classification of a label's looping depth.
3003 enum Loop_depth
3005 DEPTH_UNKNOWN,
3006 // A label never jumped to.
3007 DEPTH_NONLOOPING,
3008 // A label jumped to.
3009 DEPTH_LOOPING
3012 private:
3013 // The name of the label.
3014 std::string name_;
3015 // The location of the definition. This is 0 if the label has not
3016 // yet been defined.
3017 Location location_;
3018 // A snapshot of the set of bindings defined at this label, used to
3019 // issue errors about invalid goto statements.
3020 Bindings_snapshot* snapshot_;
3021 // A list of snapshots of goto statements which refer to this label.
3022 std::vector<Bindings_snapshot*> refs_;
3023 // Whether the label has been used.
3024 bool is_used_;
3025 // The backend representation.
3026 Blabel* blabel_;
3027 // The looping depth of this label, for escape analysis.
3028 Loop_depth depth_;
3031 // An unnamed label. These are used when lowering loops.
3033 class Unnamed_label
3035 public:
3036 Unnamed_label(Location location)
3037 : location_(location), derived_from_(NULL), blabel_(NULL)
3040 // Get the location where the label is defined.
3041 Location
3042 location() const
3043 { return this->location_; }
3045 // Set the location where the label is defined.
3046 void
3047 set_location(Location location)
3048 { this->location_ = location; }
3050 // Get the top level statement this unnamed label is derived from.
3051 Statement*
3052 derived_from() const
3053 { return this->derived_from_; }
3055 // Set the top level statement this unnamed label is derived from.
3056 void
3057 set_derived_from(Statement* s)
3058 { this->derived_from_ = s; }
3060 // Return a statement which defines this label.
3061 Bstatement*
3062 get_definition(Translate_context*);
3064 // Return a goto to this label from LOCATION.
3065 Bstatement*
3066 get_goto(Translate_context*, Location location);
3068 private:
3069 // Return the backend representation.
3070 Blabel*
3071 get_blabel(Translate_context*);
3073 // The location where the label is defined.
3074 Location location_;
3075 // The top-level statement this unnamed label was derived/lowered from.
3076 // This is NULL is this label is not the top-level of a lowered statement.
3077 Statement* derived_from_;
3078 // The backend representation of this label.
3079 Blabel* blabel_;
3082 // An alias for an imported package.
3084 class Package_alias
3086 public:
3087 Package_alias(Location location)
3088 : location_(location), used_(0)
3091 // The location of the import statement.
3092 Location
3093 location()
3094 { return this->location_; }
3096 // How many symbols from the package were used under this alias.
3097 size_t
3098 used() const
3099 { return this->used_; }
3101 // Note that some symbol was used under this alias.
3102 void
3103 note_usage()
3104 { this->used_++; }
3106 private:
3107 // The location of the import statement.
3108 Location location_;
3109 // The amount of times some name from this package was used under this alias.
3110 size_t used_;
3113 // An imported package.
3115 class Package
3117 public:
3118 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3119 Location location);
3121 // Get the package path used for all symbols exported from this
3122 // package.
3123 const std::string&
3124 pkgpath() const
3125 { return this->pkgpath_; }
3127 // Return the package path to use for a symbol name.
3128 std::string
3129 pkgpath_symbol() const;
3131 // Set the package path symbol.
3132 void
3133 set_pkgpath_symbol(const std::string&);
3135 // Return the location of the most recent import statement.
3136 Location
3137 location() const
3138 { return this->location_; }
3140 // Return whether we know the name of this package yet.
3141 bool
3142 has_package_name() const
3143 { return !this->package_name_.empty(); }
3145 // The name that this package uses in its package clause. This may
3146 // be different from the name in the associated Named_object if the
3147 // import statement used an alias.
3148 const std::string&
3149 package_name() const
3151 go_assert(!this->package_name_.empty());
3152 return this->package_name_;
3155 // Return the bindings.
3156 Bindings*
3157 bindings()
3158 { return this->bindings_; }
3160 // Type used to map import names to package aliases.
3161 typedef std::map<std::string, Package_alias*> Aliases;
3163 // Return the set of package aliases.
3164 const Aliases&
3165 aliases() const
3166 { return this->aliases_; }
3168 // Note that some symbol from this package was used and qualified by ALIAS.
3169 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3170 void
3171 note_usage(const std::string& alias) const;
3173 // Note that USAGE might be a fake usage of this package.
3174 void
3175 note_fake_usage(Expression* usage) const
3176 { this->fake_uses_.insert(usage); }
3178 // Forget a given USAGE of this package.
3179 void
3180 forget_usage(Expression* usage) const;
3182 // Clear the used field for the next file.
3183 void
3184 clear_used();
3186 // Look up a name in the package. Returns NULL if the name is not
3187 // found.
3188 Named_object*
3189 lookup(const std::string& name) const
3190 { return this->bindings_->lookup(name); }
3192 // Set the name of the package.
3193 void
3194 set_package_name(const std::string& name, Location);
3196 // Set the location of the package. This is used to record the most
3197 // recent import location.
3198 void
3199 set_location(Location location)
3200 { this->location_ = location; }
3202 // Add a package name as an ALIAS for this package.
3203 Package_alias*
3204 add_alias(const std::string& alias, Location);
3206 // Add a constant to the package.
3207 Named_object*
3208 add_constant(const Typed_identifier& tid, Expression* expr)
3209 { return this->bindings_->add_constant(tid, this, expr, 0); }
3211 // Add a type to the package.
3212 Named_object*
3213 add_type(const std::string& name, Type* type, Location location)
3214 { return this->bindings_->add_type(name, this, type, location); }
3216 // Add a type declaration to the package.
3217 Named_object*
3218 add_type_declaration(const std::string& name, Location location)
3219 { return this->bindings_->add_type_declaration(name, this, location); }
3221 // Add a variable to the package.
3222 Named_object*
3223 add_variable(const std::string& name, Variable* variable)
3224 { return this->bindings_->add_variable(name, this, variable); }
3226 // Add a function declaration to the package.
3227 Named_object*
3228 add_function_declaration(const std::string& name, Function_type* type,
3229 Location loc)
3230 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3232 // Determine types of constants.
3233 void
3234 determine_types();
3236 private:
3237 // The package path for type reflection data.
3238 std::string pkgpath_;
3239 // The package path for symbol names.
3240 std::string pkgpath_symbol_;
3241 // The name that this package uses in the package clause. This may
3242 // be the empty string if it is not yet known.
3243 std::string package_name_;
3244 // The names in this package.
3245 Bindings* bindings_;
3246 // The location of the most recent import statement.
3247 Location location_;
3248 // The set of aliases associated with this package.
3249 Aliases aliases_;
3250 // A set of possibly fake uses of this package. This is mutable because we
3251 // can track fake uses of a package even if we have a const pointer to it.
3252 mutable std::set<Expression*> fake_uses_;
3255 // Return codes for the traversal functions. This is not an enum
3256 // because we want to be able to declare traversal functions in other
3257 // header files without including this one.
3259 // Continue traversal as usual.
3260 const int TRAVERSE_CONTINUE = -1;
3262 // Exit traversal.
3263 const int TRAVERSE_EXIT = 0;
3265 // Continue traversal, but skip components of the current object.
3266 // E.g., if this is returned by Traverse::statement, we do not
3267 // traverse the expressions in the statement even if
3268 // traverse_expressions is set in the traverse_mask.
3269 const int TRAVERSE_SKIP_COMPONENTS = 1;
3271 // This class is used when traversing the parse tree. The caller uses
3272 // a subclass which overrides functions as desired.
3274 class Traverse
3276 public:
3277 // These bitmasks say what to traverse.
3278 static const unsigned int traverse_variables = 0x1;
3279 static const unsigned int traverse_constants = 0x2;
3280 static const unsigned int traverse_functions = 0x4;
3281 static const unsigned int traverse_blocks = 0x8;
3282 static const unsigned int traverse_statements = 0x10;
3283 static const unsigned int traverse_expressions = 0x20;
3284 static const unsigned int traverse_types = 0x40;
3285 static const unsigned int traverse_func_declarations = 0x80;
3287 Traverse(unsigned int traverse_mask)
3288 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3291 virtual ~Traverse();
3293 // The bitmask of what to traverse.
3294 unsigned int
3295 traverse_mask() const
3296 { return this->traverse_mask_; }
3298 // Record that we are going to traverse a type. This returns true
3299 // if the type has already been seen in this traversal. This is
3300 // required because types, unlike expressions, can form a circular
3301 // graph.
3302 bool
3303 remember_type(const Type*);
3305 // Record that we are going to see an expression. This returns true
3306 // if the expression has already been seen in this traversal. This
3307 // is only needed for cases where multiple expressions can point to
3308 // a single one.
3309 bool
3310 remember_expression(const Expression*);
3312 // These functions return one of the TRAVERSE codes defined above.
3314 // If traverse_variables is set in the mask, this is called for
3315 // every variable in the tree.
3316 virtual int
3317 variable(Named_object*);
3319 // If traverse_constants is set in the mask, this is called for
3320 // every named constant in the tree. The bool parameter is true for
3321 // a global constant.
3322 virtual int
3323 constant(Named_object*, bool);
3325 // If traverse_functions is set in the mask, this is called for
3326 // every function in the tree.
3327 virtual int
3328 function(Named_object*);
3330 // If traverse_blocks is set in the mask, this is called for every
3331 // block in the tree.
3332 virtual int
3333 block(Block*);
3335 // If traverse_statements is set in the mask, this is called for
3336 // every statement in the tree.
3337 virtual int
3338 statement(Block*, size_t* index, Statement*);
3340 // If traverse_expressions is set in the mask, this is called for
3341 // every expression in the tree.
3342 virtual int
3343 expression(Expression**);
3345 // If traverse_types is set in the mask, this is called for every
3346 // type in the tree.
3347 virtual int
3348 type(Type*);
3350 // If traverse_func_declarations is set in the mask, this is called
3351 // for every function declarations in the tree.
3352 virtual int
3353 function_declaration(Named_object*);
3355 private:
3356 // A hash table for types we have seen during this traversal. Note
3357 // that this uses the default hash functions for pointers rather
3358 // than Type_hash_identical and Type_identical. This is because for
3359 // traversal we care about seeing a specific type structure. If
3360 // there are two separate instances of identical types, we want to
3361 // traverse both.
3362 typedef Unordered_set(const Type*) Types_seen;
3364 typedef Unordered_set(const Expression*) Expressions_seen;
3366 // Bitmask of what sort of objects to traverse.
3367 unsigned int traverse_mask_;
3368 // Types which have been seen in this traversal.
3369 Types_seen* types_seen_;
3370 // Expressions which have been seen in this traversal.
3371 Expressions_seen* expressions_seen_;
3374 // A class which makes it easier to insert new statements before the
3375 // current statement during a traversal.
3377 class Statement_inserter
3379 public:
3380 // Empty constructor.
3381 Statement_inserter()
3382 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
3385 // Constructor for a statement in a block.
3386 Statement_inserter(Block* block, size_t *pindex)
3387 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
3390 // Constructor for a global variable.
3391 Statement_inserter(Gogo* gogo, Variable* var)
3392 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
3393 { go_assert(var->is_global()); }
3395 // We use the default copy constructor and assignment operator.
3397 // Insert S before the statement we are traversing, or before the
3398 // initialization expression of a global variable.
3399 void
3400 insert(Statement* s);
3402 private:
3403 // The block that the statement is in.
3404 Block* block_;
3405 // The index of the statement that we are traversing.
3406 size_t* pindex_;
3407 // The IR, needed when looking at an initializer expression for a
3408 // global variable.
3409 Gogo* gogo_;
3410 // The global variable, when looking at an initializer expression.
3411 Variable* var_;
3414 // When translating the gogo IR into the backend data structure, this
3415 // is the context we pass down the blocks and statements.
3417 class Translate_context
3419 public:
3420 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3421 Bblock* bblock)
3422 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3423 block_(block), bblock_(bblock), is_const_(false)
3426 // Accessors.
3428 Gogo*
3429 gogo()
3430 { return this->gogo_; }
3432 Backend*
3433 backend()
3434 { return this->backend_; }
3436 Named_object*
3437 function()
3438 { return this->function_; }
3440 Block*
3441 block()
3442 { return this->block_; }
3444 Bblock*
3445 bblock()
3446 { return this->bblock_; }
3448 bool
3449 is_const()
3450 { return this->is_const_; }
3452 // Make a constant context.
3453 void
3454 set_is_const()
3455 { this->is_const_ = true; }
3457 private:
3458 // The IR for the entire compilation unit.
3459 Gogo* gogo_;
3460 // The generator for the backend data structures.
3461 Backend* backend_;
3462 // The function we are currently translating. NULL if not in a
3463 // function, e.g., the initializer of a global variable.
3464 Named_object* function_;
3465 // The block we are currently translating. NULL if not in a
3466 // function.
3467 Block *block_;
3468 // The backend representation of the current block. NULL if block_
3469 // is NULL.
3470 Bblock* bblock_;
3471 // Whether this is being evaluated in a constant context. This is
3472 // used for type descriptor initializers.
3473 bool is_const_;
3476 // Runtime error codes. These must match the values in
3477 // libgo/runtime/go-runtime-error.c.
3479 // Slice index out of bounds: negative or larger than the length of
3480 // the slice.
3481 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3483 // Array index out of bounds.
3484 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3486 // String index out of bounds.
3487 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3489 // Slice slice out of bounds: negative or larger than the length of
3490 // the slice or high bound less than low bound.
3491 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3493 // Array slice out of bounds.
3494 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3496 // String slice out of bounds.
3497 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3499 // Dereference of nil pointer. This is used when there is a
3500 // dereference of a pointer to a very large struct or array, to ensure
3501 // that a gigantic array is not used a proxy to access random memory
3502 // locations.
3503 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3505 // Slice length or capacity out of bounds in make: negative or
3506 // overflow or length greater than capacity.
3507 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3509 // Map capacity out of bounds in make: negative or overflow.
3510 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3512 // Channel capacity out of bounds in make: negative or overflow.
3513 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3515 // Division by zero.
3516 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3518 // Go statement with nil function.
3519 static const int RUNTIME_ERROR_GO_NIL = 11;
3521 // This is used by some of the langhooks.
3522 extern Gogo* go_get_gogo();
3524 // Whether we have seen any errors. FIXME: Replace with a backend
3525 // interface.
3526 extern bool saw_errors();
3528 #endif // !defined(GO_GOGO_H)