compiler: make comparison operator() methods const
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob338770f5c3c0ebaf15614d71f95af7fe3a13a0cf
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 // Import a package. FILENAME is the file name argument, LOCAL_NAME
322 // is the local name to give to the package. If LOCAL_NAME is empty
323 // the declarations are added to the global scope.
324 void
325 import_package(const std::string& filename, const std::string& local_name,
326 bool is_local_name_exported, bool must_exist, Location);
328 // Whether we are the global binding level.
329 bool
330 in_global_scope() const;
332 // Look up a name in the current binding contours.
333 Named_object*
334 lookup(const std::string&, Named_object** pfunction) const;
336 // Look up a name in the current block.
337 Named_object*
338 lookup_in_block(const std::string&) const;
340 // Look up a name in the global namespace--the universal scope.
341 Named_object*
342 lookup_global(const char*) const;
344 // Add a new imported package. REAL_NAME is the real name of the
345 // package. ALIAS is the alias of the package; this may be the same
346 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
347 // this package should be added to the global namespace; this is
348 // true if the alias is ".". LOCATION is the location of the import
349 // statement. This returns the new package, or NULL on error.
350 Package*
351 add_imported_package(const std::string& real_name, const std::string& alias,
352 bool is_alias_exported,
353 const std::string& pkgpath,
354 const std::string& pkgpath_symbol,
355 Location location,
356 bool* padd_to_globals);
358 // Register a package. This package may or may not be imported.
359 // This returns the Package structure for the package, creating if
360 // it necessary.
361 Package*
362 register_package(const std::string& pkgpath,
363 const std::string& pkgpath_symbol, Location);
365 // Look up a package by pkgpath, and return its pkgpath_symbol.
366 std::string
367 pkgpath_symbol_for_package(const std::string&);
369 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
370 // method function should be added to the type of its receiver.
371 Named_object*
372 start_function(const std::string& name, Function_type* type,
373 bool add_method_to_type, Location);
375 // Finish compiling a function.
376 void
377 finish_function(Location);
379 // Return the current function.
380 Named_object*
381 current_function() const;
383 // Return the current block.
384 Block*
385 current_block();
387 // Start a new block. This is not initially associated with a
388 // function.
389 void
390 start_block(Location);
392 // Finish the current block and return it.
393 Block*
394 finish_block(Location);
396 // Declare an erroneous name. This is used to avoid knock-on errors
397 // after a parsing error.
398 Named_object*
399 add_erroneous_name(const std::string& name);
401 // Declare an unknown name. This is used while parsing. The name
402 // must be resolved by the end of the parse. Unknown names are
403 // always added at the package level.
404 Named_object*
405 add_unknown_name(const std::string& name, Location);
407 // Declare a function.
408 Named_object*
409 declare_function(const std::string&, Function_type*, Location);
411 // Declare a function at the package level. This is used for
412 // functions generated for a type.
413 Named_object*
414 declare_package_function(const std::string&, Function_type*, Location);
416 // Add a label.
417 Label*
418 add_label_definition(const std::string&, Location);
420 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
421 // report errors for a goto from the current location to the label
422 // location.
423 Label*
424 add_label_reference(const std::string&, Location,
425 bool issue_goto_errors);
427 // An analysis set is a list of functions paired with a boolean that indicates
428 // whether the list of functions are recursive.
429 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
431 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
432 // package.
433 void
434 add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
435 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
437 // Return a snapshot of the current binding state.
438 Bindings_snapshot*
439 bindings_snapshot(Location);
441 // Add a statement to the current block.
442 void
443 add_statement(Statement*);
445 // Add a block to the current block.
446 void
447 add_block(Block*, Location);
449 // Add a constant.
450 Named_object*
451 add_constant(const Typed_identifier&, Expression*, int iota_value);
453 // Add a type.
454 void
455 add_type(const std::string&, Type*, Location);
457 // Add a named type. This is used for builtin types, and to add an
458 // imported type to the global scope.
459 void
460 add_named_type(Named_type*);
462 // Declare a type.
463 Named_object*
464 declare_type(const std::string&, Location);
466 // Declare a type at the package level. This is used when the
467 // parser sees an unknown name where a type name is required.
468 Named_object*
469 declare_package_type(const std::string&, Location);
471 // Define a type which was already declared.
472 void
473 define_type(Named_object*, Named_type*);
475 // Add a variable.
476 Named_object*
477 add_variable(const std::string&, Variable*);
479 // Add a sink--a reference to the blank identifier _.
480 Named_object*
481 add_sink();
483 // Add a type which needs to be verified. This is used for sink
484 // types, just to give appropriate error messages.
485 void
486 add_type_to_verify(Type* type);
488 // Add a named object to the current namespace. This is used for
489 // import . "package".
490 void
491 add_dot_import_object(Named_object*);
493 // Add an identifier to the list of names seen in the file block.
494 void
495 add_file_block_name(const std::string& name, Location location)
496 { this->file_block_names_[name] = location; }
498 // Add a linkname, from the go:linkname compiler directive. This
499 // changes the externally visible name of go_name to be ext_name.
500 void
501 add_linkname(const std::string& go_name, bool is_exported,
502 const std::string& ext_name, Location location);
504 // Mark all local variables in current bindings as used. This is
505 // used when there is a parse error to avoid useless errors.
506 void
507 mark_locals_used();
509 // Note that we've seen an interface type. This is used to build
510 // all required interface method tables.
511 void
512 record_interface_type(Interface_type*);
514 // Note that we need an initialization function.
515 void
516 set_need_init_fn()
517 { this->need_init_fn_ = true; }
519 // Return whether the current file imported the unsafe package.
520 bool
521 current_file_imported_unsafe() const
522 { return this->current_file_imported_unsafe_; }
524 // Clear out all names in file scope. This is called when we start
525 // parsing a new file.
526 void
527 clear_file_scope();
529 // Record that VAR1 must be initialized after VAR2. This is used
530 // when VAR2 does not appear in VAR1's INIT or PREINIT.
531 void
532 record_var_depends_on(Variable* var1, Named_object* var2)
534 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
535 this->var_deps_[var1] = var2;
538 // Return the variable that VAR depends on, or NULL if none.
539 Named_object*
540 var_depends_on(Variable* var) const
542 Var_deps::const_iterator p = this->var_deps_.find(var);
543 return p != this->var_deps_.end() ? p->second : NULL;
546 // Queue up a type-specific function to be written out. This is
547 // used when a type-specific function is needed when not at the top
548 // level.
549 void
550 queue_specific_type_function(Type* type, Named_type* name, int64_t size,
551 const std::string& hash_name,
552 Function_type* hash_fntype,
553 const std::string& equal_name,
554 Function_type* equal_fntype);
556 // Write out queued specific type functions.
557 void
558 write_specific_type_functions();
560 // Whether we are done writing out specific type functions.
561 bool
562 specific_type_functions_are_written() const
563 { return this->specific_type_functions_are_written_; }
565 // Add a pointer that needs to be added to the list of objects
566 // traversed by the garbage collector. This should be an expression
567 // of pointer type that points to static storage. It's not
568 // necessary to add global variables to this list, just global
569 // variable initializers that would otherwise not be seen.
570 void
571 add_gc_root(Expression* expr)
573 this->set_need_init_fn();
574 this->gc_roots_.push_back(expr);
577 // Traverse the tree. See the Traverse class.
578 void
579 traverse(Traverse*);
581 // Define the predeclared global names.
582 void
583 define_global_names();
585 // Verify and complete all types.
586 void
587 verify_types();
589 // Lower the parse tree.
590 void
591 lower_parse_tree();
593 // Lower all the statements in a block.
594 void
595 lower_block(Named_object* function, Block*);
597 // Lower an expression.
598 void
599 lower_expression(Named_object* function, Statement_inserter*, Expression**);
601 // Lower a constant.
602 void
603 lower_constant(Named_object*);
605 // Flatten all the statements in a block.
606 void
607 flatten_block(Named_object* function, Block*);
609 // Flatten an expression.
610 void
611 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
613 // Create all necessary function descriptors.
614 void
615 create_function_descriptors();
617 // Finalize the method lists and build stub methods for named types.
618 void
619 finalize_methods();
621 // Work out the types to use for unspecified variables and
622 // constants.
623 void
624 determine_types();
626 // Type check the program.
627 void
628 check_types();
630 // Check the types in a single block. This is used for complicated
631 // go statements.
632 void
633 check_types_in_block(Block*);
635 // Check for return statements.
636 void
637 check_return_statements();
639 // Analyze the program flow for escape information.
640 void
641 analyze_escape();
643 // Discover the groups of possibly recursive functions in this package.
644 void
645 discover_analysis_sets();
647 // Build a connectivity graph between the objects in each analyzed function.
648 void
649 assign_connectivity(Escape_context*, Named_object*);
651 // Traverse the objects in the connecitivty graph from the sink, adjusting the
652 // escape levels of each object.
653 void
654 propagate_escape(Escape_context*, Node*);
656 // Add notes about the escape level of a function's input and output
657 // parameters for exporting and importing top level functions.
658 void
659 tag_function(Escape_context*, Named_object*);
661 // Do all exports.
662 void
663 do_exports();
665 // Add an import control function for an imported package to the
666 // list.
667 void
668 add_import_init_fn(const std::string& package_name,
669 const std::string& init_name, int prio);
671 // Return the Import_init for a given init name.
672 Import_init*
673 lookup_init(const std::string& init_name);
675 // Turn short-cut operators (&&, ||) into explicit if statements.
676 void
677 remove_shortcuts();
679 // Use temporary variables to force order of evaluation.
680 void
681 order_evaluations();
683 // Add write barriers as needed.
684 void
685 add_write_barriers();
687 // Return whether an assignment that sets LHS to RHS needs a write
688 // barrier.
689 bool
690 assign_needs_write_barrier(Expression* lhs);
692 // Return an assignment that sets LHS to RHS using a write barrier.
693 // This returns an if statement that checks whether write barriers
694 // are enabled. If not, it does LHS = RHS, otherwise it calls the
695 // appropriate write barrier function.
696 Statement*
697 assign_with_write_barrier(Function*, Block*, Statement_inserter*,
698 Expression* lhs, Expression* rhs, Location);
700 // Flatten parse tree.
701 void
702 flatten();
704 // Build thunks for functions which call recover.
705 void
706 build_recover_thunks();
708 // Return a declaration for __builtin_return_address or
709 // __builtin_frame_address.
710 static Named_object*
711 declare_builtin_rf_address(const char* name);
713 // Simplify statements which might use thunks: go and defer
714 // statements.
715 void
716 simplify_thunk_statements();
718 // Dump AST if -fgo-dump-ast is set
719 void
720 dump_ast(const char* basename);
722 // Dump Call Graph if -fgo-dump-calls is set.
723 void
724 dump_call_graph(const char* basename);
726 // Dump Connection Graphs if -fgo-dump-connections is set.
727 void
728 dump_connection_graphs(const char* basename);
730 // Convert named types to the backend representation.
731 void
732 convert_named_types();
734 // Convert named types in a list of bindings.
735 void
736 convert_named_types_in_bindings(Bindings*);
738 // True if named types have been converted to the backend
739 // representation.
740 bool
741 named_types_are_converted() const
742 { return this->named_types_are_converted_; }
744 // Give an error if the initialization of VAR depends on itself.
745 void
746 check_self_dep(Named_object*);
748 // Write out the global values.
749 void
750 write_globals();
752 // Build a call to the runtime error function.
753 Expression*
754 runtime_error(int code, Location);
756 // Build required interface method tables.
757 void
758 build_interface_method_tables();
760 // Return an expression which allocates memory to hold values of type TYPE.
761 Expression*
762 allocate_memory(Type *type, Location);
764 // Return the assembler name to use for an exported function, a
765 // method, or a function/method declaration.
766 std::string
767 function_asm_name(const std::string& go_name, const Package*,
768 const Type* receiver);
770 // Return the name to use for a function descriptor.
771 std::string
772 function_descriptor_name(Named_object*);
774 // Return the name to use for a generated stub method.
775 std::string
776 stub_method_name(const std::string& method_name);
778 // Return the names of the hash and equality functions for TYPE.
779 void
780 specific_type_function_names(const Type*, const Named_type*,
781 std::string* hash_name,
782 std::string* equal_name);
784 // Return the assembler name to use for a global variable.
785 std::string
786 global_var_asm_name(const std::string& go_name, const Package*);
788 // Return a name to use for an error case. This should only be used
789 // after reporting an error, and is used to avoid useless knockon
790 // errors.
791 static std::string
792 erroneous_name();
794 // Return whether the name indicates an error.
795 static bool
796 is_erroneous_name(const std::string&);
798 // Return a name to use for a thunk function. A thunk function is
799 // one we create during the compilation, for a go statement or a
800 // defer statement or a method expression.
801 static std::string
802 thunk_name();
804 // Return whether an object is a thunk.
805 static bool
806 is_thunk(const Named_object*);
808 // Return the name to use for an init function.
809 std::string
810 init_function_name();
812 // Return the name to use for a nested function.
813 static std::string
814 nested_function_name();
816 // Return the index of a nested function name.
817 static int
818 nested_function_num(const std::string&);
820 // Return the name to use for a sink funciton.
821 std::string
822 sink_function_name();
824 // Return the name to use for an (erroneous) redefined function.
825 std::string
826 redefined_function_name();
828 // Return the name for use for a recover thunk.
829 std::string
830 recover_thunk_name(const std::string& name, const Type* rtype);
832 // Return the name to use for the GC root variable.
833 std::string
834 gc_root_name();
836 // Return the name to use for a composite literal or string
837 // initializer.
838 std::string
839 initializer_name();
841 // Return the name of the variable used to represent the zero value
842 // of a map.
843 std::string
844 map_zero_value_name();
846 // Get the name of the magic initialization function.
847 const std::string&
848 get_init_fn_name();
850 // Return the name for a type descriptor symbol.
851 std::string
852 type_descriptor_name(Type*, Named_type*);
854 // Return the assembler name for the GC symbol for a type.
855 std::string
856 gc_symbol_name(Type*);
858 // Return the assembler name for a ptrmask variable.
859 std::string
860 ptrmask_symbol_name(const std::string& ptrmask_sym_name);
862 // Return the name to use for an interface method table.
863 std::string
864 interface_method_table_name(Interface_type*, Type*, bool is_pointer);
866 private:
867 // During parsing, we keep a stack of functions. Each function on
868 // the stack is one that we are currently parsing. For each
869 // function, we keep track of the current stack of blocks.
870 struct Open_function
872 // The function.
873 Named_object* function;
874 // The stack of active blocks in the function.
875 std::vector<Block*> blocks;
878 // The stack of functions.
879 typedef std::vector<Open_function> Open_functions;
881 // Set up the built-in unsafe package.
882 void
883 import_unsafe(const std::string&, bool is_exported, Location);
885 // Return the current binding contour.
886 Bindings*
887 current_bindings();
889 const Bindings*
890 current_bindings() const;
892 void
893 write_c_header();
895 // Get the decl for the magic initialization function.
896 Named_object*
897 initialization_function_decl();
899 // Create the magic initialization function.
900 Named_object*
901 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
903 // Initialize imported packages. BFUNCTION is the function
904 // into which the package init calls will be placed.
905 void
906 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
908 // Register variables with the garbage collector.
909 void
910 register_gc_vars(const std::vector<Named_object*>&,
911 std::vector<Bstatement*>&,
912 Bfunction* init_bfunction);
914 Named_object*
915 write_barrier_variable();
917 Statement*
918 check_write_barrier(Block*, Statement*, Statement*);
920 // Type used to map import names to packages.
921 typedef std::map<std::string, Package*> Imports;
923 // Type used to map package names to packages.
924 typedef std::map<std::string, Package*> Packages;
926 // Type used to map variables to the function calls that set them.
927 // This is used for initialization dependency analysis.
928 typedef std::map<Variable*, Named_object*> Var_deps;
930 // Type used to map identifiers in the file block to the location
931 // where they were defined.
932 typedef Unordered_map(std::string, Location) File_block_names;
934 // Type used to queue writing a type specific function.
935 struct Specific_type_function
937 Type* type;
938 Named_type* name;
939 int64_t size;
940 std::string hash_name;
941 Function_type* hash_fntype;
942 std::string equal_name;
943 Function_type* equal_fntype;
945 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
946 const std::string& ahash_name,
947 Function_type* ahash_fntype,
948 const std::string& aequal_name,
949 Function_type* aequal_fntype)
950 : type(atype), name(aname), size(asize), hash_name(ahash_name),
951 hash_fntype(ahash_fntype), equal_name(aequal_name),
952 equal_fntype(aequal_fntype)
956 // Recompute init priorities.
957 void
958 recompute_init_priorities();
960 // Recursive helper used by the routine above.
961 void
962 update_init_priority(Import_init* ii,
963 std::set<const Import_init *>* visited);
965 // The backend generator.
966 Backend* backend_;
967 // The object used to keep track of file names and line numbers.
968 Linemap* linemap_;
969 // The package we are compiling.
970 Package* package_;
971 // The list of currently open functions during parsing.
972 Open_functions functions_;
973 // The global binding contour. This includes the builtin functions
974 // and the package we are compiling.
975 Bindings* globals_;
976 // The list of names we have seen in the file block.
977 File_block_names file_block_names_;
978 // Mapping from import file names to packages.
979 Imports imports_;
980 // Whether the magic unsafe package was imported.
981 bool imported_unsafe_;
982 // Whether the magic unsafe package was imported by the current file.
983 bool current_file_imported_unsafe_;
984 // Mapping from package names we have seen to packages. This does
985 // not include the package we are compiling.
986 Packages packages_;
987 // The functions named "init", if there are any.
988 std::vector<Named_object*> init_functions_;
989 // A mapping from variables to the function calls that initialize
990 // them, if it is not stored in the variable's init or preinit.
991 // This is used for dependency analysis.
992 Var_deps var_deps_;
993 // Whether we need a magic initialization function.
994 bool need_init_fn_;
995 // The name of the magic initialization function.
996 std::string init_fn_name_;
997 // A list of import control variables for packages that we import.
998 Import_init_set imported_init_fns_;
999 // The package path used for reflection data.
1000 std::string pkgpath_;
1001 // The package path to use for a symbol name.
1002 std::string pkgpath_symbol_;
1003 // The prefix to use for symbols, from the -fgo-prefix option.
1004 std::string prefix_;
1005 // Whether pkgpath_ has been set.
1006 bool pkgpath_set_;
1007 // Whether an explicit package path was set by -fgo-pkgpath.
1008 bool pkgpath_from_option_;
1009 // Whether an explicit prefix was set by -fgo-prefix.
1010 bool prefix_from_option_;
1011 // The relative import path, from the -fgo-relative-import-path
1012 // option.
1013 std::string relative_import_path_;
1014 // The C header file to write, from the -fgo-c-header option.
1015 std::string c_header_;
1016 // Whether or not to check for division by zero, from the
1017 // -fgo-check-divide-zero option.
1018 bool check_divide_by_zero_;
1019 // Whether or not to check for division overflow, from the
1020 // -fgo-check-divide-overflow option.
1021 bool check_divide_overflow_;
1022 // Whether we are compiling the runtime package, from the
1023 // -fgo-compiling-runtime option.
1024 bool compiling_runtime_;
1025 // The level of escape analysis debug information to emit, from the
1026 // -fgo-debug-escape option.
1027 int debug_escape_level_;
1028 // A list of types to verify.
1029 std::vector<Type*> verify_types_;
1030 // A list of interface types defined while parsing.
1031 std::vector<Interface_type*> interface_types_;
1032 // Type specific functions to write out.
1033 std::vector<Specific_type_function*> specific_type_functions_;
1034 // Whether we are done writing out specific type functions.
1035 bool specific_type_functions_are_written_;
1036 // Whether named types have been converted.
1037 bool named_types_are_converted_;
1038 // A list containing groups of possibly mutually recursive functions to be
1039 // considered during escape analysis.
1040 std::vector<Analysis_set> analysis_sets_;
1041 // A list of objects to add to the GC roots.
1042 std::vector<Expression*> gc_roots_;
1045 // A block of statements.
1047 class Block
1049 public:
1050 Block(Block* enclosing, Location);
1052 // Return the enclosing block.
1053 const Block*
1054 enclosing() const
1055 { return this->enclosing_; }
1057 // Return the bindings of the block.
1058 Bindings*
1059 bindings()
1060 { return this->bindings_; }
1062 const Bindings*
1063 bindings() const
1064 { return this->bindings_; }
1066 // Look at the block's statements.
1067 const std::vector<Statement*>*
1068 statements() const
1069 { return &this->statements_; }
1071 // Return the start location. This is normally the location of the
1072 // left curly brace which starts the block.
1073 Location
1074 start_location() const
1075 { return this->start_location_; }
1077 // Return the end location. This is normally the location of the
1078 // right curly brace which ends the block.
1079 Location
1080 end_location() const
1081 { return this->end_location_; }
1083 // Add a statement to the block.
1084 void
1085 add_statement(Statement*);
1087 // Add a statement to the front of the block.
1088 void
1089 add_statement_at_front(Statement*);
1091 // Replace a statement in a block.
1092 void
1093 replace_statement(size_t index, Statement*);
1095 // Add a Statement before statement number INDEX.
1096 void
1097 insert_statement_before(size_t index, Statement*);
1099 // Add a Statement after statement number INDEX.
1100 void
1101 insert_statement_after(size_t index, Statement*);
1103 // Set the end location of the block.
1104 void
1105 set_end_location(Location location)
1106 { this->end_location_ = location; }
1108 // Traverse the tree.
1110 traverse(Traverse*);
1112 // Set final types for unspecified variables and constants.
1113 void
1114 determine_types();
1116 // Return true if execution of this block may fall through to the
1117 // next block.
1118 bool
1119 may_fall_through() const;
1121 // Convert the block to the backend representation.
1122 Bblock*
1123 get_backend(Translate_context*);
1125 // Iterate over statements.
1127 typedef std::vector<Statement*>::iterator iterator;
1129 iterator
1130 begin()
1131 { return this->statements_.begin(); }
1133 iterator
1134 end()
1135 { return this->statements_.end(); }
1137 private:
1138 // Enclosing block.
1139 Block* enclosing_;
1140 // Statements in the block.
1141 std::vector<Statement*> statements_;
1142 // Binding contour.
1143 Bindings* bindings_;
1144 // Location of start of block.
1145 Location start_location_;
1146 // Location of end of block.
1147 Location end_location_;
1150 // A function.
1152 class Function
1154 public:
1155 Function(Function_type* type, Named_object*, Block*, Location);
1157 // Return the function's type.
1158 Function_type*
1159 type() const
1160 { return this->type_; }
1162 // Return the enclosing function if there is one.
1163 Named_object*
1164 enclosing() const
1165 { return this->enclosing_; }
1167 // Set the enclosing function. This is used when building thunks
1168 // for functions which call recover.
1169 void
1170 set_enclosing(Named_object* enclosing)
1172 go_assert(this->enclosing_ == NULL);
1173 this->enclosing_ = enclosing;
1176 // The result variables.
1177 typedef std::vector<Named_object*> Results;
1179 // Create the result variables in the outer block.
1180 void
1181 create_result_variables(Gogo*);
1183 // Update the named result variables when cloning a function which
1184 // calls recover.
1185 void
1186 update_result_variables();
1188 // Return the result variables.
1189 Results*
1190 result_variables()
1191 { return this->results_; }
1193 bool
1194 is_sink() const
1195 { return this->is_sink_; }
1197 void
1198 set_is_sink()
1199 { this->is_sink_ = true; }
1201 // Whether the result variables have names.
1202 bool
1203 results_are_named() const
1204 { return this->results_are_named_; }
1206 // Set the assembler name.
1207 void
1208 set_asm_name(const std::string& asm_name)
1209 { this->asm_name_ = asm_name; }
1211 // Return the pragmas for this function.
1212 unsigned int
1213 pragmas() const
1214 { return this->pragmas_; }
1216 // Set the pragmas for this function.
1217 void
1218 set_pragmas(unsigned int pragmas)
1220 this->pragmas_ = pragmas;
1223 // Whether this method should not be included in the type
1224 // descriptor.
1225 bool
1226 nointerface() const;
1228 // Record that this method should not be included in the type
1229 // descriptor.
1230 void
1231 set_nointerface();
1233 // Record that this function is a stub method created for an unnamed
1234 // type.
1235 void
1236 set_is_unnamed_type_stub_method()
1238 go_assert(this->is_method());
1239 this->is_unnamed_type_stub_method_ = true;
1242 // Return the amount of enclosed variables in this closure.
1243 size_t
1244 closure_field_count() const
1245 { return this->closure_fields_.size(); }
1247 // Add a new field to the closure variable.
1248 void
1249 add_closure_field(Named_object* var, Location loc)
1250 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1252 // Whether this function needs a closure.
1253 bool
1254 needs_closure() const
1255 { return !this->closure_fields_.empty(); }
1257 // Return the closure variable, creating it if necessary. This is
1258 // passed to the function as a static chain parameter.
1259 Named_object*
1260 closure_var();
1262 // Set the closure variable. This is used when building thunks for
1263 // functions which call recover.
1264 void
1265 set_closure_var(Named_object* v)
1267 go_assert(this->closure_var_ == NULL);
1268 this->closure_var_ = v;
1271 // Return the variable for a reference to field INDEX in the closure
1272 // variable.
1273 Named_object*
1274 enclosing_var(unsigned int index)
1276 go_assert(index < this->closure_fields_.size());
1277 return closure_fields_[index].first;
1280 // Set the type of the closure variable if there is one.
1281 void
1282 set_closure_type();
1284 // Get the block of statements associated with the function.
1285 Block*
1286 block() const
1287 { return this->block_; }
1289 // Get the location of the start of the function.
1290 Location
1291 location() const
1292 { return this->location_; }
1294 // Return whether this function is actually a method.
1295 bool
1296 is_method() const;
1298 // Add a label definition to the function.
1299 Label*
1300 add_label_definition(Gogo*, const std::string& label_name, Location);
1302 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1303 // if we should report errors for a goto from the current location
1304 // to the label location.
1305 Label*
1306 add_label_reference(Gogo*, const std::string& label_name,
1307 Location, bool issue_goto_errors);
1309 // Warn about labels that are defined but not used.
1310 void
1311 check_labels() const;
1313 // Note that a new local type has been added. Return its index.
1314 unsigned int
1315 new_local_type_index()
1316 { return this->local_type_count_++; }
1318 // Whether this function calls the predeclared recover function.
1319 bool
1320 calls_recover() const
1321 { return this->calls_recover_; }
1323 // Record that this function calls the predeclared recover function.
1324 // This is set during the lowering pass.
1325 void
1326 set_calls_recover()
1327 { this->calls_recover_ = true; }
1329 // Whether this is a recover thunk function.
1330 bool
1331 is_recover_thunk() const
1332 { return this->is_recover_thunk_; }
1334 // Record that this is a thunk built for a function which calls
1335 // recover.
1336 void
1337 set_is_recover_thunk()
1338 { this->is_recover_thunk_ = true; }
1340 // Whether this function already has a recover thunk.
1341 bool
1342 has_recover_thunk() const
1343 { return this->has_recover_thunk_; }
1345 // Record that this function already has a recover thunk.
1346 void
1347 set_has_recover_thunk()
1348 { this->has_recover_thunk_ = true; }
1350 // Record that this function is a thunk created for a defer
1351 // statement that calls the __go_set_defer_retaddr runtime function.
1352 void
1353 set_calls_defer_retaddr()
1354 { this->calls_defer_retaddr_ = true; }
1356 // Whether this is a type hash or equality function created by the
1357 // compiler.
1358 bool
1359 is_type_specific_function()
1360 { return this->is_type_specific_function_; }
1362 // Record that this function is a type hash or equality function
1363 // created by the compiler.
1364 void
1365 set_is_type_specific_function()
1366 { this->is_type_specific_function_ = true; }
1368 // Mark the function as going into a unique section.
1369 void
1370 set_in_unique_section()
1371 { this->in_unique_section_ = true; }
1373 // Swap with another function. Used only for the thunk which calls
1374 // recover.
1375 void
1376 swap_for_recover(Function *);
1378 // Traverse the tree.
1380 traverse(Traverse*);
1382 // Determine types in the function.
1383 void
1384 determine_types();
1386 // Return an expression for the function descriptor, given the named
1387 // object for this function. This may only be called for functions
1388 // without a closure. This will be an immutable struct with one
1389 // field that points to the function's code.
1390 Expression*
1391 descriptor(Gogo*, Named_object*);
1393 // Set the descriptor for this function. This is used when a
1394 // function declaration is followed by a function definition.
1395 void
1396 set_descriptor(Expression* descriptor)
1398 go_assert(this->descriptor_ == NULL);
1399 this->descriptor_ = descriptor;
1402 // Return the backend representation.
1403 Bfunction*
1404 get_or_make_decl(Gogo*, Named_object*);
1406 // Return the function's decl after it has been built.
1407 Bfunction*
1408 get_decl() const;
1410 // Set the function decl to hold a backend representation of the function
1411 // code.
1412 void
1413 build(Gogo*, Named_object*);
1415 // Get the statement that assigns values to this function's result struct.
1416 Bstatement*
1417 return_value(Gogo*, Named_object*, Location) const;
1419 // Get an expression for the variable holding the defer stack.
1420 Expression*
1421 defer_stack(Location);
1423 // Export the function.
1424 void
1425 export_func(Export*, const std::string& name) const;
1427 // Export a function with a type.
1428 static void
1429 export_func_with_type(Export*, const std::string& name,
1430 const Function_type*);
1432 // Import a function.
1433 static void
1434 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1435 Typed_identifier_list** pparameters,
1436 Typed_identifier_list** presults, bool* is_varargs);
1438 private:
1439 // Type for mapping from label names to Label objects.
1440 typedef Unordered_map(std::string, Label*) Labels;
1442 void
1443 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1445 typedef std::vector<std::pair<Named_object*,
1446 Location> > Closure_fields;
1448 // The function's type.
1449 Function_type* type_;
1450 // The enclosing function. This is NULL when there isn't one, which
1451 // is the normal case.
1452 Named_object* enclosing_;
1453 // The result variables, if any.
1454 Results* results_;
1455 // If there is a closure, this is the list of variables which appear
1456 // in the closure. This is created by the parser, and then resolved
1457 // to a real type when we lower parse trees.
1458 Closure_fields closure_fields_;
1459 // The closure variable, passed as a parameter using the static
1460 // chain parameter. Normally NULL.
1461 Named_object* closure_var_;
1462 // The outer block of statements in the function.
1463 Block* block_;
1464 // The source location of the start of the function.
1465 Location location_;
1466 // Labels defined or referenced in the function.
1467 Labels labels_;
1468 // The number of local types defined in this function.
1469 unsigned int local_type_count_;
1470 // The assembler name: this is the name that will be put in the object file.
1471 // Set by the go:linkname compiler directive. This is normally empty.
1472 std::string asm_name_;
1473 // The function descriptor, if any.
1474 Expression* descriptor_;
1475 // The function decl.
1476 Bfunction* fndecl_;
1477 // The defer stack variable. A pointer to this variable is used to
1478 // distinguish the defer stack for one function from another. This
1479 // is NULL unless we actually need a defer stack.
1480 Temporary_statement* defer_stack_;
1481 // Pragmas for this function. This is a set of GOPRAGMA bits.
1482 unsigned int pragmas_;
1483 // True if this function is sink-named. No code is generated.
1484 bool is_sink_ : 1;
1485 // True if the result variables are named.
1486 bool results_are_named_ : 1;
1487 // True if this function is a stub method created for an unnamed
1488 // type.
1489 bool is_unnamed_type_stub_method_ : 1;
1490 // True if this function calls the predeclared recover function.
1491 bool calls_recover_ : 1;
1492 // True if this a thunk built for a function which calls recover.
1493 bool is_recover_thunk_ : 1;
1494 // True if this function already has a recover thunk.
1495 bool has_recover_thunk_ : 1;
1496 // True if this is a thunk built for a defer statement that calls
1497 // the __go_set_defer_retaddr runtime function.
1498 bool calls_defer_retaddr_ : 1;
1499 // True if this is a function built by the compiler to as a hash or
1500 // equality function for some type.
1501 bool is_type_specific_function_ : 1;
1502 // True if this function should be put in a unique section. This is
1503 // turned on for field tracking.
1504 bool in_unique_section_ : 1;
1507 // A snapshot of the current binding state.
1509 class Bindings_snapshot
1511 public:
1512 Bindings_snapshot(const Block*, Location);
1514 // Report any errors appropriate for a goto from the current binding
1515 // state of B to this one.
1516 void
1517 check_goto_from(const Block* b, Location);
1519 // Report any errors appropriate for a goto from this binding state
1520 // to the current state of B.
1521 void
1522 check_goto_to(const Block* b);
1524 private:
1525 bool
1526 check_goto_block(Location, const Block*, const Block*, size_t*);
1528 void
1529 check_goto_defs(Location, const Block*, size_t, size_t);
1531 // The current block.
1532 const Block* block_;
1533 // The number of names currently defined in each open block.
1534 // Element 0 is this->block_, element 1 is
1535 // this->block_->enclosing(), etc.
1536 std::vector<size_t> counts_;
1537 // The location where this snapshot was taken.
1538 Location location_;
1541 // A function declaration.
1543 class Function_declaration
1545 public:
1546 Function_declaration(Function_type* fntype, Location location)
1547 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1548 fndecl_(NULL), pragmas_(0)
1551 Function_type*
1552 type() const
1553 { return this->fntype_; }
1555 Location
1556 location() const
1557 { return this->location_; }
1559 const std::string&
1560 asm_name() const
1561 { return this->asm_name_; }
1563 // Set the assembler name.
1564 void
1565 set_asm_name(const std::string& asm_name)
1566 { this->asm_name_ = asm_name; }
1568 // Set the pragmas for this function.
1569 void
1570 set_pragmas(unsigned int pragmas)
1572 this->pragmas_ = pragmas;
1575 // Return an expression for the function descriptor, given the named
1576 // object for this function. This may only be called for functions
1577 // without a closure. This will be an immutable struct with one
1578 // field that points to the function's code.
1579 Expression*
1580 descriptor(Gogo*, Named_object*);
1582 // Return true if we have created a descriptor for this declaration.
1583 bool
1584 has_descriptor() const
1585 { return this->descriptor_ != NULL; }
1587 // Return a backend representation.
1588 Bfunction*
1589 get_or_make_decl(Gogo*, Named_object*);
1591 // If there is a descriptor, build it into the backend
1592 // representation.
1593 void
1594 build_backend_descriptor(Gogo*);
1596 // Export a function declaration.
1597 void
1598 export_func(Export* exp, const std::string& name) const
1599 { Function::export_func_with_type(exp, name, this->fntype_); }
1601 // Check that the types used in this declaration's signature are defined.
1602 void
1603 check_types() const;
1605 private:
1606 // The type of the function.
1607 Function_type* fntype_;
1608 // The location of the declaration.
1609 Location location_;
1610 // The assembler name: this is the name to use in references to the
1611 // function. This is normally empty.
1612 std::string asm_name_;
1613 // The function descriptor, if any.
1614 Expression* descriptor_;
1615 // The function decl if needed.
1616 Bfunction* fndecl_;
1617 // Pragmas for this function. This is a set of GOPRAGMA bits.
1618 unsigned int pragmas_;
1621 // A variable.
1623 class Variable
1625 public:
1626 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1627 bool is_receiver, Location);
1629 // Get the type of the variable.
1630 Type*
1631 type();
1633 Type*
1634 type() const;
1636 // Return whether the type is defined yet.
1637 bool
1638 has_type() const;
1640 // Get the initial value.
1641 Expression*
1642 init() const
1643 { return this->init_; }
1645 // Return whether there are any preinit statements.
1646 bool
1647 has_pre_init() const
1648 { return this->preinit_ != NULL; }
1650 // Return the preinit statements if any.
1651 Block*
1652 preinit() const
1653 { return this->preinit_; }
1655 // Return whether this is a global variable.
1656 bool
1657 is_global() const
1658 { return this->is_global_; }
1660 // Return whether this is a function parameter.
1661 bool
1662 is_parameter() const
1663 { return this->is_parameter_; }
1665 // Return whether this is a closure (static chain) parameter.
1666 bool
1667 is_closure() const
1668 { return this->is_closure_; }
1670 // Change this parameter to be a closure.
1671 void
1672 set_is_closure()
1674 this->is_closure_ = true;
1677 // Return whether this is the receiver parameter of a method.
1678 bool
1679 is_receiver() const
1680 { return this->is_receiver_; }
1682 // Change this parameter to be a receiver. This is used when
1683 // creating the thunks created for functions which call recover.
1684 void
1685 set_is_receiver()
1687 go_assert(this->is_parameter_);
1688 this->is_receiver_ = true;
1691 // Change this parameter to not be a receiver. This is used when
1692 // creating the thunks created for functions which call recover.
1693 void
1694 set_is_not_receiver()
1696 go_assert(this->is_parameter_);
1697 this->is_receiver_ = false;
1700 // Return whether this is the varargs parameter of a function.
1701 bool
1702 is_varargs_parameter() const
1703 { return this->is_varargs_parameter_; }
1705 // Whether this variable's address is taken.
1706 bool
1707 is_address_taken() const
1708 { return this->is_address_taken_; }
1710 // Whether this variable should live in the heap.
1711 bool
1712 is_in_heap() const
1714 return this->is_address_taken_
1715 && this->escapes_
1716 && !this->is_global_;
1719 // Note that something takes the address of this variable.
1720 void
1721 set_address_taken()
1722 { this->is_address_taken_ = true; }
1724 // Return whether the address is taken but does not escape.
1725 bool
1726 is_non_escaping_address_taken() const
1727 { return this->is_non_escaping_address_taken_; }
1729 // Note that something takes the address of this variable such that
1730 // the address does not escape the function.
1731 void
1732 set_non_escaping_address_taken()
1733 { this->is_non_escaping_address_taken_ = true; }
1735 // Return whether this variable escapes the function it is declared in.
1736 bool
1737 escapes()
1738 { return this->escapes_; }
1740 // Note that this variable does not escape the function it is declared in.
1741 void
1742 set_does_not_escape()
1743 { this->escapes_ = false; }
1745 // Get the source location of the variable's declaration.
1746 Location
1747 location() const
1748 { return this->location_; }
1750 // Record that this is the varargs parameter of a function.
1751 void
1752 set_is_varargs_parameter()
1754 go_assert(this->is_parameter_);
1755 this->is_varargs_parameter_ = true;
1758 // Return whether the variable has been used.
1759 bool
1760 is_used() const
1761 { return this->is_used_; }
1763 // Mark that the variable has been used.
1764 void
1765 set_is_used()
1766 { this->is_used_ = true; }
1768 // Clear the initial value; used for error handling and write barriers.
1769 void
1770 clear_init()
1771 { this->init_ = NULL; }
1773 // Set the initial value; used for converting shortcuts.
1774 void
1775 set_init(Expression* init)
1776 { this->init_ = init; }
1778 // Get the preinit block, a block of statements to be run before the
1779 // initialization expression.
1780 Block*
1781 preinit_block(Gogo*);
1783 // Add a statement to be run before the initialization expression.
1784 // This is only used for global variables.
1785 void
1786 add_preinit_statement(Gogo*, Statement*);
1788 // Lower the initialization expression after parsing is complete.
1789 void
1790 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1792 // Flatten the initialization expression after ordering evaluations.
1793 void
1794 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1796 // A special case: the init value is used only to determine the
1797 // type. This is used if the variable is defined using := with the
1798 // comma-ok form of a map index or a receive expression. The init
1799 // value is actually the map index expression or receive expression.
1800 // We use this because we may not know the right type at parse time.
1801 void
1802 set_type_from_init_tuple()
1803 { this->type_from_init_tuple_ = true; }
1805 // Another special case: the init value is used only to determine
1806 // the type. This is used if the variable is defined using := with
1807 // a range clause. The init value is the range expression. The
1808 // type of the variable is the index type of the range expression
1809 // (i.e., the first value returned by a range).
1810 void
1811 set_type_from_range_index()
1812 { this->type_from_range_index_ = true; }
1814 // Another special case: like set_type_from_range_index, but the
1815 // type is the value type of the range expression (i.e., the second
1816 // value returned by a range).
1817 void
1818 set_type_from_range_value()
1819 { this->type_from_range_value_ = 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 case in a select statement. The init value is the channel.
1824 // The type of the variable is the channel's element type.
1825 void
1826 set_type_from_chan_element()
1827 { this->type_from_chan_element_ = true; }
1829 // After we lower the select statement, we once again set the type
1830 // from the initialization expression.
1831 void
1832 clear_type_from_chan_element()
1834 go_assert(this->type_from_chan_element_);
1835 this->type_from_chan_element_ = false;
1838 // TRUE if this variable was created for a type switch clause.
1839 bool
1840 is_type_switch_var() const
1841 { return this->is_type_switch_var_; }
1843 // Note that this variable was created for a type switch clause.
1844 void
1845 set_is_type_switch_var()
1846 { this->is_type_switch_var_ = true; }
1848 // Mark the variable as going into a unique section.
1849 void
1850 set_in_unique_section()
1852 go_assert(this->is_global_);
1853 this->in_unique_section_ = true;
1856 // Traverse the initializer expression.
1858 traverse_expression(Traverse*, unsigned int traverse_mask);
1860 // Determine the type of the variable if necessary.
1861 void
1862 determine_type();
1864 // Get the backend representation of the variable.
1865 Bvariable*
1866 get_backend_variable(Gogo*, Named_object*, const Package*,
1867 const std::string&);
1869 // Get the initial value of the variable. This may only
1870 // be called if has_pre_init() returns false.
1871 Bexpression*
1872 get_init(Gogo*, Named_object* function);
1874 // Return a series of statements which sets the value of the
1875 // variable in DECL. This should only be called is has_pre_init()
1876 // returns true. DECL may be NULL for a sink variable.
1877 Bstatement*
1878 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1880 // Export the variable.
1881 void
1882 export_var(Export*, const std::string& name) const;
1884 // Import a variable.
1885 static void
1886 import_var(Import*, std::string* pname, Type** ptype);
1888 private:
1889 // The type of a tuple.
1890 Type*
1891 type_from_tuple(Expression*, bool) const;
1893 // The type of a range.
1894 Type*
1895 type_from_range(Expression*, bool, bool) const;
1897 // The element type of a channel.
1898 Type*
1899 type_from_chan_element(Expression*, bool) const;
1901 // The variable's type. This may be NULL if the type is set from
1902 // the expression.
1903 Type* type_;
1904 // The initial value. This may be NULL if the variable should be
1905 // initialized to the default value for the type.
1906 Expression* init_;
1907 // Statements to run before the init statement.
1908 Block* preinit_;
1909 // Location of variable definition.
1910 Location location_;
1911 // Backend representation.
1912 Bvariable* backend_;
1913 // Whether this is a global variable.
1914 bool is_global_ : 1;
1915 // Whether this is a function parameter.
1916 bool is_parameter_ : 1;
1917 // Whether this is a closure parameter.
1918 bool is_closure_ : 1;
1919 // Whether this is the receiver parameter of a method.
1920 bool is_receiver_ : 1;
1921 // Whether this is the varargs parameter of a function.
1922 bool is_varargs_parameter_ : 1;
1923 // Whether this variable is ever referenced.
1924 bool is_used_ : 1;
1925 // Whether something takes the address of this variable. For a
1926 // local variable this implies that the variable has to be on the
1927 // heap if it escapes from its function.
1928 bool is_address_taken_ : 1;
1929 // Whether something takes the address of this variable such that
1930 // the address does not escape the function.
1931 bool is_non_escaping_address_taken_ : 1;
1932 // True if we have seen this variable in a traversal.
1933 bool seen_ : 1;
1934 // True if we have lowered the initialization expression.
1935 bool init_is_lowered_ : 1;
1936 // True if we have flattened the initialization expression.
1937 bool init_is_flattened_ : 1;
1938 // True if init is a tuple used to set the type.
1939 bool type_from_init_tuple_ : 1;
1940 // True if init is a range clause and the type is the index type.
1941 bool type_from_range_index_ : 1;
1942 // True if init is a range clause and the type is the value type.
1943 bool type_from_range_value_ : 1;
1944 // True if init is a channel and the type is the channel's element type.
1945 bool type_from_chan_element_ : 1;
1946 // True if this is a variable created for a type switch case.
1947 bool is_type_switch_var_ : 1;
1948 // True if we have determined types.
1949 bool determined_type_ : 1;
1950 // True if this variable should be put in a unique section. This is
1951 // used for field tracking.
1952 bool in_unique_section_ : 1;
1953 // Whether this variable escapes the function it is created in. This is
1954 // true until shown otherwise.
1955 bool escapes_ : 1;
1958 // A variable which is really the name for a function return value, or
1959 // part of one.
1961 class Result_variable
1963 public:
1964 Result_variable(Type* type, Function* function, int index,
1965 Location location)
1966 : type_(type), function_(function), index_(index), location_(location),
1967 backend_(NULL), is_address_taken_(false),
1968 is_non_escaping_address_taken_(false), escapes_(true)
1971 // Get the type of the result variable.
1972 Type*
1973 type() const
1974 { return this->type_; }
1976 // Get the function that this is associated with.
1977 Function*
1978 function() const
1979 { return this->function_; }
1981 // Index in the list of function results.
1983 index() const
1984 { return this->index_; }
1986 // The location of the variable definition.
1987 Location
1988 location() const
1989 { return this->location_; }
1991 // Whether this variable's address is taken.
1992 bool
1993 is_address_taken() const
1994 { return this->is_address_taken_; }
1996 // Note that something takes the address of this variable.
1997 void
1998 set_address_taken()
1999 { this->is_address_taken_ = true; }
2001 // Return whether the address is taken but does not escape.
2002 bool
2003 is_non_escaping_address_taken() const
2004 { return this->is_non_escaping_address_taken_; }
2006 // Note that something takes the address of this variable such that
2007 // the address does not escape the function.
2008 void
2009 set_non_escaping_address_taken()
2010 { this->is_non_escaping_address_taken_ = true; }
2012 // Return whether this variable escapes the function it is declared in.
2013 bool
2014 escapes()
2015 { return this->escapes_; }
2017 // Note that this variable does not escape the function it is declared in.
2018 void
2019 set_does_not_escape()
2020 { this->escapes_ = false; }
2022 // Whether this variable should live in the heap.
2023 bool
2024 is_in_heap() const
2026 return this->is_address_taken_
2027 && this->escapes_;
2030 // Set the function. This is used when cloning functions which call
2031 // recover.
2032 void
2033 set_function(Function* function)
2034 { this->function_ = function; }
2036 // Get the backend representation of the variable.
2037 Bvariable*
2038 get_backend_variable(Gogo*, Named_object*, const std::string&);
2040 private:
2041 // Type of result variable.
2042 Type* type_;
2043 // Function with which this is associated.
2044 Function* function_;
2045 // Index in list of results.
2046 int index_;
2047 // Where the result variable is defined.
2048 Location location_;
2049 // Backend representation.
2050 Bvariable* backend_;
2051 // Whether something takes the address of this variable.
2052 bool is_address_taken_;
2053 // Whether something takes the address of this variable such that
2054 // the address does not escape the function.
2055 bool is_non_escaping_address_taken_;
2056 // Whether this variable escapes the function it is created in. This is
2057 // true until shown otherwise.
2058 bool escapes_;
2061 // The value we keep for a named constant. This lets us hold a type
2062 // and an expression.
2064 class Named_constant
2066 public:
2067 Named_constant(Type* type, Expression* expr, int iota_value,
2068 Location location)
2069 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2070 lowering_(false), is_sink_(false), bconst_(NULL)
2073 Type*
2074 type() const
2075 { return this->type_; }
2077 Expression*
2078 expr() const
2079 { return this->expr_; }
2082 iota_value() const
2083 { return this->iota_value_; }
2085 Location
2086 location() const
2087 { return this->location_; }
2089 // Whether we are lowering.
2090 bool
2091 lowering() const
2092 { return this->lowering_; }
2094 // Set that we are lowering.
2095 void
2096 set_lowering()
2097 { this->lowering_ = true; }
2099 // We are no longer lowering.
2100 void
2101 clear_lowering()
2102 { this->lowering_ = false; }
2104 bool
2105 is_sink() const
2106 { return this->is_sink_; }
2108 void
2109 set_is_sink()
2110 { this->is_sink_ = true; }
2112 // Traverse the expression.
2114 traverse_expression(Traverse*);
2116 // Determine the type of the constant if necessary.
2117 void
2118 determine_type();
2120 // Indicate that we found and reported an error for this constant.
2121 void
2122 set_error();
2124 // Export the constant.
2125 void
2126 export_const(Export*, const std::string& name) const;
2128 // Import a constant.
2129 static void
2130 import_const(Import*, std::string*, Type**, Expression**);
2132 // Get the backend representation of the constant value.
2133 Bexpression*
2134 get_backend(Gogo*, Named_object*);
2136 private:
2137 // The type of the constant.
2138 Type* type_;
2139 // The expression for the constant.
2140 Expression* expr_;
2141 // If the predeclared constant iota is used in EXPR_, this is the
2142 // value it will have. We do this because at parse time we don't
2143 // know whether the name "iota" will refer to the predeclared
2144 // constant or to something else. We put in the right value in when
2145 // we lower.
2146 int iota_value_;
2147 // The location of the definition.
2148 Location location_;
2149 // Whether we are currently lowering this constant.
2150 bool lowering_;
2151 // Whether this constant is blank named and needs only type checking.
2152 bool is_sink_;
2153 // The backend representation of the constant value.
2154 Bexpression* bconst_;
2157 // A type declaration.
2159 class Type_declaration
2161 public:
2162 Type_declaration(Location location)
2163 : location_(location), in_function_(NULL), in_function_index_(0),
2164 methods_(), issued_warning_(false)
2167 // Return the location.
2168 Location
2169 location() const
2170 { return this->location_; }
2172 // Return the function in which this type is declared. This will
2173 // return NULL for a type declared in global scope.
2174 Named_object*
2175 in_function(unsigned int* pindex)
2177 *pindex = this->in_function_index_;
2178 return this->in_function_;
2181 // Set the function in which this type is declared.
2182 void
2183 set_in_function(Named_object* f, unsigned int index)
2185 this->in_function_ = f;
2186 this->in_function_index_ = index;
2189 // Add a method to this type. This is used when methods are defined
2190 // before the type.
2191 Named_object*
2192 add_method(const std::string& name, Function* function);
2194 // Add a method declaration to this type.
2195 Named_object*
2196 add_method_declaration(const std::string& name, Package*,
2197 Function_type* type, Location location);
2199 // Add an already created object as a method.
2200 void
2201 add_existing_method(Named_object* no)
2202 { this->methods_.push_back(no); }
2204 // Return whether any methods were defined.
2205 bool
2206 has_methods() const;
2208 // Return the methods.
2209 const std::vector<Named_object*>*
2210 methods() const
2211 { return &this->methods_; }
2213 // Define methods when the real type is known.
2214 void
2215 define_methods(Named_type*);
2217 // This is called if we are trying to use this type. It returns
2218 // true if we should issue a warning.
2219 bool
2220 using_type();
2222 private:
2223 // The location of the type declaration.
2224 Location location_;
2225 // If this type is declared in a function, a pointer back to the
2226 // function in which it is defined.
2227 Named_object* in_function_;
2228 // The index of this type in IN_FUNCTION_.
2229 unsigned int in_function_index_;
2230 // Methods defined before the type is defined.
2231 std::vector<Named_object*> methods_;
2232 // True if we have issued a warning about a use of this type
2233 // declaration when it is undefined.
2234 bool issued_warning_;
2237 // An unknown object. These are created by the parser for forward
2238 // references to names which have not been seen before. In a correct
2239 // program, these will always point to a real definition by the end of
2240 // the parse. Because they point to another Named_object, these may
2241 // only be referenced by Unknown_expression objects.
2243 class Unknown_name
2245 public:
2246 Unknown_name(Location location)
2247 : location_(location), real_named_object_(NULL)
2250 // Return the location where this name was first seen.
2251 Location
2252 location() const
2253 { return this->location_; }
2255 // Return the real named object that this points to, or NULL if it
2256 // was never resolved.
2257 Named_object*
2258 real_named_object() const
2259 { return this->real_named_object_; }
2261 // Set the real named object that this points to.
2262 void
2263 set_real_named_object(Named_object* no);
2265 private:
2266 // The location where this name was first seen.
2267 Location location_;
2268 // The real named object when it is known.
2269 Named_object*
2270 real_named_object_;
2273 // A named object named. This is the result of a declaration. We
2274 // don't use a superclass because they all have to be handled
2275 // differently.
2277 class Named_object
2279 public:
2280 enum Classification
2282 // An uninitialized Named_object. We should never see this.
2283 NAMED_OBJECT_UNINITIALIZED,
2284 // An erroneous name. This indicates a parse error, to avoid
2285 // later errors about undefined references.
2286 NAMED_OBJECT_ERRONEOUS,
2287 // An unknown name. This is used for forward references. In a
2288 // correct program, these will all be resolved by the end of the
2289 // parse.
2290 NAMED_OBJECT_UNKNOWN,
2291 // A const.
2292 NAMED_OBJECT_CONST,
2293 // A type.
2294 NAMED_OBJECT_TYPE,
2295 // A forward type declaration.
2296 NAMED_OBJECT_TYPE_DECLARATION,
2297 // A var.
2298 NAMED_OBJECT_VAR,
2299 // A result variable in a function.
2300 NAMED_OBJECT_RESULT_VAR,
2301 // The blank identifier--the special variable named _.
2302 NAMED_OBJECT_SINK,
2303 // A func.
2304 NAMED_OBJECT_FUNC,
2305 // A forward func declaration.
2306 NAMED_OBJECT_FUNC_DECLARATION,
2307 // A package.
2308 NAMED_OBJECT_PACKAGE
2311 // Return the classification.
2312 Classification
2313 classification() const
2314 { return this->classification_; }
2316 // Classifiers.
2318 bool
2319 is_erroneous() const
2320 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2322 bool
2323 is_unknown() const
2324 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2326 bool
2327 is_const() const
2328 { return this->classification_ == NAMED_OBJECT_CONST; }
2330 bool
2331 is_type() const
2332 { return this->classification_ == NAMED_OBJECT_TYPE; }
2334 bool
2335 is_type_declaration() const
2336 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2338 bool
2339 is_variable() const
2340 { return this->classification_ == NAMED_OBJECT_VAR; }
2342 bool
2343 is_result_variable() const
2344 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2346 bool
2347 is_sink() const
2348 { return this->classification_ == NAMED_OBJECT_SINK; }
2350 bool
2351 is_function() const
2352 { return this->classification_ == NAMED_OBJECT_FUNC; }
2354 bool
2355 is_function_declaration() const
2356 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2358 bool
2359 is_package() const
2360 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2362 // Creators.
2364 static Named_object*
2365 make_erroneous_name(const std::string& name)
2366 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2368 static Named_object*
2369 make_unknown_name(const std::string& name, Location);
2371 static Named_object*
2372 make_constant(const Typed_identifier&, const Package*, Expression*,
2373 int iota_value);
2375 static Named_object*
2376 make_type(const std::string&, const Package*, Type*, Location);
2378 static Named_object*
2379 make_type_declaration(const std::string&, const Package*, Location);
2381 static Named_object*
2382 make_variable(const std::string&, const Package*, Variable*);
2384 static Named_object*
2385 make_result_variable(const std::string&, Result_variable*);
2387 static Named_object*
2388 make_sink();
2390 static Named_object*
2391 make_function(const std::string&, const Package*, Function*);
2393 static Named_object*
2394 make_function_declaration(const std::string&, const Package*, Function_type*,
2395 Location);
2397 static Named_object*
2398 make_package(const std::string& alias, Package* package);
2400 // Getters.
2402 Unknown_name*
2403 unknown_value()
2405 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2406 return this->u_.unknown_value;
2409 const Unknown_name*
2410 unknown_value() const
2412 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2413 return this->u_.unknown_value;
2416 Named_constant*
2417 const_value()
2419 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2420 return this->u_.const_value;
2423 const Named_constant*
2424 const_value() const
2426 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2427 return this->u_.const_value;
2430 Named_type*
2431 type_value()
2433 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2434 return this->u_.type_value;
2437 const Named_type*
2438 type_value() const
2440 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2441 return this->u_.type_value;
2444 Type_declaration*
2445 type_declaration_value()
2447 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2448 return this->u_.type_declaration;
2451 const Type_declaration*
2452 type_declaration_value() const
2454 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2455 return this->u_.type_declaration;
2458 Variable*
2459 var_value()
2461 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2462 return this->u_.var_value;
2465 const Variable*
2466 var_value() const
2468 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2469 return this->u_.var_value;
2472 Result_variable*
2473 result_var_value()
2475 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2476 return this->u_.result_var_value;
2479 const Result_variable*
2480 result_var_value() const
2482 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2483 return this->u_.result_var_value;
2486 Function*
2487 func_value()
2489 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2490 return this->u_.func_value;
2493 const Function*
2494 func_value() const
2496 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2497 return this->u_.func_value;
2500 Function_declaration*
2501 func_declaration_value()
2503 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2504 return this->u_.func_declaration_value;
2507 const Function_declaration*
2508 func_declaration_value() const
2510 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2511 return this->u_.func_declaration_value;
2514 Package*
2515 package_value()
2517 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2518 return this->u_.package_value;
2521 const Package*
2522 package_value() const
2524 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2525 return this->u_.package_value;
2528 const std::string&
2529 name() const
2530 { return this->name_; }
2532 // Return the name to use in an error message. The difference is
2533 // that if this Named_object is defined in a different package, this
2534 // will return PACKAGE.NAME.
2535 std::string
2536 message_name() const;
2538 const Package*
2539 package() const
2540 { return this->package_; }
2542 // Resolve an unknown value if possible. This returns the same
2543 // Named_object or a new one.
2544 Named_object*
2545 resolve()
2547 Named_object* ret = this;
2548 if (this->is_unknown())
2550 Named_object* r = this->unknown_value()->real_named_object();
2551 if (r != NULL)
2552 ret = r;
2554 return ret;
2557 const Named_object*
2558 resolve() const
2560 const Named_object* ret = this;
2561 if (this->is_unknown())
2563 const Named_object* r = this->unknown_value()->real_named_object();
2564 if (r != NULL)
2565 ret = r;
2567 return ret;
2570 // The location where this object was defined or referenced.
2571 Location
2572 location() const;
2574 // Convert a variable to the backend representation.
2575 Bvariable*
2576 get_backend_variable(Gogo*, Named_object* function);
2578 // Return the external identifier for this object.
2579 std::string
2580 get_id(Gogo*);
2582 // Get the backend representation of this object.
2583 void
2584 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2585 std::vector<Bfunction*>&);
2587 // Define a type declaration.
2588 void
2589 set_type_value(Named_type*);
2591 // Define a function declaration.
2592 void
2593 set_function_value(Function*);
2595 // Declare an unknown name as a type declaration.
2596 void
2597 declare_as_type();
2599 // Export this object.
2600 void
2601 export_named_object(Export*) const;
2603 // Mark this named object as an invalid redefinition of another object.
2604 void
2605 set_is_redefinition()
2606 { this->is_redefinition_ = true; }
2608 // Return whether or not this object is a invalid redefinition of another
2609 // object.
2610 bool
2611 is_redefinition() const
2612 { return this->is_redefinition_; }
2614 private:
2615 Named_object(const std::string&, const Package*, Classification);
2617 // The name of the object.
2618 std::string name_;
2619 // The package that this object is in. This is NULL if it is in the
2620 // file we are compiling.
2621 const Package* package_;
2622 // The type of object this is.
2623 Classification classification_;
2624 // The real data.
2625 union
2627 Unknown_name* unknown_value;
2628 Named_constant* const_value;
2629 Named_type* type_value;
2630 Type_declaration* type_declaration;
2631 Variable* var_value;
2632 Result_variable* result_var_value;
2633 Function* func_value;
2634 Function_declaration* func_declaration_value;
2635 Package* package_value;
2636 } u_;
2637 // True if this object is an invalid redefinition of another object.
2638 bool is_redefinition_;
2641 // A binding contour. This binds names to objects.
2643 class Bindings
2645 public:
2646 // Type for mapping from names to objects.
2647 typedef Unordered_map(std::string, Named_object*) Contour;
2649 Bindings(Bindings* enclosing);
2651 // Add an erroneous name.
2652 Named_object*
2653 add_erroneous_name(const std::string& name)
2654 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2656 // Add an unknown name.
2657 Named_object*
2658 add_unknown_name(const std::string& name, Location location)
2660 return this->add_named_object(Named_object::make_unknown_name(name,
2661 location));
2664 // Add a constant.
2665 Named_object*
2666 add_constant(const Typed_identifier& tid, const Package* package,
2667 Expression* expr, int iota_value)
2669 return this->add_named_object(Named_object::make_constant(tid, package,
2670 expr,
2671 iota_value));
2674 // Add a type.
2675 Named_object*
2676 add_type(const std::string& name, const Package* package, Type* type,
2677 Location location)
2679 return this->add_named_object(Named_object::make_type(name, package, type,
2680 location));
2683 // Add a named type. This is used for builtin types, and to add an
2684 // imported type to the global scope.
2685 Named_object*
2686 add_named_type(Named_type* named_type);
2688 // Add a type declaration.
2689 Named_object*
2690 add_type_declaration(const std::string& name, const Package* package,
2691 Location location)
2693 Named_object* no = Named_object::make_type_declaration(name, package,
2694 location);
2695 return this->add_named_object(no);
2698 // Add a variable.
2699 Named_object*
2700 add_variable(const std::string& name, const Package* package,
2701 Variable* variable)
2703 return this->add_named_object(Named_object::make_variable(name, package,
2704 variable));
2707 // Add a result variable.
2708 Named_object*
2709 add_result_variable(const std::string& name, Result_variable* result)
2711 return this->add_named_object(Named_object::make_result_variable(name,
2712 result));
2715 // Add a function.
2716 Named_object*
2717 add_function(const std::string& name, const Package*, Function* function);
2719 // Add a function declaration.
2720 Named_object*
2721 add_function_declaration(const std::string& name, const Package* package,
2722 Function_type* type, Location location);
2724 // Add a package. The location is the location of the import
2725 // statement.
2726 Named_object*
2727 add_package(const std::string& alias, Package* package)
2729 Named_object* no = Named_object::make_package(alias, package);
2730 return this->add_named_object(no);
2733 // Define a type which was already declared.
2734 void
2735 define_type(Named_object*, Named_type*);
2737 // Add a method to the list of objects. This is not added to the
2738 // lookup table.
2739 void
2740 add_method(Named_object*);
2742 // Add a named object to this binding.
2743 Named_object*
2744 add_named_object(Named_object* no)
2745 { return this->add_named_object_to_contour(&this->bindings_, no); }
2747 // Clear all names in file scope from the bindings.
2748 void
2749 clear_file_scope(Gogo*);
2751 // Look up a name in this binding contour and in any enclosing
2752 // binding contours. This returns NULL if the name is not found.
2753 Named_object*
2754 lookup(const std::string&) const;
2756 // Look up a name in this binding contour without looking in any
2757 // enclosing binding contours. Returns NULL if the name is not found.
2758 Named_object*
2759 lookup_local(const std::string&) const;
2761 // Remove a name.
2762 void
2763 remove_binding(Named_object*);
2765 // Mark all variables as used. This is used for some types of parse
2766 // error.
2767 void
2768 mark_locals_used();
2770 // Traverse the tree. See the Traverse class.
2772 traverse(Traverse*, bool is_global);
2774 // Iterate over definitions. This does not include things which
2775 // were only declared.
2777 typedef std::vector<Named_object*>::const_iterator
2778 const_definitions_iterator;
2780 const_definitions_iterator
2781 begin_definitions() const
2782 { return this->named_objects_.begin(); }
2784 const_definitions_iterator
2785 end_definitions() const
2786 { return this->named_objects_.end(); }
2788 // Return the number of definitions.
2789 size_t
2790 size_definitions() const
2791 { return this->named_objects_.size(); }
2793 // Return whether there are no definitions.
2794 bool
2795 empty_definitions() const
2796 { return this->named_objects_.empty(); }
2798 // Iterate over declarations. This is everything that has been
2799 // declared, which includes everything which has been defined.
2801 typedef Contour::const_iterator const_declarations_iterator;
2803 const_declarations_iterator
2804 begin_declarations() const
2805 { return this->bindings_.begin(); }
2807 const_declarations_iterator
2808 end_declarations() const
2809 { return this->bindings_.end(); }
2811 // Return the number of declarations.
2812 size_t
2813 size_declarations() const
2814 { return this->bindings_.size(); }
2816 // Return whether there are no declarations.
2817 bool
2818 empty_declarations() const
2819 { return this->bindings_.empty(); }
2821 // Return the first declaration.
2822 Named_object*
2823 first_declaration()
2824 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2826 private:
2827 Named_object*
2828 add_named_object_to_contour(Contour*, Named_object*);
2830 Named_object*
2831 new_definition(Named_object*, Named_object*);
2833 // Enclosing bindings.
2834 Bindings* enclosing_;
2835 // The list of objects.
2836 std::vector<Named_object*> named_objects_;
2837 // The mapping from names to objects.
2838 Contour bindings_;
2841 // A label.
2843 class Label
2845 public:
2846 Label(const std::string& name)
2847 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2848 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
2851 // Return the label's name.
2852 const std::string&
2853 name() const
2854 { return this->name_; }
2856 // Return whether the label has been defined.
2857 bool
2858 is_defined() const
2859 { return !Linemap::is_unknown_location(this->location_); }
2861 // Return whether the label has been used.
2862 bool
2863 is_used() const
2864 { return this->is_used_; }
2866 // Record that the label is used.
2867 void
2868 set_is_used()
2869 { this->is_used_ = true; }
2871 // Return whether this label is looping.
2872 bool
2873 looping() const
2874 { return this->depth_ == DEPTH_LOOPING; }
2876 // Set this label as looping.
2877 void
2878 set_looping()
2879 { this->depth_ = DEPTH_LOOPING; }
2881 // Return whether this label is nonlooping.
2882 bool
2883 nonlooping() const
2884 { return this->depth_ == DEPTH_NONLOOPING; }
2886 // Set this label as nonlooping.
2887 void
2888 set_nonlooping()
2889 { this->depth_ = DEPTH_NONLOOPING; }
2891 // Return the location of the definition.
2892 Location
2893 location() const
2894 { return this->location_; }
2896 // Return the bindings snapshot.
2897 Bindings_snapshot*
2898 snapshot() const
2899 { return this->snapshot_; }
2901 // Add a snapshot of a goto which refers to this label.
2902 void
2903 add_snapshot_ref(Bindings_snapshot* snapshot)
2905 go_assert(Linemap::is_unknown_location(this->location_));
2906 this->refs_.push_back(snapshot);
2909 // Return the list of snapshots of goto statements which refer to
2910 // this label.
2911 const std::vector<Bindings_snapshot*>&
2912 refs() const
2913 { return this->refs_; }
2915 // Clear the references.
2916 void
2917 clear_refs();
2919 // Define the label at LOCATION with the given bindings snapshot.
2920 void
2921 define(Location location, Bindings_snapshot* snapshot)
2923 if (this->is_dummy_label())
2924 return;
2925 go_assert(Linemap::is_unknown_location(this->location_)
2926 && this->snapshot_ == NULL);
2927 this->location_ = location;
2928 this->snapshot_ = snapshot;
2931 // Return the backend representation for this label.
2932 Blabel*
2933 get_backend_label(Translate_context*);
2935 // Return an expression for the address of this label. This is used
2936 // to get the return address of a deferred function to see whether
2937 // the function may call recover.
2938 Bexpression*
2939 get_addr(Translate_context*, Location location);
2941 // Return a dummy label, representing any instance of the blank label.
2942 static Label*
2943 create_dummy_label();
2945 // Return TRUE if this is a dummy label.
2946 bool
2947 is_dummy_label() const
2948 { return this->name_ == "_"; }
2950 // A classification of a label's looping depth.
2951 enum Loop_depth
2953 DEPTH_UNKNOWN,
2954 // A label never jumped to.
2955 DEPTH_NONLOOPING,
2956 // A label jumped to.
2957 DEPTH_LOOPING
2960 private:
2961 // The name of the label.
2962 std::string name_;
2963 // The location of the definition. This is 0 if the label has not
2964 // yet been defined.
2965 Location location_;
2966 // A snapshot of the set of bindings defined at this label, used to
2967 // issue errors about invalid goto statements.
2968 Bindings_snapshot* snapshot_;
2969 // A list of snapshots of goto statements which refer to this label.
2970 std::vector<Bindings_snapshot*> refs_;
2971 // Whether the label has been used.
2972 bool is_used_;
2973 // The backend representation.
2974 Blabel* blabel_;
2975 // The looping depth of this label, for escape analysis.
2976 Loop_depth depth_;
2979 // An unnamed label. These are used when lowering loops.
2981 class Unnamed_label
2983 public:
2984 Unnamed_label(Location location)
2985 : location_(location), derived_from_(NULL), blabel_(NULL)
2988 // Get the location where the label is defined.
2989 Location
2990 location() const
2991 { return this->location_; }
2993 // Set the location where the label is defined.
2994 void
2995 set_location(Location location)
2996 { this->location_ = location; }
2998 // Get the top level statement this unnamed label is derived from.
2999 Statement*
3000 derived_from() const
3001 { return this->derived_from_; }
3003 // Set the top level statement this unnamed label is derived from.
3004 void
3005 set_derived_from(Statement* s)
3006 { this->derived_from_ = s; }
3008 // Return a statement which defines this label.
3009 Bstatement*
3010 get_definition(Translate_context*);
3012 // Return a goto to this label from LOCATION.
3013 Bstatement*
3014 get_goto(Translate_context*, Location location);
3016 private:
3017 // Return the backend representation.
3018 Blabel*
3019 get_blabel(Translate_context*);
3021 // The location where the label is defined.
3022 Location location_;
3023 // The top-level statement this unnamed label was derived/lowered from.
3024 // This is NULL is this label is not the top-level of a lowered statement.
3025 Statement* derived_from_;
3026 // The backend representation of this label.
3027 Blabel* blabel_;
3030 // An alias for an imported package.
3032 class Package_alias
3034 public:
3035 Package_alias(Location location)
3036 : location_(location), used_(0)
3039 // The location of the import statement.
3040 Location
3041 location()
3042 { return this->location_; }
3044 // How many symbols from the package were used under this alias.
3045 size_t
3046 used() const
3047 { return this->used_; }
3049 // Note that some symbol was used under this alias.
3050 void
3051 note_usage()
3052 { this->used_++; }
3054 private:
3055 // The location of the import statement.
3056 Location location_;
3057 // The amount of times some name from this package was used under this alias.
3058 size_t used_;
3061 // An imported package.
3063 class Package
3065 public:
3066 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3067 Location location);
3069 // Get the package path used for all symbols exported from this
3070 // package.
3071 const std::string&
3072 pkgpath() const
3073 { return this->pkgpath_; }
3075 // Return the package path to use for a symbol name.
3076 std::string
3077 pkgpath_symbol() const;
3079 // Set the package path symbol.
3080 void
3081 set_pkgpath_symbol(const std::string&);
3083 // Return the location of the most recent import statement.
3084 Location
3085 location() const
3086 { return this->location_; }
3088 // Return whether we know the name of this package yet.
3089 bool
3090 has_package_name() const
3091 { return !this->package_name_.empty(); }
3093 // The name that this package uses in its package clause. This may
3094 // be different from the name in the associated Named_object if the
3095 // import statement used an alias.
3096 const std::string&
3097 package_name() const
3099 go_assert(!this->package_name_.empty());
3100 return this->package_name_;
3103 // Return the bindings.
3104 Bindings*
3105 bindings()
3106 { return this->bindings_; }
3108 // Type used to map import names to package aliases.
3109 typedef std::map<std::string, Package_alias*> Aliases;
3111 // Return the set of package aliases.
3112 const Aliases&
3113 aliases() const
3114 { return this->aliases_; }
3116 // Note that some symbol from this package was used and qualified by ALIAS.
3117 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3118 void
3119 note_usage(const std::string& alias) const;
3121 // Note that USAGE might be a fake usage of this package.
3122 void
3123 note_fake_usage(Expression* usage) const
3124 { this->fake_uses_.insert(usage); }
3126 // Forget a given USAGE of this package.
3127 void
3128 forget_usage(Expression* usage) const;
3130 // Clear the used field for the next file.
3131 void
3132 clear_used();
3134 // Look up a name in the package. Returns NULL if the name is not
3135 // found.
3136 Named_object*
3137 lookup(const std::string& name) const
3138 { return this->bindings_->lookup(name); }
3140 // Set the name of the package.
3141 void
3142 set_package_name(const std::string& name, Location);
3144 // Set the location of the package. This is used to record the most
3145 // recent import location.
3146 void
3147 set_location(Location location)
3148 { this->location_ = location; }
3150 // Add a package name as an ALIAS for this package.
3151 Package_alias*
3152 add_alias(const std::string& alias, Location);
3154 // Add a constant to the package.
3155 Named_object*
3156 add_constant(const Typed_identifier& tid, Expression* expr)
3157 { return this->bindings_->add_constant(tid, this, expr, 0); }
3159 // Add a type to the package.
3160 Named_object*
3161 add_type(const std::string& name, Type* type, Location location)
3162 { return this->bindings_->add_type(name, this, type, location); }
3164 // Add a type declaration to the package.
3165 Named_object*
3166 add_type_declaration(const std::string& name, Location location)
3167 { return this->bindings_->add_type_declaration(name, this, location); }
3169 // Add a variable to the package.
3170 Named_object*
3171 add_variable(const std::string& name, Variable* variable)
3172 { return this->bindings_->add_variable(name, this, variable); }
3174 // Add a function declaration to the package.
3175 Named_object*
3176 add_function_declaration(const std::string& name, Function_type* type,
3177 Location loc)
3178 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3180 // Determine types of constants.
3181 void
3182 determine_types();
3184 private:
3185 // The package path for type reflection data.
3186 std::string pkgpath_;
3187 // The package path for symbol names.
3188 std::string pkgpath_symbol_;
3189 // The name that this package uses in the package clause. This may
3190 // be the empty string if it is not yet known.
3191 std::string package_name_;
3192 // The names in this package.
3193 Bindings* bindings_;
3194 // The location of the most recent import statement.
3195 Location location_;
3196 // The set of aliases associated with this package.
3197 Aliases aliases_;
3198 // A set of possibly fake uses of this package. This is mutable because we
3199 // can track fake uses of a package even if we have a const pointer to it.
3200 mutable std::set<Expression*> fake_uses_;
3203 // Return codes for the traversal functions. This is not an enum
3204 // because we want to be able to declare traversal functions in other
3205 // header files without including this one.
3207 // Continue traversal as usual.
3208 const int TRAVERSE_CONTINUE = -1;
3210 // Exit traversal.
3211 const int TRAVERSE_EXIT = 0;
3213 // Continue traversal, but skip components of the current object.
3214 // E.g., if this is returned by Traverse::statement, we do not
3215 // traverse the expressions in the statement even if
3216 // traverse_expressions is set in the traverse_mask.
3217 const int TRAVERSE_SKIP_COMPONENTS = 1;
3219 // This class is used when traversing the parse tree. The caller uses
3220 // a subclass which overrides functions as desired.
3222 class Traverse
3224 public:
3225 // These bitmasks say what to traverse.
3226 static const unsigned int traverse_variables = 0x1;
3227 static const unsigned int traverse_constants = 0x2;
3228 static const unsigned int traverse_functions = 0x4;
3229 static const unsigned int traverse_blocks = 0x8;
3230 static const unsigned int traverse_statements = 0x10;
3231 static const unsigned int traverse_expressions = 0x20;
3232 static const unsigned int traverse_types = 0x40;
3234 Traverse(unsigned int traverse_mask)
3235 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3238 virtual ~Traverse();
3240 // The bitmask of what to traverse.
3241 unsigned int
3242 traverse_mask() const
3243 { return this->traverse_mask_; }
3245 // Record that we are going to traverse a type. This returns true
3246 // if the type has already been seen in this traversal. This is
3247 // required because types, unlike expressions, can form a circular
3248 // graph.
3249 bool
3250 remember_type(const Type*);
3252 // Record that we are going to see an expression. This returns true
3253 // if the expression has already been seen in this traversal. This
3254 // is only needed for cases where multiple expressions can point to
3255 // a single one.
3256 bool
3257 remember_expression(const Expression*);
3259 // These functions return one of the TRAVERSE codes defined above.
3261 // If traverse_variables is set in the mask, this is called for
3262 // every variable in the tree.
3263 virtual int
3264 variable(Named_object*);
3266 // If traverse_constants is set in the mask, this is called for
3267 // every named constant in the tree. The bool parameter is true for
3268 // a global constant.
3269 virtual int
3270 constant(Named_object*, bool);
3272 // If traverse_functions is set in the mask, this is called for
3273 // every function in the tree.
3274 virtual int
3275 function(Named_object*);
3277 // If traverse_blocks is set in the mask, this is called for every
3278 // block in the tree.
3279 virtual int
3280 block(Block*);
3282 // If traverse_statements is set in the mask, this is called for
3283 // every statement in the tree.
3284 virtual int
3285 statement(Block*, size_t* index, Statement*);
3287 // If traverse_expressions is set in the mask, this is called for
3288 // every expression in the tree.
3289 virtual int
3290 expression(Expression**);
3292 // If traverse_types is set in the mask, this is called for every
3293 // type in the tree.
3294 virtual int
3295 type(Type*);
3297 private:
3298 // A hash table for types we have seen during this traversal. Note
3299 // that this uses the default hash functions for pointers rather
3300 // than Type_hash_identical and Type_identical. This is because for
3301 // traversal we care about seeing a specific type structure. If
3302 // there are two separate instances of identical types, we want to
3303 // traverse both.
3304 typedef Unordered_set(const Type*) Types_seen;
3306 typedef Unordered_set(const Expression*) Expressions_seen;
3308 // Bitmask of what sort of objects to traverse.
3309 unsigned int traverse_mask_;
3310 // Types which have been seen in this traversal.
3311 Types_seen* types_seen_;
3312 // Expressions which have been seen in this traversal.
3313 Expressions_seen* expressions_seen_;
3316 // A class which makes it easier to insert new statements before the
3317 // current statement during a traversal.
3319 class Statement_inserter
3321 public:
3322 // Empty constructor.
3323 Statement_inserter()
3324 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
3327 // Constructor for a statement in a block.
3328 Statement_inserter(Block* block, size_t *pindex)
3329 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
3332 // Constructor for a global variable.
3333 Statement_inserter(Gogo* gogo, Variable* var)
3334 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
3335 { go_assert(var->is_global()); }
3337 // We use the default copy constructor and assignment operator.
3339 // Insert S before the statement we are traversing, or before the
3340 // initialization expression of a global variable.
3341 void
3342 insert(Statement* s);
3344 private:
3345 // The block that the statement is in.
3346 Block* block_;
3347 // The index of the statement that we are traversing.
3348 size_t* pindex_;
3349 // The IR, needed when looking at an initializer expression for a
3350 // global variable.
3351 Gogo* gogo_;
3352 // The global variable, when looking at an initializer expression.
3353 Variable* var_;
3356 // When translating the gogo IR into the backend data structure, this
3357 // is the context we pass down the blocks and statements.
3359 class Translate_context
3361 public:
3362 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3363 Bblock* bblock)
3364 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3365 block_(block), bblock_(bblock), is_const_(false)
3368 // Accessors.
3370 Gogo*
3371 gogo()
3372 { return this->gogo_; }
3374 Backend*
3375 backend()
3376 { return this->backend_; }
3378 Named_object*
3379 function()
3380 { return this->function_; }
3382 Block*
3383 block()
3384 { return this->block_; }
3386 Bblock*
3387 bblock()
3388 { return this->bblock_; }
3390 bool
3391 is_const()
3392 { return this->is_const_; }
3394 // Make a constant context.
3395 void
3396 set_is_const()
3397 { this->is_const_ = true; }
3399 private:
3400 // The IR for the entire compilation unit.
3401 Gogo* gogo_;
3402 // The generator for the backend data structures.
3403 Backend* backend_;
3404 // The function we are currently translating. NULL if not in a
3405 // function, e.g., the initializer of a global variable.
3406 Named_object* function_;
3407 // The block we are currently translating. NULL if not in a
3408 // function.
3409 Block *block_;
3410 // The backend representation of the current block. NULL if block_
3411 // is NULL.
3412 Bblock* bblock_;
3413 // Whether this is being evaluated in a constant context. This is
3414 // used for type descriptor initializers.
3415 bool is_const_;
3418 // Runtime error codes. These must match the values in
3419 // libgo/runtime/go-runtime-error.c.
3421 // Slice index out of bounds: negative or larger than the length of
3422 // the slice.
3423 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3425 // Array index out of bounds.
3426 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3428 // String index out of bounds.
3429 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3431 // Slice slice out of bounds: negative or larger than the length of
3432 // the slice or high bound less than low bound.
3433 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3435 // Array slice out of bounds.
3436 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3438 // String slice out of bounds.
3439 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3441 // Dereference of nil pointer. This is used when there is a
3442 // dereference of a pointer to a very large struct or array, to ensure
3443 // that a gigantic array is not used a proxy to access random memory
3444 // locations.
3445 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3447 // Slice length or capacity out of bounds in make: negative or
3448 // overflow or length greater than capacity.
3449 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3451 // Map capacity out of bounds in make: negative or overflow.
3452 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3454 // Channel capacity out of bounds in make: negative or overflow.
3455 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3457 // Division by zero.
3458 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3460 // Go statement with nil function.
3461 static const int RUNTIME_ERROR_GO_NIL = 11;
3463 // This is used by some of the langhooks.
3464 extern Gogo* go_get_gogo();
3466 // Whether we have seen any errors. FIXME: Replace with a backend
3467 // interface.
3468 extern bool saw_errors();
3470 #endif // !defined(GO_GOGO_H)