compiler: introduce size threshold for nil checks
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blobe48a89926c888e6f8f7564e5acccba59d0fa08f9
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 size threshold used to determine whether to issue
322 // a nil-check for a given pointer dereference. A threshold of -1
323 // implies that all potentially faulting dereference ops should
324 // be nil-checked. A positive threshold of N implies that a deref
325 // of *P where P has size less than N doesn't need a nil check.
326 int64_t
327 nil_check_size_threshold() const
328 { return this->nil_check_size_threshold_; }
330 // Set the nil-check size threshold, as described above.
331 void
332 set_nil_check_size_threshold(int64_t bytes)
333 { this->nil_check_size_threshold_ = bytes; }
335 // Import a package. FILENAME is the file name argument, LOCAL_NAME
336 // is the local name to give to the package. If LOCAL_NAME is empty
337 // the declarations are added to the global scope.
338 void
339 import_package(const std::string& filename, const std::string& local_name,
340 bool is_local_name_exported, bool must_exist, Location);
342 // Whether we are the global binding level.
343 bool
344 in_global_scope() const;
346 // Look up a name in the current binding contours.
347 Named_object*
348 lookup(const std::string&, Named_object** pfunction) const;
350 // Look up a name in the current block.
351 Named_object*
352 lookup_in_block(const std::string&) const;
354 // Look up a name in the global namespace--the universal scope.
355 Named_object*
356 lookup_global(const char*) const;
358 // Add a new imported package. REAL_NAME is the real name of the
359 // package. ALIAS is the alias of the package; this may be the same
360 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
361 // this package should be added to the global namespace; this is
362 // true if the alias is ".". LOCATION is the location of the import
363 // statement. This returns the new package, or NULL on error.
364 Package*
365 add_imported_package(const std::string& real_name, const std::string& alias,
366 bool is_alias_exported,
367 const std::string& pkgpath,
368 const std::string& pkgpath_symbol,
369 Location location,
370 bool* padd_to_globals);
372 // Register a package. This package may or may not be imported.
373 // This returns the Package structure for the package, creating if
374 // it necessary.
375 Package*
376 register_package(const std::string& pkgpath,
377 const std::string& pkgpath_symbol, Location);
379 // Look up a package by pkgpath, and return its pkgpath_symbol.
380 std::string
381 pkgpath_symbol_for_package(const std::string&);
383 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
384 // method function should be added to the type of its receiver.
385 Named_object*
386 start_function(const std::string& name, Function_type* type,
387 bool add_method_to_type, Location);
389 // Finish compiling a function.
390 void
391 finish_function(Location);
393 // Return the current function.
394 Named_object*
395 current_function() const;
397 // Return the current block.
398 Block*
399 current_block();
401 // Start a new block. This is not initially associated with a
402 // function.
403 void
404 start_block(Location);
406 // Finish the current block and return it.
407 Block*
408 finish_block(Location);
410 // Declare an erroneous name. This is used to avoid knock-on errors
411 // after a parsing error.
412 Named_object*
413 add_erroneous_name(const std::string& name);
415 // Declare an unknown name. This is used while parsing. The name
416 // must be resolved by the end of the parse. Unknown names are
417 // always added at the package level.
418 Named_object*
419 add_unknown_name(const std::string& name, Location);
421 // Declare a function.
422 Named_object*
423 declare_function(const std::string&, Function_type*, Location);
425 // Declare a function at the package level. This is used for
426 // functions generated for a type.
427 Named_object*
428 declare_package_function(const std::string&, Function_type*, Location);
430 // Add a label.
431 Label*
432 add_label_definition(const std::string&, Location);
434 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
435 // report errors for a goto from the current location to the label
436 // location.
437 Label*
438 add_label_reference(const std::string&, Location,
439 bool issue_goto_errors);
441 // An analysis set is a list of functions paired with a boolean that indicates
442 // whether the list of functions are recursive.
443 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
445 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
446 // package.
447 void
448 add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
449 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
451 // Return a snapshot of the current binding state.
452 Bindings_snapshot*
453 bindings_snapshot(Location);
455 // Add a statement to the current block.
456 void
457 add_statement(Statement*);
459 // Add a block to the current block.
460 void
461 add_block(Block*, Location);
463 // Add a constant.
464 Named_object*
465 add_constant(const Typed_identifier&, Expression*, int iota_value);
467 // Add a type.
468 void
469 add_type(const std::string&, Type*, Location);
471 // Add a named type. This is used for builtin types, and to add an
472 // imported type to the global scope.
473 void
474 add_named_type(Named_type*);
476 // Declare a type.
477 Named_object*
478 declare_type(const std::string&, Location);
480 // Declare a type at the package level. This is used when the
481 // parser sees an unknown name where a type name is required.
482 Named_object*
483 declare_package_type(const std::string&, Location);
485 // Define a type which was already declared.
486 void
487 define_type(Named_object*, Named_type*);
489 // Add a variable.
490 Named_object*
491 add_variable(const std::string&, Variable*);
493 // Add a sink--a reference to the blank identifier _.
494 Named_object*
495 add_sink();
497 // Add a type which needs to be verified. This is used for sink
498 // types, just to give appropriate error messages.
499 void
500 add_type_to_verify(Type* type);
502 // Add a named object to the current namespace. This is used for
503 // import . "package".
504 void
505 add_dot_import_object(Named_object*);
507 // Add an identifier to the list of names seen in the file block.
508 void
509 add_file_block_name(const std::string& name, Location location)
510 { this->file_block_names_[name] = location; }
512 // Add a linkname, from the go:linkname compiler directive. This
513 // changes the externally visible name of go_name to be ext_name.
514 void
515 add_linkname(const std::string& go_name, bool is_exported,
516 const std::string& ext_name, Location location);
518 // Mark all local variables in current bindings as used. This is
519 // used when there is a parse error to avoid useless errors.
520 void
521 mark_locals_used();
523 // Note that we've seen an interface type. This is used to build
524 // all required interface method tables.
525 void
526 record_interface_type(Interface_type*);
528 // Note that we need an initialization function.
529 void
530 set_need_init_fn()
531 { this->need_init_fn_ = true; }
533 // Return whether the current file imported the unsafe package.
534 bool
535 current_file_imported_unsafe() const
536 { return this->current_file_imported_unsafe_; }
538 // Clear out all names in file scope. This is called when we start
539 // parsing a new file.
540 void
541 clear_file_scope();
543 // Record that VAR1 must be initialized after VAR2. This is used
544 // when VAR2 does not appear in VAR1's INIT or PREINIT.
545 void
546 record_var_depends_on(Variable* var1, Named_object* var2)
548 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
549 this->var_deps_[var1] = var2;
552 // Return the variable that VAR depends on, or NULL if none.
553 Named_object*
554 var_depends_on(Variable* var) const
556 Var_deps::const_iterator p = this->var_deps_.find(var);
557 return p != this->var_deps_.end() ? p->second : NULL;
560 // Queue up a type-specific function to be written out. This is
561 // used when a type-specific function is needed when not at the top
562 // level.
563 void
564 queue_specific_type_function(Type* type, Named_type* name, int64_t size,
565 const std::string& hash_name,
566 Function_type* hash_fntype,
567 const std::string& equal_name,
568 Function_type* equal_fntype);
570 // Write out queued specific type functions.
571 void
572 write_specific_type_functions();
574 // Whether we are done writing out specific type functions.
575 bool
576 specific_type_functions_are_written() const
577 { return this->specific_type_functions_are_written_; }
579 // Add a pointer that needs to be added to the list of objects
580 // traversed by the garbage collector. This should be an expression
581 // of pointer type that points to static storage. It's not
582 // necessary to add global variables to this list, just global
583 // variable initializers that would otherwise not be seen.
584 void
585 add_gc_root(Expression* expr)
587 this->set_need_init_fn();
588 this->gc_roots_.push_back(expr);
591 // Traverse the tree. See the Traverse class.
592 void
593 traverse(Traverse*);
595 // Define the predeclared global names.
596 void
597 define_global_names();
599 // Verify and complete all types.
600 void
601 verify_types();
603 // Lower the parse tree.
604 void
605 lower_parse_tree();
607 // Lower all the statements in a block.
608 void
609 lower_block(Named_object* function, Block*);
611 // Lower an expression.
612 void
613 lower_expression(Named_object* function, Statement_inserter*, Expression**);
615 // Lower a constant.
616 void
617 lower_constant(Named_object*);
619 // Flatten all the statements in a block.
620 void
621 flatten_block(Named_object* function, Block*);
623 // Flatten an expression.
624 void
625 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
627 // Create all necessary function descriptors.
628 void
629 create_function_descriptors();
631 // Finalize the method lists and build stub methods for named types.
632 void
633 finalize_methods();
635 // Work out the types to use for unspecified variables and
636 // constants.
637 void
638 determine_types();
640 // Type check the program.
641 void
642 check_types();
644 // Check the types in a single block. This is used for complicated
645 // go statements.
646 void
647 check_types_in_block(Block*);
649 // Check for return statements.
650 void
651 check_return_statements();
653 // Analyze the program flow for escape information.
654 void
655 analyze_escape();
657 // Discover the groups of possibly recursive functions in this package.
658 void
659 discover_analysis_sets();
661 // Build a connectivity graph between the objects in each analyzed function.
662 void
663 assign_connectivity(Escape_context*, Named_object*);
665 // Traverse the objects in the connecitivty graph from the sink, adjusting the
666 // escape levels of each object.
667 void
668 propagate_escape(Escape_context*, Node*);
670 // Add notes about the escape level of a function's input and output
671 // parameters for exporting and importing top level functions.
672 void
673 tag_function(Escape_context*, Named_object*);
675 // Do all exports.
676 void
677 do_exports();
679 // Add an import control function for an imported package to the
680 // list.
681 void
682 add_import_init_fn(const std::string& package_name,
683 const std::string& init_name, int prio);
685 // Return the Import_init for a given init name.
686 Import_init*
687 lookup_init(const std::string& init_name);
689 // Turn short-cut operators (&&, ||) into explicit if statements.
690 void
691 remove_shortcuts();
693 // Use temporary variables to force order of evaluation.
694 void
695 order_evaluations();
697 // Add write barriers as needed.
698 void
699 add_write_barriers();
701 // Return whether an assignment that sets LHS to RHS needs a write
702 // barrier.
703 bool
704 assign_needs_write_barrier(Expression* lhs);
706 // Return an assignment that sets LHS to RHS using a write barrier.
707 // This returns an if statement that checks whether write barriers
708 // are enabled. If not, it does LHS = RHS, otherwise it calls the
709 // appropriate write barrier function.
710 Statement*
711 assign_with_write_barrier(Function*, Block*, Statement_inserter*,
712 Expression* lhs, Expression* rhs, Location);
714 // Flatten parse tree.
715 void
716 flatten();
718 // Build thunks for functions which call recover.
719 void
720 build_recover_thunks();
722 // Return a declaration for __builtin_return_address or
723 // __builtin_frame_address.
724 static Named_object*
725 declare_builtin_rf_address(const char* name);
727 // Simplify statements which might use thunks: go and defer
728 // statements.
729 void
730 simplify_thunk_statements();
732 // Dump AST if -fgo-dump-ast is set
733 void
734 dump_ast(const char* basename);
736 // Dump Call Graph if -fgo-dump-calls is set.
737 void
738 dump_call_graph(const char* basename);
740 // Dump Connection Graphs if -fgo-dump-connections is set.
741 void
742 dump_connection_graphs(const char* basename);
744 // Convert named types to the backend representation.
745 void
746 convert_named_types();
748 // Convert named types in a list of bindings.
749 void
750 convert_named_types_in_bindings(Bindings*);
752 // True if named types have been converted to the backend
753 // representation.
754 bool
755 named_types_are_converted() const
756 { return this->named_types_are_converted_; }
758 // Give an error if the initialization of VAR depends on itself.
759 void
760 check_self_dep(Named_object*);
762 // Write out the global values.
763 void
764 write_globals();
766 // Build a call to the runtime error function.
767 Expression*
768 runtime_error(int code, Location);
770 // Build required interface method tables.
771 void
772 build_interface_method_tables();
774 // Return an expression which allocates memory to hold values of type TYPE.
775 Expression*
776 allocate_memory(Type *type, Location);
778 // Return the assembler name to use for an exported function, a
779 // method, or a function/method declaration.
780 std::string
781 function_asm_name(const std::string& go_name, const Package*,
782 const Type* receiver);
784 // Return the name to use for a function descriptor.
785 std::string
786 function_descriptor_name(Named_object*);
788 // Return the name to use for a generated stub method.
789 std::string
790 stub_method_name(const std::string& method_name);
792 // Return the names of the hash and equality functions for TYPE.
793 void
794 specific_type_function_names(const Type*, const Named_type*,
795 std::string* hash_name,
796 std::string* equal_name);
798 // Return the assembler name to use for a global variable.
799 std::string
800 global_var_asm_name(const std::string& go_name, const Package*);
802 // Return a name to use for an error case. This should only be used
803 // after reporting an error, and is used to avoid useless knockon
804 // errors.
805 static std::string
806 erroneous_name();
808 // Return whether the name indicates an error.
809 static bool
810 is_erroneous_name(const std::string&);
812 // Return a name to use for a thunk function. A thunk function is
813 // one we create during the compilation, for a go statement or a
814 // defer statement or a method expression.
815 static std::string
816 thunk_name();
818 // Return whether an object is a thunk.
819 static bool
820 is_thunk(const Named_object*);
822 // Return the name to use for an init function.
823 std::string
824 init_function_name();
826 // Return the name to use for a nested function.
827 static std::string
828 nested_function_name();
830 // Return the index of a nested function name.
831 static int
832 nested_function_num(const std::string&);
834 // Return the name to use for a sink funciton.
835 std::string
836 sink_function_name();
838 // Return the name to use for an (erroneous) redefined function.
839 std::string
840 redefined_function_name();
842 // Return the name for use for a recover thunk.
843 std::string
844 recover_thunk_name(const std::string& name, const Type* rtype);
846 // Return the name to use for the GC root variable.
847 std::string
848 gc_root_name();
850 // Return the name to use for a composite literal or string
851 // initializer.
852 std::string
853 initializer_name();
855 // Return the name of the variable used to represent the zero value
856 // of a map.
857 std::string
858 map_zero_value_name();
860 // Get the name of the magic initialization function.
861 const std::string&
862 get_init_fn_name();
864 // Return the name for a type descriptor symbol.
865 std::string
866 type_descriptor_name(Type*, Named_type*);
868 // Return the assembler name for the GC symbol for a type.
869 std::string
870 gc_symbol_name(Type*);
872 // Return the assembler name for a ptrmask variable.
873 std::string
874 ptrmask_symbol_name(const std::string& ptrmask_sym_name);
876 // Return the name to use for an interface method table.
877 std::string
878 interface_method_table_name(Interface_type*, Type*, bool is_pointer);
880 private:
881 // During parsing, we keep a stack of functions. Each function on
882 // the stack is one that we are currently parsing. For each
883 // function, we keep track of the current stack of blocks.
884 struct Open_function
886 // The function.
887 Named_object* function;
888 // The stack of active blocks in the function.
889 std::vector<Block*> blocks;
892 // The stack of functions.
893 typedef std::vector<Open_function> Open_functions;
895 // Set up the built-in unsafe package.
896 void
897 import_unsafe(const std::string&, bool is_exported, Location);
899 // Return the current binding contour.
900 Bindings*
901 current_bindings();
903 const Bindings*
904 current_bindings() const;
906 void
907 write_c_header();
909 // Get the decl for the magic initialization function.
910 Named_object*
911 initialization_function_decl();
913 // Create the magic initialization function.
914 Named_object*
915 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
917 // Initialize imported packages. BFUNCTION is the function
918 // into which the package init calls will be placed.
919 void
920 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
922 // Register variables with the garbage collector.
923 void
924 register_gc_vars(const std::vector<Named_object*>&,
925 std::vector<Bstatement*>&,
926 Bfunction* init_bfunction);
928 Named_object*
929 write_barrier_variable();
931 Statement*
932 check_write_barrier(Block*, Statement*, Statement*);
934 // Type used to map import names to packages.
935 typedef std::map<std::string, Package*> Imports;
937 // Type used to map package names to packages.
938 typedef std::map<std::string, Package*> Packages;
940 // Type used to map variables to the function calls that set them.
941 // This is used for initialization dependency analysis.
942 typedef std::map<Variable*, Named_object*> Var_deps;
944 // Type used to map identifiers in the file block to the location
945 // where they were defined.
946 typedef Unordered_map(std::string, Location) File_block_names;
948 // Type used to queue writing a type specific function.
949 struct Specific_type_function
951 Type* type;
952 Named_type* name;
953 int64_t size;
954 std::string hash_name;
955 Function_type* hash_fntype;
956 std::string equal_name;
957 Function_type* equal_fntype;
959 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
960 const std::string& ahash_name,
961 Function_type* ahash_fntype,
962 const std::string& aequal_name,
963 Function_type* aequal_fntype)
964 : type(atype), name(aname), size(asize), hash_name(ahash_name),
965 hash_fntype(ahash_fntype), equal_name(aequal_name),
966 equal_fntype(aequal_fntype)
970 // Recompute init priorities.
971 void
972 recompute_init_priorities();
974 // Recursive helper used by the routine above.
975 void
976 update_init_priority(Import_init* ii,
977 std::set<const Import_init *>* visited);
979 // The backend generator.
980 Backend* backend_;
981 // The object used to keep track of file names and line numbers.
982 Linemap* linemap_;
983 // The package we are compiling.
984 Package* package_;
985 // The list of currently open functions during parsing.
986 Open_functions functions_;
987 // The global binding contour. This includes the builtin functions
988 // and the package we are compiling.
989 Bindings* globals_;
990 // The list of names we have seen in the file block.
991 File_block_names file_block_names_;
992 // Mapping from import file names to packages.
993 Imports imports_;
994 // Whether the magic unsafe package was imported.
995 bool imported_unsafe_;
996 // Whether the magic unsafe package was imported by the current file.
997 bool current_file_imported_unsafe_;
998 // Mapping from package names we have seen to packages. This does
999 // not include the package we are compiling.
1000 Packages packages_;
1001 // The functions named "init", if there are any.
1002 std::vector<Named_object*> init_functions_;
1003 // A mapping from variables to the function calls that initialize
1004 // them, if it is not stored in the variable's init or preinit.
1005 // This is used for dependency analysis.
1006 Var_deps var_deps_;
1007 // Whether we need a magic initialization function.
1008 bool need_init_fn_;
1009 // The name of the magic initialization function.
1010 std::string init_fn_name_;
1011 // A list of import control variables for packages that we import.
1012 Import_init_set imported_init_fns_;
1013 // The package path used for reflection data.
1014 std::string pkgpath_;
1015 // The package path to use for a symbol name.
1016 std::string pkgpath_symbol_;
1017 // The prefix to use for symbols, from the -fgo-prefix option.
1018 std::string prefix_;
1019 // Whether pkgpath_ has been set.
1020 bool pkgpath_set_;
1021 // Whether an explicit package path was set by -fgo-pkgpath.
1022 bool pkgpath_from_option_;
1023 // Whether an explicit prefix was set by -fgo-prefix.
1024 bool prefix_from_option_;
1025 // The relative import path, from the -fgo-relative-import-path
1026 // option.
1027 std::string relative_import_path_;
1028 // The C header file to write, from the -fgo-c-header option.
1029 std::string c_header_;
1030 // Whether or not to check for division by zero, from the
1031 // -fgo-check-divide-zero option.
1032 bool check_divide_by_zero_;
1033 // Whether or not to check for division overflow, from the
1034 // -fgo-check-divide-overflow option.
1035 bool check_divide_overflow_;
1036 // Whether we are compiling the runtime package, from the
1037 // -fgo-compiling-runtime option.
1038 bool compiling_runtime_;
1039 // The level of escape analysis debug information to emit, from the
1040 // -fgo-debug-escape option.
1041 int debug_escape_level_;
1042 // Nil-check size threshhold.
1043 int64_t nil_check_size_threshold_;
1044 // A list of types to verify.
1045 std::vector<Type*> verify_types_;
1046 // A list of interface types defined while parsing.
1047 std::vector<Interface_type*> interface_types_;
1048 // Type specific functions to write out.
1049 std::vector<Specific_type_function*> specific_type_functions_;
1050 // Whether we are done writing out specific type functions.
1051 bool specific_type_functions_are_written_;
1052 // Whether named types have been converted.
1053 bool named_types_are_converted_;
1054 // A list containing groups of possibly mutually recursive functions to be
1055 // considered during escape analysis.
1056 std::vector<Analysis_set> analysis_sets_;
1057 // A list of objects to add to the GC roots.
1058 std::vector<Expression*> gc_roots_;
1061 // A block of statements.
1063 class Block
1065 public:
1066 Block(Block* enclosing, Location);
1068 // Return the enclosing block.
1069 const Block*
1070 enclosing() const
1071 { return this->enclosing_; }
1073 // Return the bindings of the block.
1074 Bindings*
1075 bindings()
1076 { return this->bindings_; }
1078 const Bindings*
1079 bindings() const
1080 { return this->bindings_; }
1082 // Look at the block's statements.
1083 const std::vector<Statement*>*
1084 statements() const
1085 { return &this->statements_; }
1087 // Return the start location. This is normally the location of the
1088 // left curly brace which starts the block.
1089 Location
1090 start_location() const
1091 { return this->start_location_; }
1093 // Return the end location. This is normally the location of the
1094 // right curly brace which ends the block.
1095 Location
1096 end_location() const
1097 { return this->end_location_; }
1099 // Add a statement to the block.
1100 void
1101 add_statement(Statement*);
1103 // Add a statement to the front of the block.
1104 void
1105 add_statement_at_front(Statement*);
1107 // Replace a statement in a block.
1108 void
1109 replace_statement(size_t index, Statement*);
1111 // Add a Statement before statement number INDEX.
1112 void
1113 insert_statement_before(size_t index, Statement*);
1115 // Add a Statement after statement number INDEX.
1116 void
1117 insert_statement_after(size_t index, Statement*);
1119 // Set the end location of the block.
1120 void
1121 set_end_location(Location location)
1122 { this->end_location_ = location; }
1124 // Traverse the tree.
1126 traverse(Traverse*);
1128 // Set final types for unspecified variables and constants.
1129 void
1130 determine_types();
1132 // Return true if execution of this block may fall through to the
1133 // next block.
1134 bool
1135 may_fall_through() const;
1137 // Convert the block to the backend representation.
1138 Bblock*
1139 get_backend(Translate_context*);
1141 // Iterate over statements.
1143 typedef std::vector<Statement*>::iterator iterator;
1145 iterator
1146 begin()
1147 { return this->statements_.begin(); }
1149 iterator
1150 end()
1151 { return this->statements_.end(); }
1153 private:
1154 // Enclosing block.
1155 Block* enclosing_;
1156 // Statements in the block.
1157 std::vector<Statement*> statements_;
1158 // Binding contour.
1159 Bindings* bindings_;
1160 // Location of start of block.
1161 Location start_location_;
1162 // Location of end of block.
1163 Location end_location_;
1166 // A function.
1168 class Function
1170 public:
1171 Function(Function_type* type, Named_object*, Block*, Location);
1173 // Return the function's type.
1174 Function_type*
1175 type() const
1176 { return this->type_; }
1178 // Return the enclosing function if there is one.
1179 Named_object*
1180 enclosing() const
1181 { return this->enclosing_; }
1183 // Set the enclosing function. This is used when building thunks
1184 // for functions which call recover.
1185 void
1186 set_enclosing(Named_object* enclosing)
1188 go_assert(this->enclosing_ == NULL);
1189 this->enclosing_ = enclosing;
1192 // The result variables.
1193 typedef std::vector<Named_object*> Results;
1195 // Create the result variables in the outer block.
1196 void
1197 create_result_variables(Gogo*);
1199 // Update the named result variables when cloning a function which
1200 // calls recover.
1201 void
1202 update_result_variables();
1204 // Return the result variables.
1205 Results*
1206 result_variables()
1207 { return this->results_; }
1209 bool
1210 is_sink() const
1211 { return this->is_sink_; }
1213 void
1214 set_is_sink()
1215 { this->is_sink_ = true; }
1217 // Whether the result variables have names.
1218 bool
1219 results_are_named() const
1220 { return this->results_are_named_; }
1222 // Set the assembler name.
1223 void
1224 set_asm_name(const std::string& asm_name)
1225 { this->asm_name_ = asm_name; }
1227 // Return the pragmas for this function.
1228 unsigned int
1229 pragmas() const
1230 { return this->pragmas_; }
1232 // Set the pragmas for this function.
1233 void
1234 set_pragmas(unsigned int pragmas)
1236 this->pragmas_ = pragmas;
1239 // Whether this method should not be included in the type
1240 // descriptor.
1241 bool
1242 nointerface() const;
1244 // Record that this method should not be included in the type
1245 // descriptor.
1246 void
1247 set_nointerface();
1249 // Record that this function is a stub method created for an unnamed
1250 // type.
1251 void
1252 set_is_unnamed_type_stub_method()
1254 go_assert(this->is_method());
1255 this->is_unnamed_type_stub_method_ = true;
1258 // Return the amount of enclosed variables in this closure.
1259 size_t
1260 closure_field_count() const
1261 { return this->closure_fields_.size(); }
1263 // Add a new field to the closure variable.
1264 void
1265 add_closure_field(Named_object* var, Location loc)
1266 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1268 // Whether this function needs a closure.
1269 bool
1270 needs_closure() const
1271 { return !this->closure_fields_.empty(); }
1273 // Return the closure variable, creating it if necessary. This is
1274 // passed to the function as a static chain parameter.
1275 Named_object*
1276 closure_var();
1278 // Set the closure variable. This is used when building thunks for
1279 // functions which call recover.
1280 void
1281 set_closure_var(Named_object* v)
1283 go_assert(this->closure_var_ == NULL);
1284 this->closure_var_ = v;
1287 // Return the variable for a reference to field INDEX in the closure
1288 // variable.
1289 Named_object*
1290 enclosing_var(unsigned int index)
1292 go_assert(index < this->closure_fields_.size());
1293 return closure_fields_[index].first;
1296 // Set the type of the closure variable if there is one.
1297 void
1298 set_closure_type();
1300 // Get the block of statements associated with the function.
1301 Block*
1302 block() const
1303 { return this->block_; }
1305 // Get the location of the start of the function.
1306 Location
1307 location() const
1308 { return this->location_; }
1310 // Return whether this function is actually a method.
1311 bool
1312 is_method() const;
1314 // Add a label definition to the function.
1315 Label*
1316 add_label_definition(Gogo*, const std::string& label_name, Location);
1318 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1319 // if we should report errors for a goto from the current location
1320 // to the label location.
1321 Label*
1322 add_label_reference(Gogo*, const std::string& label_name,
1323 Location, bool issue_goto_errors);
1325 // Warn about labels that are defined but not used.
1326 void
1327 check_labels() const;
1329 // Note that a new local type has been added. Return its index.
1330 unsigned int
1331 new_local_type_index()
1332 { return this->local_type_count_++; }
1334 // Whether this function calls the predeclared recover function.
1335 bool
1336 calls_recover() const
1337 { return this->calls_recover_; }
1339 // Record that this function calls the predeclared recover function.
1340 // This is set during the lowering pass.
1341 void
1342 set_calls_recover()
1343 { this->calls_recover_ = true; }
1345 // Whether this is a recover thunk function.
1346 bool
1347 is_recover_thunk() const
1348 { return this->is_recover_thunk_; }
1350 // Record that this is a thunk built for a function which calls
1351 // recover.
1352 void
1353 set_is_recover_thunk()
1354 { this->is_recover_thunk_ = true; }
1356 // Whether this function already has a recover thunk.
1357 bool
1358 has_recover_thunk() const
1359 { return this->has_recover_thunk_; }
1361 // Record that this function already has a recover thunk.
1362 void
1363 set_has_recover_thunk()
1364 { this->has_recover_thunk_ = true; }
1366 // Record that this function is a thunk created for a defer
1367 // statement that calls the __go_set_defer_retaddr runtime function.
1368 void
1369 set_calls_defer_retaddr()
1370 { this->calls_defer_retaddr_ = true; }
1372 // Whether this is a type hash or equality function created by the
1373 // compiler.
1374 bool
1375 is_type_specific_function()
1376 { return this->is_type_specific_function_; }
1378 // Record that this function is a type hash or equality function
1379 // created by the compiler.
1380 void
1381 set_is_type_specific_function()
1382 { this->is_type_specific_function_ = true; }
1384 // Mark the function as going into a unique section.
1385 void
1386 set_in_unique_section()
1387 { this->in_unique_section_ = true; }
1389 // Swap with another function. Used only for the thunk which calls
1390 // recover.
1391 void
1392 swap_for_recover(Function *);
1394 // Traverse the tree.
1396 traverse(Traverse*);
1398 // Determine types in the function.
1399 void
1400 determine_types();
1402 // Return an expression for the function descriptor, given the named
1403 // object for this function. This may only be called for functions
1404 // without a closure. This will be an immutable struct with one
1405 // field that points to the function's code.
1406 Expression*
1407 descriptor(Gogo*, Named_object*);
1409 // Set the descriptor for this function. This is used when a
1410 // function declaration is followed by a function definition.
1411 void
1412 set_descriptor(Expression* descriptor)
1414 go_assert(this->descriptor_ == NULL);
1415 this->descriptor_ = descriptor;
1418 // Return the backend representation.
1419 Bfunction*
1420 get_or_make_decl(Gogo*, Named_object*);
1422 // Return the function's decl after it has been built.
1423 Bfunction*
1424 get_decl() const;
1426 // Set the function decl to hold a backend representation of the function
1427 // code.
1428 void
1429 build(Gogo*, Named_object*);
1431 // Get the statement that assigns values to this function's result struct.
1432 Bstatement*
1433 return_value(Gogo*, Named_object*, Location) const;
1435 // Get an expression for the variable holding the defer stack.
1436 Expression*
1437 defer_stack(Location);
1439 // Export the function.
1440 void
1441 export_func(Export*, const std::string& name) const;
1443 // Export a function with a type.
1444 static void
1445 export_func_with_type(Export*, const std::string& name,
1446 const Function_type*);
1448 // Import a function.
1449 static void
1450 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1451 Typed_identifier_list** pparameters,
1452 Typed_identifier_list** presults, bool* is_varargs);
1454 private:
1455 // Type for mapping from label names to Label objects.
1456 typedef Unordered_map(std::string, Label*) Labels;
1458 void
1459 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1461 typedef std::vector<std::pair<Named_object*,
1462 Location> > Closure_fields;
1464 // The function's type.
1465 Function_type* type_;
1466 // The enclosing function. This is NULL when there isn't one, which
1467 // is the normal case.
1468 Named_object* enclosing_;
1469 // The result variables, if any.
1470 Results* results_;
1471 // If there is a closure, this is the list of variables which appear
1472 // in the closure. This is created by the parser, and then resolved
1473 // to a real type when we lower parse trees.
1474 Closure_fields closure_fields_;
1475 // The closure variable, passed as a parameter using the static
1476 // chain parameter. Normally NULL.
1477 Named_object* closure_var_;
1478 // The outer block of statements in the function.
1479 Block* block_;
1480 // The source location of the start of the function.
1481 Location location_;
1482 // Labels defined or referenced in the function.
1483 Labels labels_;
1484 // The number of local types defined in this function.
1485 unsigned int local_type_count_;
1486 // The assembler name: this is the name that will be put in the object file.
1487 // Set by the go:linkname compiler directive. This is normally empty.
1488 std::string asm_name_;
1489 // The function descriptor, if any.
1490 Expression* descriptor_;
1491 // The function decl.
1492 Bfunction* fndecl_;
1493 // The defer stack variable. A pointer to this variable is used to
1494 // distinguish the defer stack for one function from another. This
1495 // is NULL unless we actually need a defer stack.
1496 Temporary_statement* defer_stack_;
1497 // Pragmas for this function. This is a set of GOPRAGMA bits.
1498 unsigned int pragmas_;
1499 // True if this function is sink-named. No code is generated.
1500 bool is_sink_ : 1;
1501 // True if the result variables are named.
1502 bool results_are_named_ : 1;
1503 // True if this function is a stub method created for an unnamed
1504 // type.
1505 bool is_unnamed_type_stub_method_ : 1;
1506 // True if this function calls the predeclared recover function.
1507 bool calls_recover_ : 1;
1508 // True if this a thunk built for a function which calls recover.
1509 bool is_recover_thunk_ : 1;
1510 // True if this function already has a recover thunk.
1511 bool has_recover_thunk_ : 1;
1512 // True if this is a thunk built for a defer statement that calls
1513 // the __go_set_defer_retaddr runtime function.
1514 bool calls_defer_retaddr_ : 1;
1515 // True if this is a function built by the compiler to as a hash or
1516 // equality function for some type.
1517 bool is_type_specific_function_ : 1;
1518 // True if this function should be put in a unique section. This is
1519 // turned on for field tracking.
1520 bool in_unique_section_ : 1;
1523 // A snapshot of the current binding state.
1525 class Bindings_snapshot
1527 public:
1528 Bindings_snapshot(const Block*, Location);
1530 // Report any errors appropriate for a goto from the current binding
1531 // state of B to this one.
1532 void
1533 check_goto_from(const Block* b, Location);
1535 // Report any errors appropriate for a goto from this binding state
1536 // to the current state of B.
1537 void
1538 check_goto_to(const Block* b);
1540 private:
1541 bool
1542 check_goto_block(Location, const Block*, const Block*, size_t*);
1544 void
1545 check_goto_defs(Location, const Block*, size_t, size_t);
1547 // The current block.
1548 const Block* block_;
1549 // The number of names currently defined in each open block.
1550 // Element 0 is this->block_, element 1 is
1551 // this->block_->enclosing(), etc.
1552 std::vector<size_t> counts_;
1553 // The location where this snapshot was taken.
1554 Location location_;
1557 // A function declaration.
1559 class Function_declaration
1561 public:
1562 Function_declaration(Function_type* fntype, Location location)
1563 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1564 fndecl_(NULL), pragmas_(0)
1567 Function_type*
1568 type() const
1569 { return this->fntype_; }
1571 Location
1572 location() const
1573 { return this->location_; }
1575 const std::string&
1576 asm_name() const
1577 { return this->asm_name_; }
1579 // Set the assembler name.
1580 void
1581 set_asm_name(const std::string& asm_name)
1582 { this->asm_name_ = asm_name; }
1584 // Set the pragmas for this function.
1585 void
1586 set_pragmas(unsigned int pragmas)
1588 this->pragmas_ = pragmas;
1591 // Return an expression for the function descriptor, given the named
1592 // object for this function. This may only be called for functions
1593 // without a closure. This will be an immutable struct with one
1594 // field that points to the function's code.
1595 Expression*
1596 descriptor(Gogo*, Named_object*);
1598 // Return true if we have created a descriptor for this declaration.
1599 bool
1600 has_descriptor() const
1601 { return this->descriptor_ != NULL; }
1603 // Return a backend representation.
1604 Bfunction*
1605 get_or_make_decl(Gogo*, Named_object*);
1607 // If there is a descriptor, build it into the backend
1608 // representation.
1609 void
1610 build_backend_descriptor(Gogo*);
1612 // Export a function declaration.
1613 void
1614 export_func(Export* exp, const std::string& name) const
1615 { Function::export_func_with_type(exp, name, this->fntype_); }
1617 // Check that the types used in this declaration's signature are defined.
1618 void
1619 check_types() const;
1621 private:
1622 // The type of the function.
1623 Function_type* fntype_;
1624 // The location of the declaration.
1625 Location location_;
1626 // The assembler name: this is the name to use in references to the
1627 // function. This is normally empty.
1628 std::string asm_name_;
1629 // The function descriptor, if any.
1630 Expression* descriptor_;
1631 // The function decl if needed.
1632 Bfunction* fndecl_;
1633 // Pragmas for this function. This is a set of GOPRAGMA bits.
1634 unsigned int pragmas_;
1637 // A variable.
1639 class Variable
1641 public:
1642 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1643 bool is_receiver, Location);
1645 // Get the type of the variable.
1646 Type*
1647 type();
1649 Type*
1650 type() const;
1652 // Return whether the type is defined yet.
1653 bool
1654 has_type() const;
1656 // Get the initial value.
1657 Expression*
1658 init() const
1659 { return this->init_; }
1661 // Return whether there are any preinit statements.
1662 bool
1663 has_pre_init() const
1664 { return this->preinit_ != NULL; }
1666 // Return the preinit statements if any.
1667 Block*
1668 preinit() const
1669 { return this->preinit_; }
1671 // Return whether this is a global variable.
1672 bool
1673 is_global() const
1674 { return this->is_global_; }
1676 // Return whether this is a function parameter.
1677 bool
1678 is_parameter() const
1679 { return this->is_parameter_; }
1681 // Return whether this is a closure (static chain) parameter.
1682 bool
1683 is_closure() const
1684 { return this->is_closure_; }
1686 // Change this parameter to be a closure.
1687 void
1688 set_is_closure()
1690 this->is_closure_ = true;
1693 // Return whether this is the receiver parameter of a method.
1694 bool
1695 is_receiver() const
1696 { return this->is_receiver_; }
1698 // Change this parameter to be a receiver. This is used when
1699 // creating the thunks created for functions which call recover.
1700 void
1701 set_is_receiver()
1703 go_assert(this->is_parameter_);
1704 this->is_receiver_ = true;
1707 // Change this parameter to not be a receiver. This is used when
1708 // creating the thunks created for functions which call recover.
1709 void
1710 set_is_not_receiver()
1712 go_assert(this->is_parameter_);
1713 this->is_receiver_ = false;
1716 // Return whether this is the varargs parameter of a function.
1717 bool
1718 is_varargs_parameter() const
1719 { return this->is_varargs_parameter_; }
1721 // Whether this variable's address is taken.
1722 bool
1723 is_address_taken() const
1724 { return this->is_address_taken_; }
1726 // Whether this variable should live in the heap.
1727 bool
1728 is_in_heap() const
1730 return this->is_address_taken_
1731 && this->escapes_
1732 && !this->is_global_;
1735 // Note that something takes the address of this variable.
1736 void
1737 set_address_taken()
1738 { this->is_address_taken_ = true; }
1740 // Return whether the address is taken but does not escape.
1741 bool
1742 is_non_escaping_address_taken() const
1743 { return this->is_non_escaping_address_taken_; }
1745 // Note that something takes the address of this variable such that
1746 // the address does not escape the function.
1747 void
1748 set_non_escaping_address_taken()
1749 { this->is_non_escaping_address_taken_ = true; }
1751 // Return whether this variable escapes the function it is declared in.
1752 bool
1753 escapes()
1754 { return this->escapes_; }
1756 // Note that this variable does not escape the function it is declared in.
1757 void
1758 set_does_not_escape()
1759 { this->escapes_ = false; }
1761 // Get the source location of the variable's declaration.
1762 Location
1763 location() const
1764 { return this->location_; }
1766 // Record that this is the varargs parameter of a function.
1767 void
1768 set_is_varargs_parameter()
1770 go_assert(this->is_parameter_);
1771 this->is_varargs_parameter_ = true;
1774 // Return whether the variable has been used.
1775 bool
1776 is_used() const
1777 { return this->is_used_; }
1779 // Mark that the variable has been used.
1780 void
1781 set_is_used()
1782 { this->is_used_ = true; }
1784 // Clear the initial value; used for error handling and write barriers.
1785 void
1786 clear_init()
1787 { this->init_ = NULL; }
1789 // Set the initial value; used for converting shortcuts.
1790 void
1791 set_init(Expression* init)
1792 { this->init_ = init; }
1794 // Get the preinit block, a block of statements to be run before the
1795 // initialization expression.
1796 Block*
1797 preinit_block(Gogo*);
1799 // Add a statement to be run before the initialization expression.
1800 // This is only used for global variables.
1801 void
1802 add_preinit_statement(Gogo*, Statement*);
1804 // Lower the initialization expression after parsing is complete.
1805 void
1806 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1808 // Flatten the initialization expression after ordering evaluations.
1809 void
1810 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1812 // A special case: the init value is used only to determine the
1813 // type. This is used if the variable is defined using := with the
1814 // comma-ok form of a map index or a receive expression. The init
1815 // value is actually the map index expression or receive expression.
1816 // We use this because we may not know the right type at parse time.
1817 void
1818 set_type_from_init_tuple()
1819 { this->type_from_init_tuple_ = true; }
1821 // Another special case: the init value is used only to determine
1822 // the type. This is used if the variable is defined using := with
1823 // a range clause. The init value is the range expression. The
1824 // type of the variable is the index type of the range expression
1825 // (i.e., the first value returned by a range).
1826 void
1827 set_type_from_range_index()
1828 { this->type_from_range_index_ = true; }
1830 // Another special case: like set_type_from_range_index, but the
1831 // type is the value type of the range expression (i.e., the second
1832 // value returned by a range).
1833 void
1834 set_type_from_range_value()
1835 { this->type_from_range_value_ = true; }
1837 // Another special case: the init value is used only to determine
1838 // the type. This is used if the variable is defined using := with
1839 // a case in a select statement. The init value is the channel.
1840 // The type of the variable is the channel's element type.
1841 void
1842 set_type_from_chan_element()
1843 { this->type_from_chan_element_ = true; }
1845 // After we lower the select statement, we once again set the type
1846 // from the initialization expression.
1847 void
1848 clear_type_from_chan_element()
1850 go_assert(this->type_from_chan_element_);
1851 this->type_from_chan_element_ = false;
1854 // TRUE if this variable was created for a type switch clause.
1855 bool
1856 is_type_switch_var() const
1857 { return this->is_type_switch_var_; }
1859 // Note that this variable was created for a type switch clause.
1860 void
1861 set_is_type_switch_var()
1862 { this->is_type_switch_var_ = true; }
1864 // Mark the variable as going into a unique section.
1865 void
1866 set_in_unique_section()
1868 go_assert(this->is_global_);
1869 this->in_unique_section_ = true;
1872 // Traverse the initializer expression.
1874 traverse_expression(Traverse*, unsigned int traverse_mask);
1876 // Determine the type of the variable if necessary.
1877 void
1878 determine_type();
1880 // Get the backend representation of the variable.
1881 Bvariable*
1882 get_backend_variable(Gogo*, Named_object*, const Package*,
1883 const std::string&);
1885 // Get the initial value of the variable. This may only
1886 // be called if has_pre_init() returns false.
1887 Bexpression*
1888 get_init(Gogo*, Named_object* function);
1890 // Return a series of statements which sets the value of the
1891 // variable in DECL. This should only be called is has_pre_init()
1892 // returns true. DECL may be NULL for a sink variable.
1893 Bstatement*
1894 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1896 // Export the variable.
1897 void
1898 export_var(Export*, const std::string& name) const;
1900 // Import a variable.
1901 static void
1902 import_var(Import*, std::string* pname, Type** ptype);
1904 private:
1905 // The type of a tuple.
1906 Type*
1907 type_from_tuple(Expression*, bool) const;
1909 // The type of a range.
1910 Type*
1911 type_from_range(Expression*, bool, bool) const;
1913 // The element type of a channel.
1914 Type*
1915 type_from_chan_element(Expression*, bool) const;
1917 // The variable's type. This may be NULL if the type is set from
1918 // the expression.
1919 Type* type_;
1920 // The initial value. This may be NULL if the variable should be
1921 // initialized to the default value for the type.
1922 Expression* init_;
1923 // Statements to run before the init statement.
1924 Block* preinit_;
1925 // Location of variable definition.
1926 Location location_;
1927 // Backend representation.
1928 Bvariable* backend_;
1929 // Whether this is a global variable.
1930 bool is_global_ : 1;
1931 // Whether this is a function parameter.
1932 bool is_parameter_ : 1;
1933 // Whether this is a closure parameter.
1934 bool is_closure_ : 1;
1935 // Whether this is the receiver parameter of a method.
1936 bool is_receiver_ : 1;
1937 // Whether this is the varargs parameter of a function.
1938 bool is_varargs_parameter_ : 1;
1939 // Whether this variable is ever referenced.
1940 bool is_used_ : 1;
1941 // Whether something takes the address of this variable. For a
1942 // local variable this implies that the variable has to be on the
1943 // heap if it escapes from its function.
1944 bool is_address_taken_ : 1;
1945 // Whether something takes the address of this variable such that
1946 // the address does not escape the function.
1947 bool is_non_escaping_address_taken_ : 1;
1948 // True if we have seen this variable in a traversal.
1949 bool seen_ : 1;
1950 // True if we have lowered the initialization expression.
1951 bool init_is_lowered_ : 1;
1952 // True if we have flattened the initialization expression.
1953 bool init_is_flattened_ : 1;
1954 // True if init is a tuple used to set the type.
1955 bool type_from_init_tuple_ : 1;
1956 // True if init is a range clause and the type is the index type.
1957 bool type_from_range_index_ : 1;
1958 // True if init is a range clause and the type is the value type.
1959 bool type_from_range_value_ : 1;
1960 // True if init is a channel and the type is the channel's element type.
1961 bool type_from_chan_element_ : 1;
1962 // True if this is a variable created for a type switch case.
1963 bool is_type_switch_var_ : 1;
1964 // True if we have determined types.
1965 bool determined_type_ : 1;
1966 // True if this variable should be put in a unique section. This is
1967 // used for field tracking.
1968 bool in_unique_section_ : 1;
1969 // Whether this variable escapes the function it is created in. This is
1970 // true until shown otherwise.
1971 bool escapes_ : 1;
1974 // A variable which is really the name for a function return value, or
1975 // part of one.
1977 class Result_variable
1979 public:
1980 Result_variable(Type* type, Function* function, int index,
1981 Location location)
1982 : type_(type), function_(function), index_(index), location_(location),
1983 backend_(NULL), is_address_taken_(false),
1984 is_non_escaping_address_taken_(false), escapes_(true)
1987 // Get the type of the result variable.
1988 Type*
1989 type() const
1990 { return this->type_; }
1992 // Get the function that this is associated with.
1993 Function*
1994 function() const
1995 { return this->function_; }
1997 // Index in the list of function results.
1999 index() const
2000 { return this->index_; }
2002 // The location of the variable definition.
2003 Location
2004 location() const
2005 { return this->location_; }
2007 // Whether this variable's address is taken.
2008 bool
2009 is_address_taken() const
2010 { return this->is_address_taken_; }
2012 // Note that something takes the address of this variable.
2013 void
2014 set_address_taken()
2015 { this->is_address_taken_ = true; }
2017 // Return whether the address is taken but does not escape.
2018 bool
2019 is_non_escaping_address_taken() const
2020 { return this->is_non_escaping_address_taken_; }
2022 // Note that something takes the address of this variable such that
2023 // the address does not escape the function.
2024 void
2025 set_non_escaping_address_taken()
2026 { this->is_non_escaping_address_taken_ = true; }
2028 // Return whether this variable escapes the function it is declared in.
2029 bool
2030 escapes()
2031 { return this->escapes_; }
2033 // Note that this variable does not escape the function it is declared in.
2034 void
2035 set_does_not_escape()
2036 { this->escapes_ = false; }
2038 // Whether this variable should live in the heap.
2039 bool
2040 is_in_heap() const
2042 return this->is_address_taken_
2043 && this->escapes_;
2046 // Set the function. This is used when cloning functions which call
2047 // recover.
2048 void
2049 set_function(Function* function)
2050 { this->function_ = function; }
2052 // Get the backend representation of the variable.
2053 Bvariable*
2054 get_backend_variable(Gogo*, Named_object*, const std::string&);
2056 private:
2057 // Type of result variable.
2058 Type* type_;
2059 // Function with which this is associated.
2060 Function* function_;
2061 // Index in list of results.
2062 int index_;
2063 // Where the result variable is defined.
2064 Location location_;
2065 // Backend representation.
2066 Bvariable* backend_;
2067 // Whether something takes the address of this variable.
2068 bool is_address_taken_;
2069 // Whether something takes the address of this variable such that
2070 // the address does not escape the function.
2071 bool is_non_escaping_address_taken_;
2072 // Whether this variable escapes the function it is created in. This is
2073 // true until shown otherwise.
2074 bool escapes_;
2077 // The value we keep for a named constant. This lets us hold a type
2078 // and an expression.
2080 class Named_constant
2082 public:
2083 Named_constant(Type* type, Expression* expr, int iota_value,
2084 Location location)
2085 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2086 lowering_(false), is_sink_(false), bconst_(NULL)
2089 Type*
2090 type() const
2091 { return this->type_; }
2093 Expression*
2094 expr() const
2095 { return this->expr_; }
2098 iota_value() const
2099 { return this->iota_value_; }
2101 Location
2102 location() const
2103 { return this->location_; }
2105 // Whether we are lowering.
2106 bool
2107 lowering() const
2108 { return this->lowering_; }
2110 // Set that we are lowering.
2111 void
2112 set_lowering()
2113 { this->lowering_ = true; }
2115 // We are no longer lowering.
2116 void
2117 clear_lowering()
2118 { this->lowering_ = false; }
2120 bool
2121 is_sink() const
2122 { return this->is_sink_; }
2124 void
2125 set_is_sink()
2126 { this->is_sink_ = true; }
2128 // Traverse the expression.
2130 traverse_expression(Traverse*);
2132 // Determine the type of the constant if necessary.
2133 void
2134 determine_type();
2136 // Indicate that we found and reported an error for this constant.
2137 void
2138 set_error();
2140 // Export the constant.
2141 void
2142 export_const(Export*, const std::string& name) const;
2144 // Import a constant.
2145 static void
2146 import_const(Import*, std::string*, Type**, Expression**);
2148 // Get the backend representation of the constant value.
2149 Bexpression*
2150 get_backend(Gogo*, Named_object*);
2152 private:
2153 // The type of the constant.
2154 Type* type_;
2155 // The expression for the constant.
2156 Expression* expr_;
2157 // If the predeclared constant iota is used in EXPR_, this is the
2158 // value it will have. We do this because at parse time we don't
2159 // know whether the name "iota" will refer to the predeclared
2160 // constant or to something else. We put in the right value in when
2161 // we lower.
2162 int iota_value_;
2163 // The location of the definition.
2164 Location location_;
2165 // Whether we are currently lowering this constant.
2166 bool lowering_;
2167 // Whether this constant is blank named and needs only type checking.
2168 bool is_sink_;
2169 // The backend representation of the constant value.
2170 Bexpression* bconst_;
2173 // A type declaration.
2175 class Type_declaration
2177 public:
2178 Type_declaration(Location location)
2179 : location_(location), in_function_(NULL), in_function_index_(0),
2180 methods_(), issued_warning_(false)
2183 // Return the location.
2184 Location
2185 location() const
2186 { return this->location_; }
2188 // Return the function in which this type is declared. This will
2189 // return NULL for a type declared in global scope.
2190 Named_object*
2191 in_function(unsigned int* pindex)
2193 *pindex = this->in_function_index_;
2194 return this->in_function_;
2197 // Set the function in which this type is declared.
2198 void
2199 set_in_function(Named_object* f, unsigned int index)
2201 this->in_function_ = f;
2202 this->in_function_index_ = index;
2205 // Add a method to this type. This is used when methods are defined
2206 // before the type.
2207 Named_object*
2208 add_method(const std::string& name, Function* function);
2210 // Add a method declaration to this type.
2211 Named_object*
2212 add_method_declaration(const std::string& name, Package*,
2213 Function_type* type, Location location);
2215 // Add an already created object as a method.
2216 void
2217 add_existing_method(Named_object* no)
2218 { this->methods_.push_back(no); }
2220 // Return whether any methods were defined.
2221 bool
2222 has_methods() const;
2224 // Return the methods.
2225 const std::vector<Named_object*>*
2226 methods() const
2227 { return &this->methods_; }
2229 // Define methods when the real type is known.
2230 void
2231 define_methods(Named_type*);
2233 // This is called if we are trying to use this type. It returns
2234 // true if we should issue a warning.
2235 bool
2236 using_type();
2238 private:
2239 // The location of the type declaration.
2240 Location location_;
2241 // If this type is declared in a function, a pointer back to the
2242 // function in which it is defined.
2243 Named_object* in_function_;
2244 // The index of this type in IN_FUNCTION_.
2245 unsigned int in_function_index_;
2246 // Methods defined before the type is defined.
2247 std::vector<Named_object*> methods_;
2248 // True if we have issued a warning about a use of this type
2249 // declaration when it is undefined.
2250 bool issued_warning_;
2253 // An unknown object. These are created by the parser for forward
2254 // references to names which have not been seen before. In a correct
2255 // program, these will always point to a real definition by the end of
2256 // the parse. Because they point to another Named_object, these may
2257 // only be referenced by Unknown_expression objects.
2259 class Unknown_name
2261 public:
2262 Unknown_name(Location location)
2263 : location_(location), real_named_object_(NULL)
2266 // Return the location where this name was first seen.
2267 Location
2268 location() const
2269 { return this->location_; }
2271 // Return the real named object that this points to, or NULL if it
2272 // was never resolved.
2273 Named_object*
2274 real_named_object() const
2275 { return this->real_named_object_; }
2277 // Set the real named object that this points to.
2278 void
2279 set_real_named_object(Named_object* no);
2281 private:
2282 // The location where this name was first seen.
2283 Location location_;
2284 // The real named object when it is known.
2285 Named_object*
2286 real_named_object_;
2289 // A named object named. This is the result of a declaration. We
2290 // don't use a superclass because they all have to be handled
2291 // differently.
2293 class Named_object
2295 public:
2296 enum Classification
2298 // An uninitialized Named_object. We should never see this.
2299 NAMED_OBJECT_UNINITIALIZED,
2300 // An erroneous name. This indicates a parse error, to avoid
2301 // later errors about undefined references.
2302 NAMED_OBJECT_ERRONEOUS,
2303 // An unknown name. This is used for forward references. In a
2304 // correct program, these will all be resolved by the end of the
2305 // parse.
2306 NAMED_OBJECT_UNKNOWN,
2307 // A const.
2308 NAMED_OBJECT_CONST,
2309 // A type.
2310 NAMED_OBJECT_TYPE,
2311 // A forward type declaration.
2312 NAMED_OBJECT_TYPE_DECLARATION,
2313 // A var.
2314 NAMED_OBJECT_VAR,
2315 // A result variable in a function.
2316 NAMED_OBJECT_RESULT_VAR,
2317 // The blank identifier--the special variable named _.
2318 NAMED_OBJECT_SINK,
2319 // A func.
2320 NAMED_OBJECT_FUNC,
2321 // A forward func declaration.
2322 NAMED_OBJECT_FUNC_DECLARATION,
2323 // A package.
2324 NAMED_OBJECT_PACKAGE
2327 // Return the classification.
2328 Classification
2329 classification() const
2330 { return this->classification_; }
2332 // Classifiers.
2334 bool
2335 is_erroneous() const
2336 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2338 bool
2339 is_unknown() const
2340 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2342 bool
2343 is_const() const
2344 { return this->classification_ == NAMED_OBJECT_CONST; }
2346 bool
2347 is_type() const
2348 { return this->classification_ == NAMED_OBJECT_TYPE; }
2350 bool
2351 is_type_declaration() const
2352 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2354 bool
2355 is_variable() const
2356 { return this->classification_ == NAMED_OBJECT_VAR; }
2358 bool
2359 is_result_variable() const
2360 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2362 bool
2363 is_sink() const
2364 { return this->classification_ == NAMED_OBJECT_SINK; }
2366 bool
2367 is_function() const
2368 { return this->classification_ == NAMED_OBJECT_FUNC; }
2370 bool
2371 is_function_declaration() const
2372 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2374 bool
2375 is_package() const
2376 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2378 // Creators.
2380 static Named_object*
2381 make_erroneous_name(const std::string& name)
2382 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2384 static Named_object*
2385 make_unknown_name(const std::string& name, Location);
2387 static Named_object*
2388 make_constant(const Typed_identifier&, const Package*, Expression*,
2389 int iota_value);
2391 static Named_object*
2392 make_type(const std::string&, const Package*, Type*, Location);
2394 static Named_object*
2395 make_type_declaration(const std::string&, const Package*, Location);
2397 static Named_object*
2398 make_variable(const std::string&, const Package*, Variable*);
2400 static Named_object*
2401 make_result_variable(const std::string&, Result_variable*);
2403 static Named_object*
2404 make_sink();
2406 static Named_object*
2407 make_function(const std::string&, const Package*, Function*);
2409 static Named_object*
2410 make_function_declaration(const std::string&, const Package*, Function_type*,
2411 Location);
2413 static Named_object*
2414 make_package(const std::string& alias, Package* package);
2416 // Getters.
2418 Unknown_name*
2419 unknown_value()
2421 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2422 return this->u_.unknown_value;
2425 const Unknown_name*
2426 unknown_value() const
2428 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2429 return this->u_.unknown_value;
2432 Named_constant*
2433 const_value()
2435 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2436 return this->u_.const_value;
2439 const Named_constant*
2440 const_value() const
2442 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2443 return this->u_.const_value;
2446 Named_type*
2447 type_value()
2449 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2450 return this->u_.type_value;
2453 const Named_type*
2454 type_value() const
2456 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2457 return this->u_.type_value;
2460 Type_declaration*
2461 type_declaration_value()
2463 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2464 return this->u_.type_declaration;
2467 const Type_declaration*
2468 type_declaration_value() const
2470 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2471 return this->u_.type_declaration;
2474 Variable*
2475 var_value()
2477 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2478 return this->u_.var_value;
2481 const Variable*
2482 var_value() const
2484 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2485 return this->u_.var_value;
2488 Result_variable*
2489 result_var_value()
2491 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2492 return this->u_.result_var_value;
2495 const Result_variable*
2496 result_var_value() const
2498 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2499 return this->u_.result_var_value;
2502 Function*
2503 func_value()
2505 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2506 return this->u_.func_value;
2509 const Function*
2510 func_value() const
2512 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2513 return this->u_.func_value;
2516 Function_declaration*
2517 func_declaration_value()
2519 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2520 return this->u_.func_declaration_value;
2523 const Function_declaration*
2524 func_declaration_value() const
2526 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2527 return this->u_.func_declaration_value;
2530 Package*
2531 package_value()
2533 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2534 return this->u_.package_value;
2537 const Package*
2538 package_value() const
2540 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2541 return this->u_.package_value;
2544 const std::string&
2545 name() const
2546 { return this->name_; }
2548 // Return the name to use in an error message. The difference is
2549 // that if this Named_object is defined in a different package, this
2550 // will return PACKAGE.NAME.
2551 std::string
2552 message_name() const;
2554 const Package*
2555 package() const
2556 { return this->package_; }
2558 // Resolve an unknown value if possible. This returns the same
2559 // Named_object or a new one.
2560 Named_object*
2561 resolve()
2563 Named_object* ret = this;
2564 if (this->is_unknown())
2566 Named_object* r = this->unknown_value()->real_named_object();
2567 if (r != NULL)
2568 ret = r;
2570 return ret;
2573 const Named_object*
2574 resolve() const
2576 const Named_object* ret = this;
2577 if (this->is_unknown())
2579 const Named_object* r = this->unknown_value()->real_named_object();
2580 if (r != NULL)
2581 ret = r;
2583 return ret;
2586 // The location where this object was defined or referenced.
2587 Location
2588 location() const;
2590 // Convert a variable to the backend representation.
2591 Bvariable*
2592 get_backend_variable(Gogo*, Named_object* function);
2594 // Return the external identifier for this object.
2595 std::string
2596 get_id(Gogo*);
2598 // Get the backend representation of this object.
2599 void
2600 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2601 std::vector<Bfunction*>&);
2603 // Define a type declaration.
2604 void
2605 set_type_value(Named_type*);
2607 // Define a function declaration.
2608 void
2609 set_function_value(Function*);
2611 // Declare an unknown name as a type declaration.
2612 void
2613 declare_as_type();
2615 // Export this object.
2616 void
2617 export_named_object(Export*) const;
2619 // Mark this named object as an invalid redefinition of another object.
2620 void
2621 set_is_redefinition()
2622 { this->is_redefinition_ = true; }
2624 // Return whether or not this object is a invalid redefinition of another
2625 // object.
2626 bool
2627 is_redefinition() const
2628 { return this->is_redefinition_; }
2630 private:
2631 Named_object(const std::string&, const Package*, Classification);
2633 // The name of the object.
2634 std::string name_;
2635 // The package that this object is in. This is NULL if it is in the
2636 // file we are compiling.
2637 const Package* package_;
2638 // The type of object this is.
2639 Classification classification_;
2640 // The real data.
2641 union
2643 Unknown_name* unknown_value;
2644 Named_constant* const_value;
2645 Named_type* type_value;
2646 Type_declaration* type_declaration;
2647 Variable* var_value;
2648 Result_variable* result_var_value;
2649 Function* func_value;
2650 Function_declaration* func_declaration_value;
2651 Package* package_value;
2652 } u_;
2653 // True if this object is an invalid redefinition of another object.
2654 bool is_redefinition_;
2657 // A binding contour. This binds names to objects.
2659 class Bindings
2661 public:
2662 // Type for mapping from names to objects.
2663 typedef Unordered_map(std::string, Named_object*) Contour;
2665 Bindings(Bindings* enclosing);
2667 // Add an erroneous name.
2668 Named_object*
2669 add_erroneous_name(const std::string& name)
2670 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2672 // Add an unknown name.
2673 Named_object*
2674 add_unknown_name(const std::string& name, Location location)
2676 return this->add_named_object(Named_object::make_unknown_name(name,
2677 location));
2680 // Add a constant.
2681 Named_object*
2682 add_constant(const Typed_identifier& tid, const Package* package,
2683 Expression* expr, int iota_value)
2685 return this->add_named_object(Named_object::make_constant(tid, package,
2686 expr,
2687 iota_value));
2690 // Add a type.
2691 Named_object*
2692 add_type(const std::string& name, const Package* package, Type* type,
2693 Location location)
2695 return this->add_named_object(Named_object::make_type(name, package, type,
2696 location));
2699 // Add a named type. This is used for builtin types, and to add an
2700 // imported type to the global scope.
2701 Named_object*
2702 add_named_type(Named_type* named_type);
2704 // Add a type declaration.
2705 Named_object*
2706 add_type_declaration(const std::string& name, const Package* package,
2707 Location location)
2709 Named_object* no = Named_object::make_type_declaration(name, package,
2710 location);
2711 return this->add_named_object(no);
2714 // Add a variable.
2715 Named_object*
2716 add_variable(const std::string& name, const Package* package,
2717 Variable* variable)
2719 return this->add_named_object(Named_object::make_variable(name, package,
2720 variable));
2723 // Add a result variable.
2724 Named_object*
2725 add_result_variable(const std::string& name, Result_variable* result)
2727 return this->add_named_object(Named_object::make_result_variable(name,
2728 result));
2731 // Add a function.
2732 Named_object*
2733 add_function(const std::string& name, const Package*, Function* function);
2735 // Add a function declaration.
2736 Named_object*
2737 add_function_declaration(const std::string& name, const Package* package,
2738 Function_type* type, Location location);
2740 // Add a package. The location is the location of the import
2741 // statement.
2742 Named_object*
2743 add_package(const std::string& alias, Package* package)
2745 Named_object* no = Named_object::make_package(alias, package);
2746 return this->add_named_object(no);
2749 // Define a type which was already declared.
2750 void
2751 define_type(Named_object*, Named_type*);
2753 // Add a method to the list of objects. This is not added to the
2754 // lookup table.
2755 void
2756 add_method(Named_object*);
2758 // Add a named object to this binding.
2759 Named_object*
2760 add_named_object(Named_object* no)
2761 { return this->add_named_object_to_contour(&this->bindings_, no); }
2763 // Clear all names in file scope from the bindings.
2764 void
2765 clear_file_scope(Gogo*);
2767 // Look up a name in this binding contour and in any enclosing
2768 // binding contours. This returns NULL if the name is not found.
2769 Named_object*
2770 lookup(const std::string&) const;
2772 // Look up a name in this binding contour without looking in any
2773 // enclosing binding contours. Returns NULL if the name is not found.
2774 Named_object*
2775 lookup_local(const std::string&) const;
2777 // Remove a name.
2778 void
2779 remove_binding(Named_object*);
2781 // Mark all variables as used. This is used for some types of parse
2782 // error.
2783 void
2784 mark_locals_used();
2786 // Traverse the tree. See the Traverse class.
2788 traverse(Traverse*, bool is_global);
2790 // Iterate over definitions. This does not include things which
2791 // were only declared.
2793 typedef std::vector<Named_object*>::const_iterator
2794 const_definitions_iterator;
2796 const_definitions_iterator
2797 begin_definitions() const
2798 { return this->named_objects_.begin(); }
2800 const_definitions_iterator
2801 end_definitions() const
2802 { return this->named_objects_.end(); }
2804 // Return the number of definitions.
2805 size_t
2806 size_definitions() const
2807 { return this->named_objects_.size(); }
2809 // Return whether there are no definitions.
2810 bool
2811 empty_definitions() const
2812 { return this->named_objects_.empty(); }
2814 // Iterate over declarations. This is everything that has been
2815 // declared, which includes everything which has been defined.
2817 typedef Contour::const_iterator const_declarations_iterator;
2819 const_declarations_iterator
2820 begin_declarations() const
2821 { return this->bindings_.begin(); }
2823 const_declarations_iterator
2824 end_declarations() const
2825 { return this->bindings_.end(); }
2827 // Return the number of declarations.
2828 size_t
2829 size_declarations() const
2830 { return this->bindings_.size(); }
2832 // Return whether there are no declarations.
2833 bool
2834 empty_declarations() const
2835 { return this->bindings_.empty(); }
2837 // Return the first declaration.
2838 Named_object*
2839 first_declaration()
2840 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2842 private:
2843 Named_object*
2844 add_named_object_to_contour(Contour*, Named_object*);
2846 Named_object*
2847 new_definition(Named_object*, Named_object*);
2849 // Enclosing bindings.
2850 Bindings* enclosing_;
2851 // The list of objects.
2852 std::vector<Named_object*> named_objects_;
2853 // The mapping from names to objects.
2854 Contour bindings_;
2857 // A label.
2859 class Label
2861 public:
2862 Label(const std::string& name)
2863 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2864 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
2867 // Return the label's name.
2868 const std::string&
2869 name() const
2870 { return this->name_; }
2872 // Return whether the label has been defined.
2873 bool
2874 is_defined() const
2875 { return !Linemap::is_unknown_location(this->location_); }
2877 // Return whether the label has been used.
2878 bool
2879 is_used() const
2880 { return this->is_used_; }
2882 // Record that the label is used.
2883 void
2884 set_is_used()
2885 { this->is_used_ = true; }
2887 // Return whether this label is looping.
2888 bool
2889 looping() const
2890 { return this->depth_ == DEPTH_LOOPING; }
2892 // Set this label as looping.
2893 void
2894 set_looping()
2895 { this->depth_ = DEPTH_LOOPING; }
2897 // Return whether this label is nonlooping.
2898 bool
2899 nonlooping() const
2900 { return this->depth_ == DEPTH_NONLOOPING; }
2902 // Set this label as nonlooping.
2903 void
2904 set_nonlooping()
2905 { this->depth_ = DEPTH_NONLOOPING; }
2907 // Return the location of the definition.
2908 Location
2909 location() const
2910 { return this->location_; }
2912 // Return the bindings snapshot.
2913 Bindings_snapshot*
2914 snapshot() const
2915 { return this->snapshot_; }
2917 // Add a snapshot of a goto which refers to this label.
2918 void
2919 add_snapshot_ref(Bindings_snapshot* snapshot)
2921 go_assert(Linemap::is_unknown_location(this->location_));
2922 this->refs_.push_back(snapshot);
2925 // Return the list of snapshots of goto statements which refer to
2926 // this label.
2927 const std::vector<Bindings_snapshot*>&
2928 refs() const
2929 { return this->refs_; }
2931 // Clear the references.
2932 void
2933 clear_refs();
2935 // Define the label at LOCATION with the given bindings snapshot.
2936 void
2937 define(Location location, Bindings_snapshot* snapshot)
2939 if (this->is_dummy_label())
2940 return;
2941 go_assert(Linemap::is_unknown_location(this->location_)
2942 && this->snapshot_ == NULL);
2943 this->location_ = location;
2944 this->snapshot_ = snapshot;
2947 // Return the backend representation for this label.
2948 Blabel*
2949 get_backend_label(Translate_context*);
2951 // Return an expression for the address of this label. This is used
2952 // to get the return address of a deferred function to see whether
2953 // the function may call recover.
2954 Bexpression*
2955 get_addr(Translate_context*, Location location);
2957 // Return a dummy label, representing any instance of the blank label.
2958 static Label*
2959 create_dummy_label();
2961 // Return TRUE if this is a dummy label.
2962 bool
2963 is_dummy_label() const
2964 { return this->name_ == "_"; }
2966 // A classification of a label's looping depth.
2967 enum Loop_depth
2969 DEPTH_UNKNOWN,
2970 // A label never jumped to.
2971 DEPTH_NONLOOPING,
2972 // A label jumped to.
2973 DEPTH_LOOPING
2976 private:
2977 // The name of the label.
2978 std::string name_;
2979 // The location of the definition. This is 0 if the label has not
2980 // yet been defined.
2981 Location location_;
2982 // A snapshot of the set of bindings defined at this label, used to
2983 // issue errors about invalid goto statements.
2984 Bindings_snapshot* snapshot_;
2985 // A list of snapshots of goto statements which refer to this label.
2986 std::vector<Bindings_snapshot*> refs_;
2987 // Whether the label has been used.
2988 bool is_used_;
2989 // The backend representation.
2990 Blabel* blabel_;
2991 // The looping depth of this label, for escape analysis.
2992 Loop_depth depth_;
2995 // An unnamed label. These are used when lowering loops.
2997 class Unnamed_label
2999 public:
3000 Unnamed_label(Location location)
3001 : location_(location), derived_from_(NULL), blabel_(NULL)
3004 // Get the location where the label is defined.
3005 Location
3006 location() const
3007 { return this->location_; }
3009 // Set the location where the label is defined.
3010 void
3011 set_location(Location location)
3012 { this->location_ = location; }
3014 // Get the top level statement this unnamed label is derived from.
3015 Statement*
3016 derived_from() const
3017 { return this->derived_from_; }
3019 // Set the top level statement this unnamed label is derived from.
3020 void
3021 set_derived_from(Statement* s)
3022 { this->derived_from_ = s; }
3024 // Return a statement which defines this label.
3025 Bstatement*
3026 get_definition(Translate_context*);
3028 // Return a goto to this label from LOCATION.
3029 Bstatement*
3030 get_goto(Translate_context*, Location location);
3032 private:
3033 // Return the backend representation.
3034 Blabel*
3035 get_blabel(Translate_context*);
3037 // The location where the label is defined.
3038 Location location_;
3039 // The top-level statement this unnamed label was derived/lowered from.
3040 // This is NULL is this label is not the top-level of a lowered statement.
3041 Statement* derived_from_;
3042 // The backend representation of this label.
3043 Blabel* blabel_;
3046 // An alias for an imported package.
3048 class Package_alias
3050 public:
3051 Package_alias(Location location)
3052 : location_(location), used_(0)
3055 // The location of the import statement.
3056 Location
3057 location()
3058 { return this->location_; }
3060 // How many symbols from the package were used under this alias.
3061 size_t
3062 used() const
3063 { return this->used_; }
3065 // Note that some symbol was used under this alias.
3066 void
3067 note_usage()
3068 { this->used_++; }
3070 private:
3071 // The location of the import statement.
3072 Location location_;
3073 // The amount of times some name from this package was used under this alias.
3074 size_t used_;
3077 // An imported package.
3079 class Package
3081 public:
3082 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3083 Location location);
3085 // Get the package path used for all symbols exported from this
3086 // package.
3087 const std::string&
3088 pkgpath() const
3089 { return this->pkgpath_; }
3091 // Return the package path to use for a symbol name.
3092 std::string
3093 pkgpath_symbol() const;
3095 // Set the package path symbol.
3096 void
3097 set_pkgpath_symbol(const std::string&);
3099 // Return the location of the most recent import statement.
3100 Location
3101 location() const
3102 { return this->location_; }
3104 // Return whether we know the name of this package yet.
3105 bool
3106 has_package_name() const
3107 { return !this->package_name_.empty(); }
3109 // The name that this package uses in its package clause. This may
3110 // be different from the name in the associated Named_object if the
3111 // import statement used an alias.
3112 const std::string&
3113 package_name() const
3115 go_assert(!this->package_name_.empty());
3116 return this->package_name_;
3119 // Return the bindings.
3120 Bindings*
3121 bindings()
3122 { return this->bindings_; }
3124 // Type used to map import names to package aliases.
3125 typedef std::map<std::string, Package_alias*> Aliases;
3127 // Return the set of package aliases.
3128 const Aliases&
3129 aliases() const
3130 { return this->aliases_; }
3132 // Note that some symbol from this package was used and qualified by ALIAS.
3133 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3134 void
3135 note_usage(const std::string& alias) const;
3137 // Note that USAGE might be a fake usage of this package.
3138 void
3139 note_fake_usage(Expression* usage) const
3140 { this->fake_uses_.insert(usage); }
3142 // Forget a given USAGE of this package.
3143 void
3144 forget_usage(Expression* usage) const;
3146 // Clear the used field for the next file.
3147 void
3148 clear_used();
3150 // Look up a name in the package. Returns NULL if the name is not
3151 // found.
3152 Named_object*
3153 lookup(const std::string& name) const
3154 { return this->bindings_->lookup(name); }
3156 // Set the name of the package.
3157 void
3158 set_package_name(const std::string& name, Location);
3160 // Set the location of the package. This is used to record the most
3161 // recent import location.
3162 void
3163 set_location(Location location)
3164 { this->location_ = location; }
3166 // Add a package name as an ALIAS for this package.
3167 Package_alias*
3168 add_alias(const std::string& alias, Location);
3170 // Add a constant to the package.
3171 Named_object*
3172 add_constant(const Typed_identifier& tid, Expression* expr)
3173 { return this->bindings_->add_constant(tid, this, expr, 0); }
3175 // Add a type to the package.
3176 Named_object*
3177 add_type(const std::string& name, Type* type, Location location)
3178 { return this->bindings_->add_type(name, this, type, location); }
3180 // Add a type declaration to the package.
3181 Named_object*
3182 add_type_declaration(const std::string& name, Location location)
3183 { return this->bindings_->add_type_declaration(name, this, location); }
3185 // Add a variable to the package.
3186 Named_object*
3187 add_variable(const std::string& name, Variable* variable)
3188 { return this->bindings_->add_variable(name, this, variable); }
3190 // Add a function declaration to the package.
3191 Named_object*
3192 add_function_declaration(const std::string& name, Function_type* type,
3193 Location loc)
3194 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3196 // Determine types of constants.
3197 void
3198 determine_types();
3200 private:
3201 // The package path for type reflection data.
3202 std::string pkgpath_;
3203 // The package path for symbol names.
3204 std::string pkgpath_symbol_;
3205 // The name that this package uses in the package clause. This may
3206 // be the empty string if it is not yet known.
3207 std::string package_name_;
3208 // The names in this package.
3209 Bindings* bindings_;
3210 // The location of the most recent import statement.
3211 Location location_;
3212 // The set of aliases associated with this package.
3213 Aliases aliases_;
3214 // A set of possibly fake uses of this package. This is mutable because we
3215 // can track fake uses of a package even if we have a const pointer to it.
3216 mutable std::set<Expression*> fake_uses_;
3219 // Return codes for the traversal functions. This is not an enum
3220 // because we want to be able to declare traversal functions in other
3221 // header files without including this one.
3223 // Continue traversal as usual.
3224 const int TRAVERSE_CONTINUE = -1;
3226 // Exit traversal.
3227 const int TRAVERSE_EXIT = 0;
3229 // Continue traversal, but skip components of the current object.
3230 // E.g., if this is returned by Traverse::statement, we do not
3231 // traverse the expressions in the statement even if
3232 // traverse_expressions is set in the traverse_mask.
3233 const int TRAVERSE_SKIP_COMPONENTS = 1;
3235 // This class is used when traversing the parse tree. The caller uses
3236 // a subclass which overrides functions as desired.
3238 class Traverse
3240 public:
3241 // These bitmasks say what to traverse.
3242 static const unsigned int traverse_variables = 0x1;
3243 static const unsigned int traverse_constants = 0x2;
3244 static const unsigned int traverse_functions = 0x4;
3245 static const unsigned int traverse_blocks = 0x8;
3246 static const unsigned int traverse_statements = 0x10;
3247 static const unsigned int traverse_expressions = 0x20;
3248 static const unsigned int traverse_types = 0x40;
3250 Traverse(unsigned int traverse_mask)
3251 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3254 virtual ~Traverse();
3256 // The bitmask of what to traverse.
3257 unsigned int
3258 traverse_mask() const
3259 { return this->traverse_mask_; }
3261 // Record that we are going to traverse a type. This returns true
3262 // if the type has already been seen in this traversal. This is
3263 // required because types, unlike expressions, can form a circular
3264 // graph.
3265 bool
3266 remember_type(const Type*);
3268 // Record that we are going to see an expression. This returns true
3269 // if the expression has already been seen in this traversal. This
3270 // is only needed for cases where multiple expressions can point to
3271 // a single one.
3272 bool
3273 remember_expression(const Expression*);
3275 // These functions return one of the TRAVERSE codes defined above.
3277 // If traverse_variables is set in the mask, this is called for
3278 // every variable in the tree.
3279 virtual int
3280 variable(Named_object*);
3282 // If traverse_constants is set in the mask, this is called for
3283 // every named constant in the tree. The bool parameter is true for
3284 // a global constant.
3285 virtual int
3286 constant(Named_object*, bool);
3288 // If traverse_functions is set in the mask, this is called for
3289 // every function in the tree.
3290 virtual int
3291 function(Named_object*);
3293 // If traverse_blocks is set in the mask, this is called for every
3294 // block in the tree.
3295 virtual int
3296 block(Block*);
3298 // If traverse_statements is set in the mask, this is called for
3299 // every statement in the tree.
3300 virtual int
3301 statement(Block*, size_t* index, Statement*);
3303 // If traverse_expressions is set in the mask, this is called for
3304 // every expression in the tree.
3305 virtual int
3306 expression(Expression**);
3308 // If traverse_types is set in the mask, this is called for every
3309 // type in the tree.
3310 virtual int
3311 type(Type*);
3313 private:
3314 // A hash table for types we have seen during this traversal. Note
3315 // that this uses the default hash functions for pointers rather
3316 // than Type_hash_identical and Type_identical. This is because for
3317 // traversal we care about seeing a specific type structure. If
3318 // there are two separate instances of identical types, we want to
3319 // traverse both.
3320 typedef Unordered_set(const Type*) Types_seen;
3322 typedef Unordered_set(const Expression*) Expressions_seen;
3324 // Bitmask of what sort of objects to traverse.
3325 unsigned int traverse_mask_;
3326 // Types which have been seen in this traversal.
3327 Types_seen* types_seen_;
3328 // Expressions which have been seen in this traversal.
3329 Expressions_seen* expressions_seen_;
3332 // A class which makes it easier to insert new statements before the
3333 // current statement during a traversal.
3335 class Statement_inserter
3337 public:
3338 // Empty constructor.
3339 Statement_inserter()
3340 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
3343 // Constructor for a statement in a block.
3344 Statement_inserter(Block* block, size_t *pindex)
3345 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
3348 // Constructor for a global variable.
3349 Statement_inserter(Gogo* gogo, Variable* var)
3350 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
3351 { go_assert(var->is_global()); }
3353 // We use the default copy constructor and assignment operator.
3355 // Insert S before the statement we are traversing, or before the
3356 // initialization expression of a global variable.
3357 void
3358 insert(Statement* s);
3360 private:
3361 // The block that the statement is in.
3362 Block* block_;
3363 // The index of the statement that we are traversing.
3364 size_t* pindex_;
3365 // The IR, needed when looking at an initializer expression for a
3366 // global variable.
3367 Gogo* gogo_;
3368 // The global variable, when looking at an initializer expression.
3369 Variable* var_;
3372 // When translating the gogo IR into the backend data structure, this
3373 // is the context we pass down the blocks and statements.
3375 class Translate_context
3377 public:
3378 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3379 Bblock* bblock)
3380 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3381 block_(block), bblock_(bblock), is_const_(false)
3384 // Accessors.
3386 Gogo*
3387 gogo()
3388 { return this->gogo_; }
3390 Backend*
3391 backend()
3392 { return this->backend_; }
3394 Named_object*
3395 function()
3396 { return this->function_; }
3398 Block*
3399 block()
3400 { return this->block_; }
3402 Bblock*
3403 bblock()
3404 { return this->bblock_; }
3406 bool
3407 is_const()
3408 { return this->is_const_; }
3410 // Make a constant context.
3411 void
3412 set_is_const()
3413 { this->is_const_ = true; }
3415 private:
3416 // The IR for the entire compilation unit.
3417 Gogo* gogo_;
3418 // The generator for the backend data structures.
3419 Backend* backend_;
3420 // The function we are currently translating. NULL if not in a
3421 // function, e.g., the initializer of a global variable.
3422 Named_object* function_;
3423 // The block we are currently translating. NULL if not in a
3424 // function.
3425 Block *block_;
3426 // The backend representation of the current block. NULL if block_
3427 // is NULL.
3428 Bblock* bblock_;
3429 // Whether this is being evaluated in a constant context. This is
3430 // used for type descriptor initializers.
3431 bool is_const_;
3434 // Runtime error codes. These must match the values in
3435 // libgo/runtime/go-runtime-error.c.
3437 // Slice index out of bounds: negative or larger than the length of
3438 // the slice.
3439 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3441 // Array index out of bounds.
3442 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3444 // String index out of bounds.
3445 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3447 // Slice slice out of bounds: negative or larger than the length of
3448 // the slice or high bound less than low bound.
3449 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3451 // Array slice out of bounds.
3452 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3454 // String slice out of bounds.
3455 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3457 // Dereference of nil pointer. This is used when there is a
3458 // dereference of a pointer to a very large struct or array, to ensure
3459 // that a gigantic array is not used a proxy to access random memory
3460 // locations.
3461 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3463 // Slice length or capacity out of bounds in make: negative or
3464 // overflow or length greater than capacity.
3465 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3467 // Map capacity out of bounds in make: negative or overflow.
3468 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3470 // Channel capacity out of bounds in make: negative or overflow.
3471 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3473 // Division by zero.
3474 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3476 // Go statement with nil function.
3477 static const int RUNTIME_ERROR_GO_NIL = 11;
3479 // This is used by some of the langhooks.
3480 extern Gogo* go_get_gogo();
3482 // Whether we have seen any errors. FIXME: Replace with a backend
3483 // interface.
3484 extern bool saw_errors();
3486 #endif // !defined(GO_GOGO_H)