compiler, runtime: drop size arguments to hash/equal functions
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob7c29828231f3afbccda483d7acba55ef0968a65b
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)
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 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
366 // method function should be added to the type of its receiver.
367 Named_object*
368 start_function(const std::string& name, Function_type* type,
369 bool add_method_to_type, Location);
371 // Finish compiling a function.
372 void
373 finish_function(Location);
375 // Return the current function.
376 Named_object*
377 current_function() const;
379 // Return the current block.
380 Block*
381 current_block();
383 // Start a new block. This is not initially associated with a
384 // function.
385 void
386 start_block(Location);
388 // Finish the current block and return it.
389 Block*
390 finish_block(Location);
392 // Declare an erroneous name. This is used to avoid knock-on errors
393 // after a parsing error.
394 Named_object*
395 add_erroneous_name(const std::string& name);
397 // Declare an unknown name. This is used while parsing. The name
398 // must be resolved by the end of the parse. Unknown names are
399 // always added at the package level.
400 Named_object*
401 add_unknown_name(const std::string& name, Location);
403 // Declare a function.
404 Named_object*
405 declare_function(const std::string&, Function_type*, Location);
407 // Declare a function at the package level. This is used for
408 // functions generated for a type.
409 Named_object*
410 declare_package_function(const std::string&, Function_type*, Location);
412 // Add a label.
413 Label*
414 add_label_definition(const std::string&, Location);
416 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
417 // report errors for a goto from the current location to the label
418 // location.
419 Label*
420 add_label_reference(const std::string&, Location,
421 bool issue_goto_errors);
423 // An analysis set is a list of functions paired with a boolean that indicates
424 // whether the list of functions are recursive.
425 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
427 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
428 // package.
429 void
430 add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
431 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
433 // Return a snapshot of the current binding state.
434 Bindings_snapshot*
435 bindings_snapshot(Location);
437 // Add a statement to the current block.
438 void
439 add_statement(Statement*);
441 // Add a block to the current block.
442 void
443 add_block(Block*, Location);
445 // Add a constant.
446 Named_object*
447 add_constant(const Typed_identifier&, Expression*, int iota_value);
449 // Add a type.
450 void
451 add_type(const std::string&, Type*, Location);
453 // Add a named type. This is used for builtin types, and to add an
454 // imported type to the global scope.
455 void
456 add_named_type(Named_type*);
458 // Declare a type.
459 Named_object*
460 declare_type(const std::string&, Location);
462 // Declare a type at the package level. This is used when the
463 // parser sees an unknown name where a type name is required.
464 Named_object*
465 declare_package_type(const std::string&, Location);
467 // Define a type which was already declared.
468 void
469 define_type(Named_object*, Named_type*);
471 // Add a variable.
472 Named_object*
473 add_variable(const std::string&, Variable*);
475 // Add a sink--a reference to the blank identifier _.
476 Named_object*
477 add_sink();
479 // Add a type which needs to be verified. This is used for sink
480 // types, just to give appropriate error messages.
481 void
482 add_type_to_verify(Type* type);
484 // Add a named object to the current namespace. This is used for
485 // import . "package".
486 void
487 add_dot_import_object(Named_object*);
489 // Add an identifier to the list of names seen in the file block.
490 void
491 add_file_block_name(const std::string& name, Location location)
492 { this->file_block_names_[name] = location; }
494 // Add a linkname, from the go:linkname compiler directive. This
495 // changes the externally visible name of go_name to be ext_name.
496 void
497 add_linkname(const std::string& go_name, bool is_exported,
498 const std::string& ext_name, Location location);
500 // Mark all local variables in current bindings as used. This is
501 // used when there is a parse error to avoid useless errors.
502 void
503 mark_locals_used();
505 // Return a name to use for an error case. This should only be used
506 // after reporting an error, and is used to avoid useless knockon
507 // errors.
508 static std::string
509 erroneous_name();
511 // Return whether the name indicates an error.
512 static bool
513 is_erroneous_name(const std::string&);
515 // Return a name to use for a thunk function. A thunk function is
516 // one we create during the compilation, for a go statement or a
517 // defer statement or a method expression.
518 static std::string
519 thunk_name();
521 // Return whether an object is a thunk.
522 static bool
523 is_thunk(const Named_object*);
525 // Note that we've seen an interface type. This is used to build
526 // all required interface method tables.
527 void
528 record_interface_type(Interface_type*);
530 // Note that we need an initialization function.
531 void
532 set_need_init_fn()
533 { this->need_init_fn_ = true; }
535 // Return whether the current file imported the unsafe package.
536 bool
537 current_file_imported_unsafe() const
538 { return this->current_file_imported_unsafe_; }
540 // Clear out all names in file scope. This is called when we start
541 // parsing a new file.
542 void
543 clear_file_scope();
545 // Record that VAR1 must be initialized after VAR2. This is used
546 // when VAR2 does not appear in VAR1's INIT or PREINIT.
547 void
548 record_var_depends_on(Variable* var1, Named_object* var2)
550 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
551 this->var_deps_[var1] = var2;
554 // Return the variable that VAR depends on, or NULL if none.
555 Named_object*
556 var_depends_on(Variable* var) const
558 Var_deps::const_iterator p = this->var_deps_.find(var);
559 return p != this->var_deps_.end() ? p->second : NULL;
562 // Queue up a type-specific function to be written out. This is
563 // used when a type-specific function is needed when not at the top
564 // level.
565 void
566 queue_specific_type_function(Type* type, Named_type* name, int64_t size,
567 const std::string& hash_name,
568 Function_type* hash_fntype,
569 const std::string& equal_name,
570 Function_type* equal_fntype);
572 // Write out queued specific type functions.
573 void
574 write_specific_type_functions();
576 // Whether we are done writing out specific type functions.
577 bool
578 specific_type_functions_are_written() const
579 { return this->specific_type_functions_are_written_; }
581 // Add a pointer that needs to be added to the list of objects
582 // traversed by the garbage collector. This should be an expression
583 // of pointer type that points to static storage. It's not
584 // necessary to add global variables to this list, just global
585 // variable initializers that would otherwise not be seen.
586 void
587 add_gc_root(Expression* expr)
588 { this->gc_roots_.push_back(expr); }
590 // Traverse the tree. See the Traverse class.
591 void
592 traverse(Traverse*);
594 // Define the predeclared global names.
595 void
596 define_global_names();
598 // Verify and complete all types.
599 void
600 verify_types();
602 // Lower the parse tree.
603 void
604 lower_parse_tree();
606 // Lower all the statements in a block.
607 void
608 lower_block(Named_object* function, Block*);
610 // Lower an expression.
611 void
612 lower_expression(Named_object* function, Statement_inserter*, Expression**);
614 // Lower a constant.
615 void
616 lower_constant(Named_object*);
618 // Flatten all the statements in a block.
619 void
620 flatten_block(Named_object* function, Block*);
622 // Flatten an expression.
623 void
624 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
626 // Create all necessary function descriptors.
627 void
628 create_function_descriptors();
630 // Finalize the method lists and build stub methods for named types.
631 void
632 finalize_methods();
634 // Work out the types to use for unspecified variables and
635 // constants.
636 void
637 determine_types();
639 // Type check the program.
640 void
641 check_types();
643 // Check the types in a single block. This is used for complicated
644 // go statements.
645 void
646 check_types_in_block(Block*);
648 // Check for return statements.
649 void
650 check_return_statements();
652 // Analyze the program flow for escape information.
653 void
654 analyze_escape();
656 // Discover the groups of possibly recursive functions in this package.
657 void
658 discover_analysis_sets();
660 // Build a connectivity graph between the objects in each analyzed function.
661 void
662 assign_connectivity(Escape_context*, Named_object*);
664 // Traverse the objects in the connecitivty graph from the sink, adjusting the
665 // escape levels of each object.
666 void
667 propagate_escape(Escape_context*, Node*);
669 // Add notes about the escape level of a function's input and output
670 // parameters for exporting and importing top level functions.
671 void
672 tag_function(Escape_context*, Named_object*);
674 // Do all exports.
675 void
676 do_exports();
678 // Add an import control function for an imported package to the
679 // list.
680 void
681 add_import_init_fn(const std::string& package_name,
682 const std::string& init_name, int prio);
684 // Return the Import_init for a given init name.
685 Import_init*
686 lookup_init(const std::string& init_name);
688 // Turn short-cut operators (&&, ||) into explicit if statements.
689 void
690 remove_shortcuts();
692 // Use temporary variables to force order of evaluation.
693 void
694 order_evaluations();
696 // Flatten parse tree.
697 void
698 flatten();
700 // Build thunks for functions which call recover.
701 void
702 build_recover_thunks();
704 // Return a declaration for __builtin_return_address or
705 // __builtin_frame_address.
706 static Named_object*
707 declare_builtin_rf_address(const char* name);
709 // Simplify statements which might use thunks: go and defer
710 // statements.
711 void
712 simplify_thunk_statements();
714 // Dump AST if -fgo-dump-ast is set
715 void
716 dump_ast(const char* basename);
718 // Dump Call Graph if -fgo-dump-calls is set.
719 void
720 dump_call_graph(const char* basename);
722 // Dump Connection Graphs if -fgo-dump-connections is set.
723 void
724 dump_connection_graphs(const char* basename);
726 // Convert named types to the backend representation.
727 void
728 convert_named_types();
730 // Convert named types in a list of bindings.
731 void
732 convert_named_types_in_bindings(Bindings*);
734 // True if named types have been converted to the backend
735 // representation.
736 bool
737 named_types_are_converted() const
738 { return this->named_types_are_converted_; }
740 // Write out the global values.
741 void
742 write_globals();
744 // Build a call to the runtime error function.
745 Expression*
746 runtime_error(int code, Location);
748 // Build required interface method tables.
749 void
750 build_interface_method_tables();
752 // Return an expression which allocates memory to hold values of type TYPE.
753 Expression*
754 allocate_memory(Type *type, Location);
756 // Get the name of the magic initialization function.
757 const std::string&
758 get_init_fn_name();
760 private:
761 // During parsing, we keep a stack of functions. Each function on
762 // the stack is one that we are currently parsing. For each
763 // function, we keep track of the current stack of blocks.
764 struct Open_function
766 // The function.
767 Named_object* function;
768 // The stack of active blocks in the function.
769 std::vector<Block*> blocks;
772 // The stack of functions.
773 typedef std::vector<Open_function> Open_functions;
775 // Set up the built-in unsafe package.
776 void
777 import_unsafe(const std::string&, bool is_exported, Location);
779 // Return the current binding contour.
780 Bindings*
781 current_bindings();
783 const Bindings*
784 current_bindings() const;
786 void
787 write_c_header();
789 // Get the decl for the magic initialization function.
790 Named_object*
791 initialization_function_decl();
793 // Create the magic initialization function.
794 Named_object*
795 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
797 // Initialize imported packages. BFUNCTION is the function
798 // into which the package init calls will be placed.
799 void
800 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
802 // Register variables with the garbage collector.
803 void
804 register_gc_vars(const std::vector<Named_object*>&,
805 std::vector<Bstatement*>&,
806 Bfunction* init_bfunction);
808 // Type used to map import names to packages.
809 typedef std::map<std::string, Package*> Imports;
811 // Type used to map package names to packages.
812 typedef std::map<std::string, Package*> Packages;
814 // Type used to map variables to the function calls that set them.
815 // This is used for initialization dependency analysis.
816 typedef std::map<Variable*, Named_object*> Var_deps;
818 // Type used to map identifiers in the file block to the location
819 // where they were defined.
820 typedef Unordered_map(std::string, Location) File_block_names;
822 // Type used to queue writing a type specific function.
823 struct Specific_type_function
825 Type* type;
826 Named_type* name;
827 int64_t size;
828 std::string hash_name;
829 Function_type* hash_fntype;
830 std::string equal_name;
831 Function_type* equal_fntype;
833 Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
834 const std::string& ahash_name,
835 Function_type* ahash_fntype,
836 const std::string& aequal_name,
837 Function_type* aequal_fntype)
838 : type(atype), name(aname), size(asize), hash_name(ahash_name),
839 hash_fntype(ahash_fntype), equal_name(aequal_name),
840 equal_fntype(aequal_fntype)
844 // Recompute init priorities.
845 void
846 recompute_init_priorities();
848 // Recursive helper used by the routine above.
849 void
850 update_init_priority(Import_init* ii,
851 std::set<const Import_init *>* visited);
853 // The backend generator.
854 Backend* backend_;
855 // The object used to keep track of file names and line numbers.
856 Linemap* linemap_;
857 // The package we are compiling.
858 Package* package_;
859 // The list of currently open functions during parsing.
860 Open_functions functions_;
861 // The global binding contour. This includes the builtin functions
862 // and the package we are compiling.
863 Bindings* globals_;
864 // The list of names we have seen in the file block.
865 File_block_names file_block_names_;
866 // Mapping from import file names to packages.
867 Imports imports_;
868 // Whether the magic unsafe package was imported.
869 bool imported_unsafe_;
870 // Whether the magic unsafe package was imported by the current file.
871 bool current_file_imported_unsafe_;
872 // Mapping from package names we have seen to packages. This does
873 // not include the package we are compiling.
874 Packages packages_;
875 // The functions named "init", if there are any.
876 std::vector<Named_object*> init_functions_;
877 // A mapping from variables to the function calls that initialize
878 // them, if it is not stored in the variable's init or preinit.
879 // This is used for dependency analysis.
880 Var_deps var_deps_;
881 // Whether we need a magic initialization function.
882 bool need_init_fn_;
883 // The name of the magic initialization function.
884 std::string init_fn_name_;
885 // A list of import control variables for packages that we import.
886 Import_init_set imported_init_fns_;
887 // The package path used for reflection data.
888 std::string pkgpath_;
889 // The package path to use for a symbol name.
890 std::string pkgpath_symbol_;
891 // The prefix to use for symbols, from the -fgo-prefix option.
892 std::string prefix_;
893 // Whether pkgpath_ has been set.
894 bool pkgpath_set_;
895 // Whether an explicit package path was set by -fgo-pkgpath.
896 bool pkgpath_from_option_;
897 // Whether an explicit prefix was set by -fgo-prefix.
898 bool prefix_from_option_;
899 // The relative import path, from the -fgo-relative-import-path
900 // option.
901 std::string relative_import_path_;
902 // The C header file to write, from the -fgo-c-header option.
903 std::string c_header_;
904 // Whether or not to check for division by zero, from the
905 // -fgo-check-divide-zero option.
906 bool check_divide_by_zero_;
907 // Whether or not to check for division overflow, from the
908 // -fgo-check-divide-overflow option.
909 bool check_divide_overflow_;
910 // Whether we are compiling the runtime package, from the
911 // -fgo-compiling-runtime option.
912 bool compiling_runtime_;
913 // The level of escape analysis debug information to emit, from the
914 // -fgo-debug-escape option.
915 int debug_escape_level_;
916 // A list of types to verify.
917 std::vector<Type*> verify_types_;
918 // A list of interface types defined while parsing.
919 std::vector<Interface_type*> interface_types_;
920 // Type specific functions to write out.
921 std::vector<Specific_type_function*> specific_type_functions_;
922 // Whether we are done writing out specific type functions.
923 bool specific_type_functions_are_written_;
924 // Whether named types have been converted.
925 bool named_types_are_converted_;
926 // A list containing groups of possibly mutually recursive functions to be
927 // considered during escape analysis.
928 std::vector<Analysis_set> analysis_sets_;
929 // A list of objects to add to the GC roots.
930 std::vector<Expression*> gc_roots_;
933 // A block of statements.
935 class Block
937 public:
938 Block(Block* enclosing, Location);
940 // Return the enclosing block.
941 const Block*
942 enclosing() const
943 { return this->enclosing_; }
945 // Return the bindings of the block.
946 Bindings*
947 bindings()
948 { return this->bindings_; }
950 const Bindings*
951 bindings() const
952 { return this->bindings_; }
954 // Look at the block's statements.
955 const std::vector<Statement*>*
956 statements() const
957 { return &this->statements_; }
959 // Return the start location. This is normally the location of the
960 // left curly brace which starts the block.
961 Location
962 start_location() const
963 { return this->start_location_; }
965 // Return the end location. This is normally the location of the
966 // right curly brace which ends the block.
967 Location
968 end_location() const
969 { return this->end_location_; }
971 // Add a statement to the block.
972 void
973 add_statement(Statement*);
975 // Add a statement to the front of the block.
976 void
977 add_statement_at_front(Statement*);
979 // Replace a statement in a block.
980 void
981 replace_statement(size_t index, Statement*);
983 // Add a Statement before statement number INDEX.
984 void
985 insert_statement_before(size_t index, Statement*);
987 // Add a Statement after statement number INDEX.
988 void
989 insert_statement_after(size_t index, Statement*);
991 // Set the end location of the block.
992 void
993 set_end_location(Location location)
994 { this->end_location_ = location; }
996 // Traverse the tree.
998 traverse(Traverse*);
1000 // Set final types for unspecified variables and constants.
1001 void
1002 determine_types();
1004 // Return true if execution of this block may fall through to the
1005 // next block.
1006 bool
1007 may_fall_through() const;
1009 // Convert the block to the backend representation.
1010 Bblock*
1011 get_backend(Translate_context*);
1013 // Iterate over statements.
1015 typedef std::vector<Statement*>::iterator iterator;
1017 iterator
1018 begin()
1019 { return this->statements_.begin(); }
1021 iterator
1022 end()
1023 { return this->statements_.end(); }
1025 private:
1026 // Enclosing block.
1027 Block* enclosing_;
1028 // Statements in the block.
1029 std::vector<Statement*> statements_;
1030 // Binding contour.
1031 Bindings* bindings_;
1032 // Location of start of block.
1033 Location start_location_;
1034 // Location of end of block.
1035 Location end_location_;
1038 // A function.
1040 class Function
1042 public:
1043 Function(Function_type* type, Named_object*, Block*, Location);
1045 // Return the function's type.
1046 Function_type*
1047 type() const
1048 { return this->type_; }
1050 // Return the enclosing function if there is one.
1051 Named_object*
1052 enclosing() const
1053 { return this->enclosing_; }
1055 // Set the enclosing function. This is used when building thunks
1056 // for functions which call recover.
1057 void
1058 set_enclosing(Named_object* enclosing)
1060 go_assert(this->enclosing_ == NULL);
1061 this->enclosing_ = enclosing;
1064 // The result variables.
1065 typedef std::vector<Named_object*> Results;
1067 // Create the result variables in the outer block.
1068 void
1069 create_result_variables(Gogo*);
1071 // Update the named result variables when cloning a function which
1072 // calls recover.
1073 void
1074 update_result_variables();
1076 // Return the result variables.
1077 Results*
1078 result_variables()
1079 { return this->results_; }
1081 bool
1082 is_sink() const
1083 { return this->is_sink_; }
1085 void
1086 set_is_sink()
1087 { this->is_sink_ = true; }
1089 // Whether the result variables have names.
1090 bool
1091 results_are_named() const
1092 { return this->results_are_named_; }
1094 // Set the assembler name.
1095 void
1096 set_asm_name(const std::string& asm_name)
1097 { this->asm_name_ = asm_name; }
1099 // Set the pragmas for this function.
1100 void
1101 set_pragmas(unsigned int pragmas)
1103 this->pragmas_ = pragmas;
1106 // Whether this method should not be included in the type
1107 // descriptor.
1108 bool
1109 nointerface() const;
1111 // Record that this method should not be included in the type
1112 // descriptor.
1113 void
1114 set_nointerface();
1116 // Record that this function is a stub method created for an unnamed
1117 // type.
1118 void
1119 set_is_unnamed_type_stub_method()
1121 go_assert(this->is_method());
1122 this->is_unnamed_type_stub_method_ = true;
1125 // Return the amount of enclosed variables in this closure.
1126 size_t
1127 closure_field_count() const
1128 { return this->closure_fields_.size(); }
1130 // Add a new field to the closure variable.
1131 void
1132 add_closure_field(Named_object* var, Location loc)
1133 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1135 // Whether this function needs a closure.
1136 bool
1137 needs_closure() const
1138 { return !this->closure_fields_.empty(); }
1140 // Return the closure variable, creating it if necessary. This is
1141 // passed to the function as a static chain parameter.
1142 Named_object*
1143 closure_var();
1145 // Set the closure variable. This is used when building thunks for
1146 // functions which call recover.
1147 void
1148 set_closure_var(Named_object* v)
1150 go_assert(this->closure_var_ == NULL);
1151 this->closure_var_ = v;
1154 // Return the variable for a reference to field INDEX in the closure
1155 // variable.
1156 Named_object*
1157 enclosing_var(unsigned int index)
1159 go_assert(index < this->closure_fields_.size());
1160 return closure_fields_[index].first;
1163 // Set the type of the closure variable if there is one.
1164 void
1165 set_closure_type();
1167 // Get the block of statements associated with the function.
1168 Block*
1169 block() const
1170 { return this->block_; }
1172 // Get the location of the start of the function.
1173 Location
1174 location() const
1175 { return this->location_; }
1177 // Return whether this function is actually a method.
1178 bool
1179 is_method() const;
1181 // Add a label definition to the function.
1182 Label*
1183 add_label_definition(Gogo*, const std::string& label_name, Location);
1185 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1186 // if we should report errors for a goto from the current location
1187 // to the label location.
1188 Label*
1189 add_label_reference(Gogo*, const std::string& label_name,
1190 Location, bool issue_goto_errors);
1192 // Warn about labels that are defined but not used.
1193 void
1194 check_labels() const;
1196 // Note that a new local type has been added. Return its index.
1197 unsigned int
1198 new_local_type_index()
1199 { return this->local_type_count_++; }
1201 // Whether this function calls the predeclared recover function.
1202 bool
1203 calls_recover() const
1204 { return this->calls_recover_; }
1206 // Record that this function calls the predeclared recover function.
1207 // This is set during the lowering pass.
1208 void
1209 set_calls_recover()
1210 { this->calls_recover_ = true; }
1212 // Whether this is a recover thunk function.
1213 bool
1214 is_recover_thunk() const
1215 { return this->is_recover_thunk_; }
1217 // Record that this is a thunk built for a function which calls
1218 // recover.
1219 void
1220 set_is_recover_thunk()
1221 { this->is_recover_thunk_ = true; }
1223 // Whether this function already has a recover thunk.
1224 bool
1225 has_recover_thunk() const
1226 { return this->has_recover_thunk_; }
1228 // Record that this function already has a recover thunk.
1229 void
1230 set_has_recover_thunk()
1231 { this->has_recover_thunk_ = true; }
1233 // Record that this function is a thunk created for a defer
1234 // statement that calls the __go_set_defer_retaddr runtime function.
1235 void
1236 set_calls_defer_retaddr()
1237 { this->calls_defer_retaddr_ = true; }
1239 // Whether this is a type hash or equality function created by the
1240 // compiler.
1241 bool
1242 is_type_specific_function()
1243 { return this->is_type_specific_function_; }
1245 // Record that this function is a type hash or equality function
1246 // created by the compiler.
1247 void
1248 set_is_type_specific_function()
1249 { this->is_type_specific_function_ = true; }
1251 // Mark the function as going into a unique section.
1252 void
1253 set_in_unique_section()
1254 { this->in_unique_section_ = true; }
1256 // Swap with another function. Used only for the thunk which calls
1257 // recover.
1258 void
1259 swap_for_recover(Function *);
1261 // Traverse the tree.
1263 traverse(Traverse*);
1265 // Determine types in the function.
1266 void
1267 determine_types();
1269 // Return an expression for the function descriptor, given the named
1270 // object for this function. This may only be called for functions
1271 // without a closure. This will be an immutable struct with one
1272 // field that points to the function's code.
1273 Expression*
1274 descriptor(Gogo*, Named_object*);
1276 // Set the descriptor for this function. This is used when a
1277 // function declaration is followed by a function definition.
1278 void
1279 set_descriptor(Expression* descriptor)
1281 go_assert(this->descriptor_ == NULL);
1282 this->descriptor_ = descriptor;
1285 // Return the backend representation.
1286 Bfunction*
1287 get_or_make_decl(Gogo*, Named_object*);
1289 // Return the function's decl after it has been built.
1290 Bfunction*
1291 get_decl() const;
1293 // Set the function decl to hold a backend representation of the function
1294 // code.
1295 void
1296 build(Gogo*, Named_object*);
1298 // Get the statement that assigns values to this function's result struct.
1299 Bstatement*
1300 return_value(Gogo*, Named_object*, Location) const;
1302 // Get an expression for the variable holding the defer stack.
1303 Expression*
1304 defer_stack(Location);
1306 // Export the function.
1307 void
1308 export_func(Export*, const std::string& name) const;
1310 // Export a function with a type.
1311 static void
1312 export_func_with_type(Export*, const std::string& name,
1313 const Function_type*);
1315 // Import a function.
1316 static void
1317 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1318 Typed_identifier_list** pparameters,
1319 Typed_identifier_list** presults, bool* is_varargs);
1321 private:
1322 // Type for mapping from label names to Label objects.
1323 typedef Unordered_map(std::string, Label*) Labels;
1325 void
1326 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1328 typedef std::vector<std::pair<Named_object*,
1329 Location> > Closure_fields;
1331 // The function's type.
1332 Function_type* type_;
1333 // The enclosing function. This is NULL when there isn't one, which
1334 // is the normal case.
1335 Named_object* enclosing_;
1336 // The result variables, if any.
1337 Results* results_;
1338 // If there is a closure, this is the list of variables which appear
1339 // in the closure. This is created by the parser, and then resolved
1340 // to a real type when we lower parse trees.
1341 Closure_fields closure_fields_;
1342 // The closure variable, passed as a parameter using the static
1343 // chain parameter. Normally NULL.
1344 Named_object* closure_var_;
1345 // The outer block of statements in the function.
1346 Block* block_;
1347 // The source location of the start of the function.
1348 Location location_;
1349 // Labels defined or referenced in the function.
1350 Labels labels_;
1351 // The number of local types defined in this function.
1352 unsigned int local_type_count_;
1353 // The assembler name: this is the name that will be put in the object file.
1354 // Set by the go:linkname compiler directive. This is normally empty.
1355 std::string asm_name_;
1356 // The function descriptor, if any.
1357 Expression* descriptor_;
1358 // The function decl.
1359 Bfunction* fndecl_;
1360 // The defer stack variable. A pointer to this variable is used to
1361 // distinguish the defer stack for one function from another. This
1362 // is NULL unless we actually need a defer stack.
1363 Temporary_statement* defer_stack_;
1364 // Pragmas for this function. This is a set of GOPRAGMA bits.
1365 unsigned int pragmas_;
1366 // True if this function is sink-named. No code is generated.
1367 bool is_sink_ : 1;
1368 // True if the result variables are named.
1369 bool results_are_named_ : 1;
1370 // True if this function is a stub method created for an unnamed
1371 // type.
1372 bool is_unnamed_type_stub_method_ : 1;
1373 // True if this function calls the predeclared recover function.
1374 bool calls_recover_ : 1;
1375 // True if this a thunk built for a function which calls recover.
1376 bool is_recover_thunk_ : 1;
1377 // True if this function already has a recover thunk.
1378 bool has_recover_thunk_ : 1;
1379 // True if this is a thunk built for a defer statement that calls
1380 // the __go_set_defer_retaddr runtime function.
1381 bool calls_defer_retaddr_ : 1;
1382 // True if this is a function built by the compiler to as a hash or
1383 // equality function for some type.
1384 bool is_type_specific_function_ : 1;
1385 // True if this function should be put in a unique section. This is
1386 // turned on for field tracking.
1387 bool in_unique_section_ : 1;
1390 // A snapshot of the current binding state.
1392 class Bindings_snapshot
1394 public:
1395 Bindings_snapshot(const Block*, Location);
1397 // Report any errors appropriate for a goto from the current binding
1398 // state of B to this one.
1399 void
1400 check_goto_from(const Block* b, Location);
1402 // Report any errors appropriate for a goto from this binding state
1403 // to the current state of B.
1404 void
1405 check_goto_to(const Block* b);
1407 private:
1408 bool
1409 check_goto_block(Location, const Block*, const Block*, size_t*);
1411 void
1412 check_goto_defs(Location, const Block*, size_t, size_t);
1414 // The current block.
1415 const Block* block_;
1416 // The number of names currently defined in each open block.
1417 // Element 0 is this->block_, element 1 is
1418 // this->block_->enclosing(), etc.
1419 std::vector<size_t> counts_;
1420 // The location where this snapshot was taken.
1421 Location location_;
1424 // A function declaration.
1426 class Function_declaration
1428 public:
1429 Function_declaration(Function_type* fntype, Location location)
1430 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1431 fndecl_(NULL), pragmas_(0)
1434 Function_type*
1435 type() const
1436 { return this->fntype_; }
1438 Location
1439 location() const
1440 { return this->location_; }
1442 const std::string&
1443 asm_name() const
1444 { return this->asm_name_; }
1446 // Set the assembler name.
1447 void
1448 set_asm_name(const std::string& asm_name)
1449 { this->asm_name_ = asm_name; }
1451 // Set the pragmas for this function.
1452 void
1453 set_pragmas(unsigned int pragmas)
1455 this->pragmas_ = pragmas;
1458 // Return an expression for the function descriptor, given the named
1459 // object for this function. This may only be called for functions
1460 // without a closure. This will be an immutable struct with one
1461 // field that points to the function's code.
1462 Expression*
1463 descriptor(Gogo*, Named_object*);
1465 // Return true if we have created a descriptor for this declaration.
1466 bool
1467 has_descriptor() const
1468 { return this->descriptor_ != NULL; }
1470 // Return a backend representation.
1471 Bfunction*
1472 get_or_make_decl(Gogo*, Named_object*);
1474 // If there is a descriptor, build it into the backend
1475 // representation.
1476 void
1477 build_backend_descriptor(Gogo*);
1479 // Export a function declaration.
1480 void
1481 export_func(Export* exp, const std::string& name) const
1482 { Function::export_func_with_type(exp, name, this->fntype_); }
1484 // Check that the types used in this declaration's signature are defined.
1485 void
1486 check_types() const;
1488 private:
1489 // The type of the function.
1490 Function_type* fntype_;
1491 // The location of the declaration.
1492 Location location_;
1493 // The assembler name: this is the name to use in references to the
1494 // function. This is normally empty.
1495 std::string asm_name_;
1496 // The function descriptor, if any.
1497 Expression* descriptor_;
1498 // The function decl if needed.
1499 Bfunction* fndecl_;
1500 // Pragmas for this function. This is a set of GOPRAGMA bits.
1501 unsigned int pragmas_;
1504 // A variable.
1506 class Variable
1508 public:
1509 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1510 bool is_receiver, Location);
1512 // Get the type of the variable.
1513 Type*
1514 type();
1516 Type*
1517 type() const;
1519 // Return whether the type is defined yet.
1520 bool
1521 has_type() const;
1523 // Get the initial value.
1524 Expression*
1525 init() const
1526 { return this->init_; }
1528 // Return whether there are any preinit statements.
1529 bool
1530 has_pre_init() const
1531 { return this->preinit_ != NULL; }
1533 // Return the preinit statements if any.
1534 Block*
1535 preinit() const
1536 { return this->preinit_; }
1538 // Return whether this is a global variable.
1539 bool
1540 is_global() const
1541 { return this->is_global_; }
1543 // Return whether this is a function parameter.
1544 bool
1545 is_parameter() const
1546 { return this->is_parameter_; }
1548 // Return whether this is a closure (static chain) parameter.
1549 bool
1550 is_closure() const
1551 { return this->is_closure_; }
1553 // Change this parameter to be a closure.
1554 void
1555 set_is_closure()
1557 this->is_closure_ = true;
1560 // Return whether this is the receiver parameter of a method.
1561 bool
1562 is_receiver() const
1563 { return this->is_receiver_; }
1565 // Change this parameter to be a receiver. This is used when
1566 // creating the thunks created for functions which call recover.
1567 void
1568 set_is_receiver()
1570 go_assert(this->is_parameter_);
1571 this->is_receiver_ = true;
1574 // Change this parameter to not be a receiver. This is used when
1575 // creating the thunks created for functions which call recover.
1576 void
1577 set_is_not_receiver()
1579 go_assert(this->is_parameter_);
1580 this->is_receiver_ = false;
1583 // Return whether this is the varargs parameter of a function.
1584 bool
1585 is_varargs_parameter() const
1586 { return this->is_varargs_parameter_; }
1588 // Whether this variable's address is taken.
1589 bool
1590 is_address_taken() const
1591 { return this->is_address_taken_; }
1593 // Whether this variable should live in the heap.
1594 bool
1595 is_in_heap() const
1597 return this->is_address_taken_
1598 && this->escapes_
1599 && !this->is_global_;
1602 // Note that something takes the address of this variable.
1603 void
1604 set_address_taken()
1605 { this->is_address_taken_ = true; }
1607 // Return whether the address is taken but does not escape.
1608 bool
1609 is_non_escaping_address_taken() const
1610 { return this->is_non_escaping_address_taken_; }
1612 // Note that something takes the address of this variable such that
1613 // the address does not escape the function.
1614 void
1615 set_non_escaping_address_taken()
1616 { this->is_non_escaping_address_taken_ = true; }
1618 // Return whether this variable escapes the function it is declared in.
1619 bool
1620 escapes()
1621 { return this->escapes_; }
1623 // Note that this variable does not escape the function it is declared in.
1624 void
1625 set_does_not_escape()
1626 { this->escapes_ = false; }
1628 // Get the source location of the variable's declaration.
1629 Location
1630 location() const
1631 { return this->location_; }
1633 // Record that this is the varargs parameter of a function.
1634 void
1635 set_is_varargs_parameter()
1637 go_assert(this->is_parameter_);
1638 this->is_varargs_parameter_ = true;
1641 // Return whether the variable has been used.
1642 bool
1643 is_used() const
1644 { return this->is_used_; }
1646 // Mark that the variable has been used.
1647 void
1648 set_is_used()
1649 { this->is_used_ = true; }
1651 // Clear the initial value; used for error handling.
1652 void
1653 clear_init()
1654 { this->init_ = NULL; }
1656 // Set the initial value; used for converting shortcuts.
1657 void
1658 set_init(Expression* init)
1659 { this->init_ = init; }
1661 // Get the preinit block, a block of statements to be run before the
1662 // initialization expression.
1663 Block*
1664 preinit_block(Gogo*);
1666 // Add a statement to be run before the initialization expression.
1667 // This is only used for global variables.
1668 void
1669 add_preinit_statement(Gogo*, Statement*);
1671 // Lower the initialization expression after parsing is complete.
1672 void
1673 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1675 // Flatten the initialization expression after ordering evaluations.
1676 void
1677 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1679 // A special case: the init value is used only to determine the
1680 // type. This is used if the variable is defined using := with the
1681 // comma-ok form of a map index or a receive expression. The init
1682 // value is actually the map index expression or receive expression.
1683 // We use this because we may not know the right type at parse time.
1684 void
1685 set_type_from_init_tuple()
1686 { this->type_from_init_tuple_ = true; }
1688 // Another special case: the init value is used only to determine
1689 // the type. This is used if the variable is defined using := with
1690 // a range clause. The init value is the range expression. The
1691 // type of the variable is the index type of the range expression
1692 // (i.e., the first value returned by a range).
1693 void
1694 set_type_from_range_index()
1695 { this->type_from_range_index_ = true; }
1697 // Another special case: like set_type_from_range_index, but the
1698 // type is the value type of the range expression (i.e., the second
1699 // value returned by a range).
1700 void
1701 set_type_from_range_value()
1702 { this->type_from_range_value_ = true; }
1704 // Another special case: the init value is used only to determine
1705 // the type. This is used if the variable is defined using := with
1706 // a case in a select statement. The init value is the channel.
1707 // The type of the variable is the channel's element type.
1708 void
1709 set_type_from_chan_element()
1710 { this->type_from_chan_element_ = true; }
1712 // After we lower the select statement, we once again set the type
1713 // from the initialization expression.
1714 void
1715 clear_type_from_chan_element()
1717 go_assert(this->type_from_chan_element_);
1718 this->type_from_chan_element_ = false;
1721 // TRUE if this variable was created for a type switch clause.
1722 bool
1723 is_type_switch_var() const
1724 { return this->is_type_switch_var_; }
1726 // Note that this variable was created for a type switch clause.
1727 void
1728 set_is_type_switch_var()
1729 { this->is_type_switch_var_ = true; }
1731 // Mark the variable as going into a unique section.
1732 void
1733 set_in_unique_section()
1735 go_assert(this->is_global_);
1736 this->in_unique_section_ = true;
1739 // Traverse the initializer expression.
1741 traverse_expression(Traverse*, unsigned int traverse_mask);
1743 // Determine the type of the variable if necessary.
1744 void
1745 determine_type();
1747 // Get the backend representation of the variable.
1748 Bvariable*
1749 get_backend_variable(Gogo*, Named_object*, const Package*,
1750 const std::string&);
1752 // Get the initial value of the variable. This may only
1753 // be called if has_pre_init() returns false.
1754 Bexpression*
1755 get_init(Gogo*, Named_object* function);
1757 // Return a series of statements which sets the value of the
1758 // variable in DECL. This should only be called is has_pre_init()
1759 // returns true. DECL may be NULL for a sink variable.
1760 Bstatement*
1761 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1763 // Export the variable.
1764 void
1765 export_var(Export*, const std::string& name) const;
1767 // Import a variable.
1768 static void
1769 import_var(Import*, std::string* pname, Type** ptype);
1771 private:
1772 // The type of a tuple.
1773 Type*
1774 type_from_tuple(Expression*, bool) const;
1776 // The type of a range.
1777 Type*
1778 type_from_range(Expression*, bool, bool) const;
1780 // The element type of a channel.
1781 Type*
1782 type_from_chan_element(Expression*, bool) const;
1784 // The variable's type. This may be NULL if the type is set from
1785 // the expression.
1786 Type* type_;
1787 // The initial value. This may be NULL if the variable should be
1788 // initialized to the default value for the type.
1789 Expression* init_;
1790 // Statements to run before the init statement.
1791 Block* preinit_;
1792 // Location of variable definition.
1793 Location location_;
1794 // Backend representation.
1795 Bvariable* backend_;
1796 // Whether this is a global variable.
1797 bool is_global_ : 1;
1798 // Whether this is a function parameter.
1799 bool is_parameter_ : 1;
1800 // Whether this is a closure parameter.
1801 bool is_closure_ : 1;
1802 // Whether this is the receiver parameter of a method.
1803 bool is_receiver_ : 1;
1804 // Whether this is the varargs parameter of a function.
1805 bool is_varargs_parameter_ : 1;
1806 // Whether this variable is ever referenced.
1807 bool is_used_ : 1;
1808 // Whether something takes the address of this variable. For a
1809 // local variable this implies that the variable has to be on the
1810 // heap if it escapes from its function.
1811 bool is_address_taken_ : 1;
1812 // Whether something takes the address of this variable such that
1813 // the address does not escape the function.
1814 bool is_non_escaping_address_taken_ : 1;
1815 // True if we have seen this variable in a traversal.
1816 bool seen_ : 1;
1817 // True if we have lowered the initialization expression.
1818 bool init_is_lowered_ : 1;
1819 // True if we have flattened the initialization expression.
1820 bool init_is_flattened_ : 1;
1821 // True if init is a tuple used to set the type.
1822 bool type_from_init_tuple_ : 1;
1823 // True if init is a range clause and the type is the index type.
1824 bool type_from_range_index_ : 1;
1825 // True if init is a range clause and the type is the value type.
1826 bool type_from_range_value_ : 1;
1827 // True if init is a channel and the type is the channel's element type.
1828 bool type_from_chan_element_ : 1;
1829 // True if this is a variable created for a type switch case.
1830 bool is_type_switch_var_ : 1;
1831 // True if we have determined types.
1832 bool determined_type_ : 1;
1833 // True if this variable should be put in a unique section. This is
1834 // used for field tracking.
1835 bool in_unique_section_ : 1;
1836 // Whether this variable escapes the function it is created in. This is
1837 // true until shown otherwise.
1838 bool escapes_ : 1;
1841 // A variable which is really the name for a function return value, or
1842 // part of one.
1844 class Result_variable
1846 public:
1847 Result_variable(Type* type, Function* function, int index,
1848 Location location)
1849 : type_(type), function_(function), index_(index), location_(location),
1850 backend_(NULL), is_address_taken_(false),
1851 is_non_escaping_address_taken_(false), escapes_(true)
1854 // Get the type of the result variable.
1855 Type*
1856 type() const
1857 { return this->type_; }
1859 // Get the function that this is associated with.
1860 Function*
1861 function() const
1862 { return this->function_; }
1864 // Index in the list of function results.
1866 index() const
1867 { return this->index_; }
1869 // The location of the variable definition.
1870 Location
1871 location() const
1872 { return this->location_; }
1874 // Whether this variable's address is taken.
1875 bool
1876 is_address_taken() const
1877 { return this->is_address_taken_; }
1879 // Note that something takes the address of this variable.
1880 void
1881 set_address_taken()
1882 { this->is_address_taken_ = true; }
1884 // Return whether the address is taken but does not escape.
1885 bool
1886 is_non_escaping_address_taken() const
1887 { return this->is_non_escaping_address_taken_; }
1889 // Note that something takes the address of this variable such that
1890 // the address does not escape the function.
1891 void
1892 set_non_escaping_address_taken()
1893 { this->is_non_escaping_address_taken_ = true; }
1895 // Return whether this variable escapes the function it is declared in.
1896 bool
1897 escapes()
1898 { return this->escapes_; }
1900 // Note that this variable does not escape the function it is declared in.
1901 void
1902 set_does_not_escape()
1903 { this->escapes_ = false; }
1905 // Whether this variable should live in the heap.
1906 bool
1907 is_in_heap() const
1909 return this->is_address_taken_
1910 && this->escapes_;
1913 // Set the function. This is used when cloning functions which call
1914 // recover.
1915 void
1916 set_function(Function* function)
1917 { this->function_ = function; }
1919 // Get the backend representation of the variable.
1920 Bvariable*
1921 get_backend_variable(Gogo*, Named_object*, const std::string&);
1923 private:
1924 // Type of result variable.
1925 Type* type_;
1926 // Function with which this is associated.
1927 Function* function_;
1928 // Index in list of results.
1929 int index_;
1930 // Where the result variable is defined.
1931 Location location_;
1932 // Backend representation.
1933 Bvariable* backend_;
1934 // Whether something takes the address of this variable.
1935 bool is_address_taken_;
1936 // Whether something takes the address of this variable such that
1937 // the address does not escape the function.
1938 bool is_non_escaping_address_taken_;
1939 // Whether this variable escapes the function it is created in. This is
1940 // true until shown otherwise.
1941 bool escapes_;
1944 // The value we keep for a named constant. This lets us hold a type
1945 // and an expression.
1947 class Named_constant
1949 public:
1950 Named_constant(Type* type, Expression* expr, int iota_value,
1951 Location location)
1952 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1953 lowering_(false), is_sink_(false), bconst_(NULL)
1956 Type*
1957 type() const
1958 { return this->type_; }
1960 Expression*
1961 expr() const
1962 { return this->expr_; }
1965 iota_value() const
1966 { return this->iota_value_; }
1968 Location
1969 location() const
1970 { return this->location_; }
1972 // Whether we are lowering.
1973 bool
1974 lowering() const
1975 { return this->lowering_; }
1977 // Set that we are lowering.
1978 void
1979 set_lowering()
1980 { this->lowering_ = true; }
1982 // We are no longer lowering.
1983 void
1984 clear_lowering()
1985 { this->lowering_ = false; }
1987 bool
1988 is_sink() const
1989 { return this->is_sink_; }
1991 void
1992 set_is_sink()
1993 { this->is_sink_ = true; }
1995 // Traverse the expression.
1997 traverse_expression(Traverse*);
1999 // Determine the type of the constant if necessary.
2000 void
2001 determine_type();
2003 // Indicate that we found and reported an error for this constant.
2004 void
2005 set_error();
2007 // Export the constant.
2008 void
2009 export_const(Export*, const std::string& name) const;
2011 // Import a constant.
2012 static void
2013 import_const(Import*, std::string*, Type**, Expression**);
2015 // Get the backend representation of the constant value.
2016 Bexpression*
2017 get_backend(Gogo*, Named_object*);
2019 private:
2020 // The type of the constant.
2021 Type* type_;
2022 // The expression for the constant.
2023 Expression* expr_;
2024 // If the predeclared constant iota is used in EXPR_, this is the
2025 // value it will have. We do this because at parse time we don't
2026 // know whether the name "iota" will refer to the predeclared
2027 // constant or to something else. We put in the right value in when
2028 // we lower.
2029 int iota_value_;
2030 // The location of the definition.
2031 Location location_;
2032 // Whether we are currently lowering this constant.
2033 bool lowering_;
2034 // Whether this constant is blank named and needs only type checking.
2035 bool is_sink_;
2036 // The backend representation of the constant value.
2037 Bexpression* bconst_;
2040 // A type declaration.
2042 class Type_declaration
2044 public:
2045 Type_declaration(Location location)
2046 : location_(location), in_function_(NULL), in_function_index_(0),
2047 methods_(), issued_warning_(false)
2050 // Return the location.
2051 Location
2052 location() const
2053 { return this->location_; }
2055 // Return the function in which this type is declared. This will
2056 // return NULL for a type declared in global scope.
2057 Named_object*
2058 in_function(unsigned int* pindex)
2060 *pindex = this->in_function_index_;
2061 return this->in_function_;
2064 // Set the function in which this type is declared.
2065 void
2066 set_in_function(Named_object* f, unsigned int index)
2068 this->in_function_ = f;
2069 this->in_function_index_ = index;
2072 // Add a method to this type. This is used when methods are defined
2073 // before the type.
2074 Named_object*
2075 add_method(const std::string& name, Function* function);
2077 // Add a method declaration to this type.
2078 Named_object*
2079 add_method_declaration(const std::string& name, Package*,
2080 Function_type* type, Location location);
2082 // Return whether any methods were defined.
2083 bool
2084 has_methods() const;
2086 // Return the methods.
2087 const std::vector<Named_object*>*
2088 methods() const
2089 { return &this->methods_; }
2091 // Define methods when the real type is known.
2092 void
2093 define_methods(Named_type*);
2095 // This is called if we are trying to use this type. It returns
2096 // true if we should issue a warning.
2097 bool
2098 using_type();
2100 private:
2101 // The location of the type declaration.
2102 Location location_;
2103 // If this type is declared in a function, a pointer back to the
2104 // function in which it is defined.
2105 Named_object* in_function_;
2106 // The index of this type in IN_FUNCTION_.
2107 unsigned int in_function_index_;
2108 // Methods defined before the type is defined.
2109 std::vector<Named_object*> methods_;
2110 // True if we have issued a warning about a use of this type
2111 // declaration when it is undefined.
2112 bool issued_warning_;
2115 // An unknown object. These are created by the parser for forward
2116 // references to names which have not been seen before. In a correct
2117 // program, these will always point to a real definition by the end of
2118 // the parse. Because they point to another Named_object, these may
2119 // only be referenced by Unknown_expression objects.
2121 class Unknown_name
2123 public:
2124 Unknown_name(Location location)
2125 : location_(location), real_named_object_(NULL)
2128 // Return the location where this name was first seen.
2129 Location
2130 location() const
2131 { return this->location_; }
2133 // Return the real named object that this points to, or NULL if it
2134 // was never resolved.
2135 Named_object*
2136 real_named_object() const
2137 { return this->real_named_object_; }
2139 // Set the real named object that this points to.
2140 void
2141 set_real_named_object(Named_object* no);
2143 private:
2144 // The location where this name was first seen.
2145 Location location_;
2146 // The real named object when it is known.
2147 Named_object*
2148 real_named_object_;
2151 // A named object named. This is the result of a declaration. We
2152 // don't use a superclass because they all have to be handled
2153 // differently.
2155 class Named_object
2157 public:
2158 enum Classification
2160 // An uninitialized Named_object. We should never see this.
2161 NAMED_OBJECT_UNINITIALIZED,
2162 // An erroneous name. This indicates a parse error, to avoid
2163 // later errors about undefined references.
2164 NAMED_OBJECT_ERRONEOUS,
2165 // An unknown name. This is used for forward references. In a
2166 // correct program, these will all be resolved by the end of the
2167 // parse.
2168 NAMED_OBJECT_UNKNOWN,
2169 // A const.
2170 NAMED_OBJECT_CONST,
2171 // A type.
2172 NAMED_OBJECT_TYPE,
2173 // A forward type declaration.
2174 NAMED_OBJECT_TYPE_DECLARATION,
2175 // A var.
2176 NAMED_OBJECT_VAR,
2177 // A result variable in a function.
2178 NAMED_OBJECT_RESULT_VAR,
2179 // The blank identifier--the special variable named _.
2180 NAMED_OBJECT_SINK,
2181 // A func.
2182 NAMED_OBJECT_FUNC,
2183 // A forward func declaration.
2184 NAMED_OBJECT_FUNC_DECLARATION,
2185 // A package.
2186 NAMED_OBJECT_PACKAGE
2189 // Return the classification.
2190 Classification
2191 classification() const
2192 { return this->classification_; }
2194 // Classifiers.
2196 bool
2197 is_erroneous() const
2198 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2200 bool
2201 is_unknown() const
2202 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2204 bool
2205 is_const() const
2206 { return this->classification_ == NAMED_OBJECT_CONST; }
2208 bool
2209 is_type() const
2210 { return this->classification_ == NAMED_OBJECT_TYPE; }
2212 bool
2213 is_type_declaration() const
2214 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2216 bool
2217 is_variable() const
2218 { return this->classification_ == NAMED_OBJECT_VAR; }
2220 bool
2221 is_result_variable() const
2222 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2224 bool
2225 is_sink() const
2226 { return this->classification_ == NAMED_OBJECT_SINK; }
2228 bool
2229 is_function() const
2230 { return this->classification_ == NAMED_OBJECT_FUNC; }
2232 bool
2233 is_function_declaration() const
2234 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2236 bool
2237 is_package() const
2238 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2240 // Creators.
2242 static Named_object*
2243 make_erroneous_name(const std::string& name)
2244 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2246 static Named_object*
2247 make_unknown_name(const std::string& name, Location);
2249 static Named_object*
2250 make_constant(const Typed_identifier&, const Package*, Expression*,
2251 int iota_value);
2253 static Named_object*
2254 make_type(const std::string&, const Package*, Type*, Location);
2256 static Named_object*
2257 make_type_declaration(const std::string&, const Package*, Location);
2259 static Named_object*
2260 make_variable(const std::string&, const Package*, Variable*);
2262 static Named_object*
2263 make_result_variable(const std::string&, Result_variable*);
2265 static Named_object*
2266 make_sink();
2268 static Named_object*
2269 make_function(const std::string&, const Package*, Function*);
2271 static Named_object*
2272 make_function_declaration(const std::string&, const Package*, Function_type*,
2273 Location);
2275 static Named_object*
2276 make_package(const std::string& alias, Package* package);
2278 // Getters.
2280 Unknown_name*
2281 unknown_value()
2283 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2284 return this->u_.unknown_value;
2287 const Unknown_name*
2288 unknown_value() const
2290 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2291 return this->u_.unknown_value;
2294 Named_constant*
2295 const_value()
2297 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2298 return this->u_.const_value;
2301 const Named_constant*
2302 const_value() const
2304 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2305 return this->u_.const_value;
2308 Named_type*
2309 type_value()
2311 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2312 return this->u_.type_value;
2315 const Named_type*
2316 type_value() const
2318 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2319 return this->u_.type_value;
2322 Type_declaration*
2323 type_declaration_value()
2325 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2326 return this->u_.type_declaration;
2329 const Type_declaration*
2330 type_declaration_value() const
2332 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2333 return this->u_.type_declaration;
2336 Variable*
2337 var_value()
2339 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2340 return this->u_.var_value;
2343 const Variable*
2344 var_value() const
2346 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2347 return this->u_.var_value;
2350 Result_variable*
2351 result_var_value()
2353 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2354 return this->u_.result_var_value;
2357 const Result_variable*
2358 result_var_value() const
2360 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2361 return this->u_.result_var_value;
2364 Function*
2365 func_value()
2367 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2368 return this->u_.func_value;
2371 const Function*
2372 func_value() const
2374 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2375 return this->u_.func_value;
2378 Function_declaration*
2379 func_declaration_value()
2381 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2382 return this->u_.func_declaration_value;
2385 const Function_declaration*
2386 func_declaration_value() const
2388 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2389 return this->u_.func_declaration_value;
2392 Package*
2393 package_value()
2395 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2396 return this->u_.package_value;
2399 const Package*
2400 package_value() const
2402 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2403 return this->u_.package_value;
2406 const std::string&
2407 name() const
2408 { return this->name_; }
2410 // Return the name to use in an error message. The difference is
2411 // that if this Named_object is defined in a different package, this
2412 // will return PACKAGE.NAME.
2413 std::string
2414 message_name() const;
2416 const Package*
2417 package() const
2418 { return this->package_; }
2420 // Resolve an unknown value if possible. This returns the same
2421 // Named_object or a new one.
2422 Named_object*
2423 resolve()
2425 Named_object* ret = this;
2426 if (this->is_unknown())
2428 Named_object* r = this->unknown_value()->real_named_object();
2429 if (r != NULL)
2430 ret = r;
2432 return ret;
2435 const Named_object*
2436 resolve() const
2438 const Named_object* ret = this;
2439 if (this->is_unknown())
2441 const Named_object* r = this->unknown_value()->real_named_object();
2442 if (r != NULL)
2443 ret = r;
2445 return ret;
2448 // The location where this object was defined or referenced.
2449 Location
2450 location() const;
2452 // Convert a variable to the backend representation.
2453 Bvariable*
2454 get_backend_variable(Gogo*, Named_object* function);
2456 // Return the external identifier for this object.
2457 std::string
2458 get_id(Gogo*);
2460 // Get the backend representation of this object.
2461 void
2462 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2463 std::vector<Bfunction*>&);
2465 // Define a type declaration.
2466 void
2467 set_type_value(Named_type*);
2469 // Define a function declaration.
2470 void
2471 set_function_value(Function*);
2473 // Declare an unknown name as a type declaration.
2474 void
2475 declare_as_type();
2477 // Export this object.
2478 void
2479 export_named_object(Export*) const;
2481 // Mark this named object as an invalid redefinition of another object.
2482 void
2483 set_is_redefinition()
2484 { this->is_redefinition_ = true; }
2486 // Return whether or not this object is a invalid redefinition of another
2487 // object.
2488 bool
2489 is_redefinition() const
2490 { return this->is_redefinition_; }
2492 private:
2493 Named_object(const std::string&, const Package*, Classification);
2495 // The name of the object.
2496 std::string name_;
2497 // The package that this object is in. This is NULL if it is in the
2498 // file we are compiling.
2499 const Package* package_;
2500 // The type of object this is.
2501 Classification classification_;
2502 // The real data.
2503 union
2505 Unknown_name* unknown_value;
2506 Named_constant* const_value;
2507 Named_type* type_value;
2508 Type_declaration* type_declaration;
2509 Variable* var_value;
2510 Result_variable* result_var_value;
2511 Function* func_value;
2512 Function_declaration* func_declaration_value;
2513 Package* package_value;
2514 } u_;
2515 // True if this object is an invalid redefinition of another object.
2516 bool is_redefinition_;
2519 // A binding contour. This binds names to objects.
2521 class Bindings
2523 public:
2524 // Type for mapping from names to objects.
2525 typedef Unordered_map(std::string, Named_object*) Contour;
2527 Bindings(Bindings* enclosing);
2529 // Add an erroneous name.
2530 Named_object*
2531 add_erroneous_name(const std::string& name)
2532 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2534 // Add an unknown name.
2535 Named_object*
2536 add_unknown_name(const std::string& name, Location location)
2538 return this->add_named_object(Named_object::make_unknown_name(name,
2539 location));
2542 // Add a constant.
2543 Named_object*
2544 add_constant(const Typed_identifier& tid, const Package* package,
2545 Expression* expr, int iota_value)
2547 return this->add_named_object(Named_object::make_constant(tid, package,
2548 expr,
2549 iota_value));
2552 // Add a type.
2553 Named_object*
2554 add_type(const std::string& name, const Package* package, Type* type,
2555 Location location)
2557 return this->add_named_object(Named_object::make_type(name, package, type,
2558 location));
2561 // Add a named type. This is used for builtin types, and to add an
2562 // imported type to the global scope.
2563 Named_object*
2564 add_named_type(Named_type* named_type);
2566 // Add a type declaration.
2567 Named_object*
2568 add_type_declaration(const std::string& name, const Package* package,
2569 Location location)
2571 Named_object* no = Named_object::make_type_declaration(name, package,
2572 location);
2573 return this->add_named_object(no);
2576 // Add a variable.
2577 Named_object*
2578 add_variable(const std::string& name, const Package* package,
2579 Variable* variable)
2581 return this->add_named_object(Named_object::make_variable(name, package,
2582 variable));
2585 // Add a result variable.
2586 Named_object*
2587 add_result_variable(const std::string& name, Result_variable* result)
2589 return this->add_named_object(Named_object::make_result_variable(name,
2590 result));
2593 // Add a function.
2594 Named_object*
2595 add_function(const std::string& name, const Package*, Function* function);
2597 // Add a function declaration.
2598 Named_object*
2599 add_function_declaration(const std::string& name, const Package* package,
2600 Function_type* type, Location location);
2602 // Add a package. The location is the location of the import
2603 // statement.
2604 Named_object*
2605 add_package(const std::string& alias, Package* package)
2607 Named_object* no = Named_object::make_package(alias, package);
2608 return this->add_named_object(no);
2611 // Define a type which was already declared.
2612 void
2613 define_type(Named_object*, Named_type*);
2615 // Add a method to the list of objects. This is not added to the
2616 // lookup table.
2617 void
2618 add_method(Named_object*);
2620 // Add a named object to this binding.
2621 Named_object*
2622 add_named_object(Named_object* no)
2623 { return this->add_named_object_to_contour(&this->bindings_, no); }
2625 // Clear all names in file scope from the bindings.
2626 void
2627 clear_file_scope(Gogo*);
2629 // Look up a name in this binding contour and in any enclosing
2630 // binding contours. This returns NULL if the name is not found.
2631 Named_object*
2632 lookup(const std::string&) const;
2634 // Look up a name in this binding contour without looking in any
2635 // enclosing binding contours. Returns NULL if the name is not found.
2636 Named_object*
2637 lookup_local(const std::string&) const;
2639 // Remove a name.
2640 void
2641 remove_binding(Named_object*);
2643 // Mark all variables as used. This is used for some types of parse
2644 // error.
2645 void
2646 mark_locals_used();
2648 // Traverse the tree. See the Traverse class.
2650 traverse(Traverse*, bool is_global);
2652 // Iterate over definitions. This does not include things which
2653 // were only declared.
2655 typedef std::vector<Named_object*>::const_iterator
2656 const_definitions_iterator;
2658 const_definitions_iterator
2659 begin_definitions() const
2660 { return this->named_objects_.begin(); }
2662 const_definitions_iterator
2663 end_definitions() const
2664 { return this->named_objects_.end(); }
2666 // Return the number of definitions.
2667 size_t
2668 size_definitions() const
2669 { return this->named_objects_.size(); }
2671 // Return whether there are no definitions.
2672 bool
2673 empty_definitions() const
2674 { return this->named_objects_.empty(); }
2676 // Iterate over declarations. This is everything that has been
2677 // declared, which includes everything which has been defined.
2679 typedef Contour::const_iterator const_declarations_iterator;
2681 const_declarations_iterator
2682 begin_declarations() const
2683 { return this->bindings_.begin(); }
2685 const_declarations_iterator
2686 end_declarations() const
2687 { return this->bindings_.end(); }
2689 // Return the number of declarations.
2690 size_t
2691 size_declarations() const
2692 { return this->bindings_.size(); }
2694 // Return whether there are no declarations.
2695 bool
2696 empty_declarations() const
2697 { return this->bindings_.empty(); }
2699 // Return the first declaration.
2700 Named_object*
2701 first_declaration()
2702 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2704 private:
2705 Named_object*
2706 add_named_object_to_contour(Contour*, Named_object*);
2708 Named_object*
2709 new_definition(Named_object*, Named_object*);
2711 // Enclosing bindings.
2712 Bindings* enclosing_;
2713 // The list of objects.
2714 std::vector<Named_object*> named_objects_;
2715 // The mapping from names to objects.
2716 Contour bindings_;
2719 // A label.
2721 class Label
2723 public:
2724 Label(const std::string& name)
2725 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2726 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
2729 // Return the label's name.
2730 const std::string&
2731 name() const
2732 { return this->name_; }
2734 // Return whether the label has been defined.
2735 bool
2736 is_defined() const
2737 { return !Linemap::is_unknown_location(this->location_); }
2739 // Return whether the label has been used.
2740 bool
2741 is_used() const
2742 { return this->is_used_; }
2744 // Record that the label is used.
2745 void
2746 set_is_used()
2747 { this->is_used_ = true; }
2749 // Return whether this label is looping.
2750 bool
2751 looping() const
2752 { return this->depth_ == DEPTH_LOOPING; }
2754 // Set this label as looping.
2755 void
2756 set_looping()
2757 { this->depth_ = DEPTH_LOOPING; }
2759 // Return whether this label is nonlooping.
2760 bool
2761 nonlooping() const
2762 { return this->depth_ == DEPTH_NONLOOPING; }
2764 // Set this label as nonlooping.
2765 void
2766 set_nonlooping()
2767 { this->depth_ = DEPTH_NONLOOPING; }
2769 // Return the location of the definition.
2770 Location
2771 location() const
2772 { return this->location_; }
2774 // Return the bindings snapshot.
2775 Bindings_snapshot*
2776 snapshot() const
2777 { return this->snapshot_; }
2779 // Add a snapshot of a goto which refers to this label.
2780 void
2781 add_snapshot_ref(Bindings_snapshot* snapshot)
2783 go_assert(Linemap::is_unknown_location(this->location_));
2784 this->refs_.push_back(snapshot);
2787 // Return the list of snapshots of goto statements which refer to
2788 // this label.
2789 const std::vector<Bindings_snapshot*>&
2790 refs() const
2791 { return this->refs_; }
2793 // Clear the references.
2794 void
2795 clear_refs();
2797 // Define the label at LOCATION with the given bindings snapshot.
2798 void
2799 define(Location location, Bindings_snapshot* snapshot)
2801 if (this->is_dummy_label())
2802 return;
2803 go_assert(Linemap::is_unknown_location(this->location_)
2804 && this->snapshot_ == NULL);
2805 this->location_ = location;
2806 this->snapshot_ = snapshot;
2809 // Return the backend representation for this label.
2810 Blabel*
2811 get_backend_label(Translate_context*);
2813 // Return an expression for the address of this label. This is used
2814 // to get the return address of a deferred function to see whether
2815 // the function may call recover.
2816 Bexpression*
2817 get_addr(Translate_context*, Location location);
2819 // Return a dummy label, representing any instance of the blank label.
2820 static Label*
2821 create_dummy_label();
2823 // Return TRUE if this is a dummy label.
2824 bool
2825 is_dummy_label() const
2826 { return this->name_ == "_"; }
2828 // A classification of a label's looping depth.
2829 enum Loop_depth
2831 DEPTH_UNKNOWN,
2832 // A label never jumped to.
2833 DEPTH_NONLOOPING,
2834 // A label jumped to.
2835 DEPTH_LOOPING
2838 private:
2839 // The name of the label.
2840 std::string name_;
2841 // The location of the definition. This is 0 if the label has not
2842 // yet been defined.
2843 Location location_;
2844 // A snapshot of the set of bindings defined at this label, used to
2845 // issue errors about invalid goto statements.
2846 Bindings_snapshot* snapshot_;
2847 // A list of snapshots of goto statements which refer to this label.
2848 std::vector<Bindings_snapshot*> refs_;
2849 // Whether the label has been used.
2850 bool is_used_;
2851 // The backend representation.
2852 Blabel* blabel_;
2853 // The looping depth of this label, for escape analysis.
2854 Loop_depth depth_;
2857 // An unnamed label. These are used when lowering loops.
2859 class Unnamed_label
2861 public:
2862 Unnamed_label(Location location)
2863 : location_(location), derived_from_(NULL), blabel_(NULL)
2866 // Get the location where the label is defined.
2867 Location
2868 location() const
2869 { return this->location_; }
2871 // Set the location where the label is defined.
2872 void
2873 set_location(Location location)
2874 { this->location_ = location; }
2876 // Get the top level statement this unnamed label is derived from.
2877 Statement*
2878 derived_from() const
2879 { return this->derived_from_; }
2881 // Set the top level statement this unnamed label is derived from.
2882 void
2883 set_derived_from(Statement* s)
2884 { this->derived_from_ = s; }
2886 // Return a statement which defines this label.
2887 Bstatement*
2888 get_definition(Translate_context*);
2890 // Return a goto to this label from LOCATION.
2891 Bstatement*
2892 get_goto(Translate_context*, Location location);
2894 private:
2895 // Return the backend representation.
2896 Blabel*
2897 get_blabel(Translate_context*);
2899 // The location where the label is defined.
2900 Location location_;
2901 // The top-level statement this unnamed label was derived/lowered from.
2902 // This is NULL is this label is not the top-level of a lowered statement.
2903 Statement* derived_from_;
2904 // The backend representation of this label.
2905 Blabel* blabel_;
2908 // An alias for an imported package.
2910 class Package_alias
2912 public:
2913 Package_alias(Location location)
2914 : location_(location), used_(0)
2917 // The location of the import statement.
2918 Location
2919 location()
2920 { return this->location_; }
2922 // How many symbols from the package were used under this alias.
2923 size_t
2924 used() const
2925 { return this->used_; }
2927 // Note that some symbol was used under this alias.
2928 void
2929 note_usage()
2930 { this->used_++; }
2932 private:
2933 // The location of the import statement.
2934 Location location_;
2935 // The amount of times some name from this package was used under this alias.
2936 size_t used_;
2939 // An imported package.
2941 class Package
2943 public:
2944 Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
2945 Location location);
2947 // Get the package path used for all symbols exported from this
2948 // package.
2949 const std::string&
2950 pkgpath() const
2951 { return this->pkgpath_; }
2953 // Return the package path to use for a symbol name.
2954 std::string
2955 pkgpath_symbol() const;
2957 // Set the package path symbol.
2958 void
2959 set_pkgpath_symbol(const std::string&);
2961 // Return the location of the most recent import statement.
2962 Location
2963 location() const
2964 { return this->location_; }
2966 // Return whether we know the name of this package yet.
2967 bool
2968 has_package_name() const
2969 { return !this->package_name_.empty(); }
2971 // The name that this package uses in its package clause. This may
2972 // be different from the name in the associated Named_object if the
2973 // import statement used an alias.
2974 const std::string&
2975 package_name() const
2977 go_assert(!this->package_name_.empty());
2978 return this->package_name_;
2981 // Return the bindings.
2982 Bindings*
2983 bindings()
2984 { return this->bindings_; }
2986 // Type used to map import names to package aliases.
2987 typedef std::map<std::string, Package_alias*> Aliases;
2989 // Return the set of package aliases.
2990 const Aliases&
2991 aliases() const
2992 { return this->aliases_; }
2994 // Note that some symbol from this package was used and qualified by ALIAS.
2995 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
2996 void
2997 note_usage(const std::string& alias) const;
2999 // Note that USAGE might be a fake usage of this package.
3000 void
3001 note_fake_usage(Expression* usage) const
3002 { this->fake_uses_.insert(usage); }
3004 // Forget a given USAGE of this package.
3005 void
3006 forget_usage(Expression* usage) const;
3008 // Clear the used field for the next file.
3009 void
3010 clear_used();
3012 // Look up a name in the package. Returns NULL if the name is not
3013 // found.
3014 Named_object*
3015 lookup(const std::string& name) const
3016 { return this->bindings_->lookup(name); }
3018 // Set the name of the package.
3019 void
3020 set_package_name(const std::string& name, Location);
3022 // Set the location of the package. This is used to record the most
3023 // recent import location.
3024 void
3025 set_location(Location location)
3026 { this->location_ = location; }
3028 // Add a package name as an ALIAS for this package.
3029 Package_alias*
3030 add_alias(const std::string& alias, Location);
3032 // Add a constant to the package.
3033 Named_object*
3034 add_constant(const Typed_identifier& tid, Expression* expr)
3035 { return this->bindings_->add_constant(tid, this, expr, 0); }
3037 // Add a type to the package.
3038 Named_object*
3039 add_type(const std::string& name, Type* type, Location location)
3040 { return this->bindings_->add_type(name, this, type, location); }
3042 // Add a type declaration to the package.
3043 Named_object*
3044 add_type_declaration(const std::string& name, Location location)
3045 { return this->bindings_->add_type_declaration(name, this, location); }
3047 // Add a variable to the package.
3048 Named_object*
3049 add_variable(const std::string& name, Variable* variable)
3050 { return this->bindings_->add_variable(name, this, variable); }
3052 // Add a function declaration to the package.
3053 Named_object*
3054 add_function_declaration(const std::string& name, Function_type* type,
3055 Location loc)
3056 { return this->bindings_->add_function_declaration(name, this, type, loc); }
3058 // Determine types of constants.
3059 void
3060 determine_types();
3062 private:
3063 // The package path for type reflection data.
3064 std::string pkgpath_;
3065 // The package path for symbol names.
3066 std::string pkgpath_symbol_;
3067 // The name that this package uses in the package clause. This may
3068 // be the empty string if it is not yet known.
3069 std::string package_name_;
3070 // The names in this package.
3071 Bindings* bindings_;
3072 // The location of the most recent import statement.
3073 Location location_;
3074 // The set of aliases associated with this package.
3075 Aliases aliases_;
3076 // A set of possibly fake uses of this package. This is mutable because we
3077 // can track fake uses of a package even if we have a const pointer to it.
3078 mutable std::set<Expression*> fake_uses_;
3081 // Return codes for the traversal functions. This is not an enum
3082 // because we want to be able to declare traversal functions in other
3083 // header files without including this one.
3085 // Continue traversal as usual.
3086 const int TRAVERSE_CONTINUE = -1;
3088 // Exit traversal.
3089 const int TRAVERSE_EXIT = 0;
3091 // Continue traversal, but skip components of the current object.
3092 // E.g., if this is returned by Traverse::statement, we do not
3093 // traverse the expressions in the statement even if
3094 // traverse_expressions is set in the traverse_mask.
3095 const int TRAVERSE_SKIP_COMPONENTS = 1;
3097 // This class is used when traversing the parse tree. The caller uses
3098 // a subclass which overrides functions as desired.
3100 class Traverse
3102 public:
3103 // These bitmasks say what to traverse.
3104 static const unsigned int traverse_variables = 0x1;
3105 static const unsigned int traverse_constants = 0x2;
3106 static const unsigned int traverse_functions = 0x4;
3107 static const unsigned int traverse_blocks = 0x8;
3108 static const unsigned int traverse_statements = 0x10;
3109 static const unsigned int traverse_expressions = 0x20;
3110 static const unsigned int traverse_types = 0x40;
3112 Traverse(unsigned int traverse_mask)
3113 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3116 virtual ~Traverse();
3118 // The bitmask of what to traverse.
3119 unsigned int
3120 traverse_mask() const
3121 { return this->traverse_mask_; }
3123 // Record that we are going to traverse a type. This returns true
3124 // if the type has already been seen in this traversal. This is
3125 // required because types, unlike expressions, can form a circular
3126 // graph.
3127 bool
3128 remember_type(const Type*);
3130 // Record that we are going to see an expression. This returns true
3131 // if the expression has already been seen in this traversal. This
3132 // is only needed for cases where multiple expressions can point to
3133 // a single one.
3134 bool
3135 remember_expression(const Expression*);
3137 // These functions return one of the TRAVERSE codes defined above.
3139 // If traverse_variables is set in the mask, this is called for
3140 // every variable in the tree.
3141 virtual int
3142 variable(Named_object*);
3144 // If traverse_constants is set in the mask, this is called for
3145 // every named constant in the tree. The bool parameter is true for
3146 // a global constant.
3147 virtual int
3148 constant(Named_object*, bool);
3150 // If traverse_functions is set in the mask, this is called for
3151 // every function in the tree.
3152 virtual int
3153 function(Named_object*);
3155 // If traverse_blocks is set in the mask, this is called for every
3156 // block in the tree.
3157 virtual int
3158 block(Block*);
3160 // If traverse_statements is set in the mask, this is called for
3161 // every statement in the tree.
3162 virtual int
3163 statement(Block*, size_t* index, Statement*);
3165 // If traverse_expressions is set in the mask, this is called for
3166 // every expression in the tree.
3167 virtual int
3168 expression(Expression**);
3170 // If traverse_types is set in the mask, this is called for every
3171 // type in the tree.
3172 virtual int
3173 type(Type*);
3175 private:
3176 // A hash table for types we have seen during this traversal. Note
3177 // that this uses the default hash functions for pointers rather
3178 // than Type_hash_identical and Type_identical. This is because for
3179 // traversal we care about seeing a specific type structure. If
3180 // there are two separate instances of identical types, we want to
3181 // traverse both.
3182 typedef Unordered_set(const Type*) Types_seen;
3184 typedef Unordered_set(const Expression*) Expressions_seen;
3186 // Bitmask of what sort of objects to traverse.
3187 unsigned int traverse_mask_;
3188 // Types which have been seen in this traversal.
3189 Types_seen* types_seen_;
3190 // Expressions which have been seen in this traversal.
3191 Expressions_seen* expressions_seen_;
3194 // A class which makes it easier to insert new statements before the
3195 // current statement during a traversal.
3197 class Statement_inserter
3199 public:
3200 // Empty constructor.
3201 Statement_inserter()
3202 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
3205 // Constructor for a statement in a block.
3206 Statement_inserter(Block* block, size_t *pindex)
3207 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
3210 // Constructor for a global variable.
3211 Statement_inserter(Gogo* gogo, Variable* var)
3212 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
3213 { go_assert(var->is_global()); }
3215 // We use the default copy constructor and assignment operator.
3217 // Insert S before the statement we are traversing, or before the
3218 // initialization expression of a global variable.
3219 void
3220 insert(Statement* s);
3222 private:
3223 // The block that the statement is in.
3224 Block* block_;
3225 // The index of the statement that we are traversing.
3226 size_t* pindex_;
3227 // The IR, needed when looking at an initializer expression for a
3228 // global variable.
3229 Gogo* gogo_;
3230 // The global variable, when looking at an initializer expression.
3231 Variable* var_;
3234 // When translating the gogo IR into the backend data structure, this
3235 // is the context we pass down the blocks and statements.
3237 class Translate_context
3239 public:
3240 Translate_context(Gogo* gogo, Named_object* function, Block* block,
3241 Bblock* bblock)
3242 : gogo_(gogo), backend_(gogo->backend()), function_(function),
3243 block_(block), bblock_(bblock), is_const_(false)
3246 // Accessors.
3248 Gogo*
3249 gogo()
3250 { return this->gogo_; }
3252 Backend*
3253 backend()
3254 { return this->backend_; }
3256 Named_object*
3257 function()
3258 { return this->function_; }
3260 Block*
3261 block()
3262 { return this->block_; }
3264 Bblock*
3265 bblock()
3266 { return this->bblock_; }
3268 bool
3269 is_const()
3270 { return this->is_const_; }
3272 // Make a constant context.
3273 void
3274 set_is_const()
3275 { this->is_const_ = true; }
3277 private:
3278 // The IR for the entire compilation unit.
3279 Gogo* gogo_;
3280 // The generator for the backend data structures.
3281 Backend* backend_;
3282 // The function we are currently translating. NULL if not in a
3283 // function, e.g., the initializer of a global variable.
3284 Named_object* function_;
3285 // The block we are currently translating. NULL if not in a
3286 // function.
3287 Block *block_;
3288 // The backend representation of the current block. NULL if block_
3289 // is NULL.
3290 Bblock* bblock_;
3291 // Whether this is being evaluated in a constant context. This is
3292 // used for type descriptor initializers.
3293 bool is_const_;
3296 // Runtime error codes. These must match the values in
3297 // libgo/runtime/go-runtime-error.c.
3299 // Slice index out of bounds: negative or larger than the length of
3300 // the slice.
3301 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3303 // Array index out of bounds.
3304 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3306 // String index out of bounds.
3307 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3309 // Slice slice out of bounds: negative or larger than the length of
3310 // the slice or high bound less than low bound.
3311 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3313 // Array slice out of bounds.
3314 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3316 // String slice out of bounds.
3317 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3319 // Dereference of nil pointer. This is used when there is a
3320 // dereference of a pointer to a very large struct or array, to ensure
3321 // that a gigantic array is not used a proxy to access random memory
3322 // locations.
3323 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3325 // Slice length or capacity out of bounds in make: negative or
3326 // overflow or length greater than capacity.
3327 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3329 // Map capacity out of bounds in make: negative or overflow.
3330 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3332 // Channel capacity out of bounds in make: negative or overflow.
3333 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3335 // Division by zero.
3336 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3338 // This is used by some of the langhooks.
3339 extern Gogo* go_get_gogo();
3341 // Whether we have seen any errors. FIXME: Replace with a backend
3342 // interface.
3343 extern bool saw_errors();
3345 #endif // !defined(GO_GOGO_H)