* go-gcc.cc: #include "langhooks.h".
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob37cbbdf4411473f3bdd54827bc5464f0a9762a0b
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 Statement;
23 class Temporary_statement;
24 class Block;
25 class Function;
26 class Bindings;
27 class Bindings_snapshot;
28 class Package;
29 class Variable;
30 class Pointer_type;
31 class Struct_type;
32 class Struct_field;
33 class Struct_field_list;
34 class Array_type;
35 class Map_type;
36 class Channel_type;
37 class Interface_type;
38 class Named_type;
39 class Forward_declaration_type;
40 class Named_object;
41 class Label;
42 class Translate_context;
43 class Backend;
44 class Export;
45 class Import;
46 class Bexpression;
47 class Btype;
48 class Bstatement;
49 class Bblock;
50 class Bvariable;
51 class Blabel;
52 class Bfunction;
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
59 // function.
61 class Import_init
63 public:
64 Import_init(const std::string& package_name, const std::string& init_name,
65 int priority)
66 : package_name_(package_name), init_name_(init_name), priority_(priority)
67 { }
69 // The name of the package being imported.
70 const std::string&
71 package_name() const
72 { return this->package_name_; }
74 // The name of the package's init function.
75 const std::string&
76 init_name() const
77 { return this->init_name_; }
79 // The priority of the initialization function. Functions with a
80 // lower priority number must be run first.
81 int
82 priority() const
83 { return this->priority_; }
85 private:
86 // The name of the package being imported.
87 std::string package_name_;
88 // The name of the package's init function.
89 std::string init_name_;
90 // The priority.
91 int priority_;
94 // For sorting purposes.
96 inline bool
97 operator<(const Import_init& i1, const Import_init& i2)
99 if (i1.priority() < i2.priority())
100 return true;
101 if (i1.priority() > i2.priority())
102 return false;
103 if (i1.package_name() != i2.package_name())
104 return i1.package_name() < i2.package_name();
105 return i1.init_name() < i2.init_name();
108 // The holder for the internal representation of the entire
109 // compilation unit.
111 class Gogo
113 public:
114 // Create the IR, passing in the sizes of the types "int" and
115 // "uintptr" in bits.
116 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
118 // Get the backend generator.
119 Backend*
120 backend()
121 { return this->backend_; }
123 // Get the Location generator.
124 Linemap*
125 linemap()
126 { return this->linemap_; }
128 // Get the package name.
129 const std::string&
130 package_name() const;
132 // Set the package name.
133 void
134 set_package_name(const std::string&, Location);
136 // Return whether this is the "main" package.
137 bool
138 is_main_package() const;
140 // If necessary, adjust the name to use for a hidden symbol. We add
141 // the package name, so that hidden symbols in different packages do
142 // not collide.
143 std::string
144 pack_hidden_name(const std::string& name, bool is_exported) const
146 return (is_exported
147 ? name
148 : '.' + this->pkgpath() + '.' + name);
151 // Unpack a name which may have been hidden. Returns the
152 // user-visible name of the object.
153 static std::string
154 unpack_hidden_name(const std::string& name)
155 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
157 // Return whether a possibly packed name is hidden.
158 static bool
159 is_hidden_name(const std::string& name)
160 { return name[0] == '.'; }
162 // Return the package path of a hidden name.
163 static std::string
164 hidden_name_pkgpath(const std::string& name)
166 go_assert(Gogo::is_hidden_name(name));
167 return name.substr(1, name.rfind('.') - 1);
170 // Given a name which may or may not have been hidden, return the
171 // name to use in an error message.
172 static std::string
173 message_name(const std::string& name);
175 // Return whether a name is the blank identifier _.
176 static bool
177 is_sink_name(const std::string& name)
179 return (name[0] == '.'
180 && name[name.length() - 1] == '_'
181 && name[name.length() - 2] == '.');
184 // Convert a pkgpath into a string suitable for a symbol
185 static std::string
186 pkgpath_for_symbol(const std::string& pkgpath);
188 // Return the package path to use for reflect.Type.PkgPath.
189 const std::string&
190 pkgpath() const;
192 // Return the package path to use for a symbol name.
193 const std::string&
194 pkgpath_symbol() const;
196 // Set the package path from a command line option.
197 void
198 set_pkgpath(const std::string&);
200 // Set the prefix from a command line option.
201 void
202 set_prefix(const std::string&);
204 // Return whether pkgpath was set from a command line option.
205 bool
206 pkgpath_from_option() const
207 { return this->pkgpath_from_option_; }
209 // Return the relative import path as set from the command line.
210 // Returns an empty string if it was not set.
211 const std::string&
212 relative_import_path() const
213 { return this->relative_import_path_; }
215 // Set the relative import path from a command line option.
216 void
217 set_relative_import_path(const std::string& s)
218 {this->relative_import_path_ = s; }
220 // Return the priority to use for the package we are compiling.
221 // This is two more than the largest priority of any package we
222 // import.
224 package_priority() const;
226 // Import a package. FILENAME is the file name argument, LOCAL_NAME
227 // is the local name to give to the package. If LOCAL_NAME is empty
228 // the declarations are added to the global scope.
229 void
230 import_package(const std::string& filename, const std::string& local_name,
231 bool is_local_name_exported, Location);
233 // Whether we are the global binding level.
234 bool
235 in_global_scope() const;
237 // Look up a name in the current binding contours.
238 Named_object*
239 lookup(const std::string&, Named_object** pfunction) const;
241 // Look up a name in the current block.
242 Named_object*
243 lookup_in_block(const std::string&) const;
245 // Look up a name in the global namespace--the universal scope.
246 Named_object*
247 lookup_global(const char*) const;
249 // Add a new imported package. REAL_NAME is the real name of the
250 // package. ALIAS is the alias of the package; this may be the same
251 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
252 // this package should be added to the global namespace; this is
253 // true if the alias is ".". LOCATION is the location of the import
254 // statement. This returns the new package, or NULL on error.
255 Package*
256 add_imported_package(const std::string& real_name, const std::string& alias,
257 bool is_alias_exported,
258 const std::string& pkgpath,
259 Location location,
260 bool* padd_to_globals);
262 // Register a package. This package may or may not be imported.
263 // This returns the Package structure for the package, creating if
264 // it necessary.
265 Package*
266 register_package(const std::string& pkgpath, Location);
268 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
269 // method function should be added to the type of its receiver.
270 Named_object*
271 start_function(const std::string& name, Function_type* type,
272 bool add_method_to_type, Location);
274 // Finish compiling a function.
275 void
276 finish_function(Location);
278 // Return the current function.
279 Named_object*
280 current_function() const;
282 // Return the current block.
283 Block*
284 current_block();
286 // Start a new block. This is not initially associated with a
287 // function.
288 void
289 start_block(Location);
291 // Finish the current block and return it.
292 Block*
293 finish_block(Location);
295 // Declare an erroneous name. This is used to avoid knock-on errors
296 // after a parsing error.
297 Named_object*
298 add_erroneous_name(const std::string& name);
300 // Declare an unknown name. This is used while parsing. The name
301 // must be resolved by the end of the parse. Unknown names are
302 // always added at the package level.
303 Named_object*
304 add_unknown_name(const std::string& name, Location);
306 // Declare a function.
307 Named_object*
308 declare_function(const std::string&, Function_type*, Location);
310 // Declare a function at the package level. This is used for
311 // functions generated for a type.
312 Named_object*
313 declare_package_function(const std::string&, Function_type*, Location);
315 // Add a label.
316 Label*
317 add_label_definition(const std::string&, Location);
319 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
320 // report errors for a goto from the current location to the label
321 // location.
322 Label*
323 add_label_reference(const std::string&, Location,
324 bool issue_goto_errors);
326 // Return a snapshot of the current binding state.
327 Bindings_snapshot*
328 bindings_snapshot(Location);
330 // Add a statement to the current block.
331 void
332 add_statement(Statement*);
334 // Add a block to the current block.
335 void
336 add_block(Block*, Location);
338 // Add a constant.
339 Named_object*
340 add_constant(const Typed_identifier&, Expression*, int iota_value);
342 // Add a type.
343 void
344 add_type(const std::string&, Type*, Location);
346 // Add a named type. This is used for builtin types, and to add an
347 // imported type to the global scope.
348 void
349 add_named_type(Named_type*);
351 // Declare a type.
352 Named_object*
353 declare_type(const std::string&, Location);
355 // Declare a type at the package level. This is used when the
356 // parser sees an unknown name where a type name is required.
357 Named_object*
358 declare_package_type(const std::string&, Location);
360 // Define a type which was already declared.
361 void
362 define_type(Named_object*, Named_type*);
364 // Add a variable.
365 Named_object*
366 add_variable(const std::string&, Variable*);
368 // Add a sink--a reference to the blank identifier _.
369 Named_object*
370 add_sink();
372 // Add a type which needs to be verified. This is used for sink
373 // types, just to give appropriate error messages.
374 void
375 add_type_to_verify(Type* type);
377 // Add a named object to the current namespace. This is used for
378 // import . "package".
379 void
380 add_named_object(Named_object*);
382 // Add an identifier to the list of names seen in the file block.
383 void
384 add_file_block_name(const std::string& name, Location location)
385 { this->file_block_names_[name] = location; }
387 // Mark all local variables in current bindings as used. This is
388 // used when there is a parse error to avoid useless errors.
389 void
390 mark_locals_used();
392 // Return a name to use for an error case. This should only be used
393 // after reporting an error, and is used to avoid useless knockon
394 // errors.
395 static std::string
396 erroneous_name();
398 // Return whether the name indicates an error.
399 static bool
400 is_erroneous_name(const std::string&);
402 // Return a name to use for a thunk function. A thunk function is
403 // one we create during the compilation, for a go statement or a
404 // defer statement or a method expression.
405 static std::string
406 thunk_name();
408 // Return whether an object is a thunk.
409 static bool
410 is_thunk(const Named_object*);
412 // Note that we've seen an interface type. This is used to build
413 // all required interface method tables.
414 void
415 record_interface_type(Interface_type*);
417 // Note that we need an initialization function.
418 void
419 set_need_init_fn()
420 { this->need_init_fn_ = true; }
422 // Clear out all names in file scope. This is called when we start
423 // parsing a new file.
424 void
425 clear_file_scope();
427 // Record that VAR1 must be initialized after VAR2. This is used
428 // when VAR2 does not appear in VAR1's INIT or PREINIT.
429 void
430 record_var_depends_on(Variable* var1, Named_object* var2)
432 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
433 this->var_deps_[var1] = var2;
436 // Return the variable that VAR depends on, or NULL if none.
437 Named_object*
438 var_depends_on(Variable* var) const
440 Var_deps::const_iterator p = this->var_deps_.find(var);
441 return p != this->var_deps_.end() ? p->second : NULL;
444 // Queue up a type-specific function to be written out. This is
445 // used when a type-specific function is needed when not at the top
446 // level.
447 void
448 queue_specific_type_function(Type* type, Named_type* name,
449 const std::string& hash_name,
450 Function_type* hash_fntype,
451 const std::string& equal_name,
452 Function_type* equal_fntype);
454 // Write out queued specific type functions.
455 void
456 write_specific_type_functions();
458 // Whether we are done writing out specific type functions.
459 bool
460 specific_type_functions_are_written() const
461 { return this->specific_type_functions_are_written_; }
463 // Traverse the tree. See the Traverse class.
464 void
465 traverse(Traverse*);
467 // Define the predeclared global names.
468 void
469 define_global_names();
471 // Verify and complete all types.
472 void
473 verify_types();
475 // Lower the parse tree.
476 void
477 lower_parse_tree();
479 // Lower all the statements in a block.
480 void
481 lower_block(Named_object* function, Block*);
483 // Lower an expression.
484 void
485 lower_expression(Named_object* function, Statement_inserter*, Expression**);
487 // Lower a constant.
488 void
489 lower_constant(Named_object*);
491 // Flatten all the statements in a block.
492 void
493 flatten_block(Named_object* function, Block*);
495 // Flatten an expression.
496 void
497 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
499 // Create all necessary function descriptors.
500 void
501 create_function_descriptors();
503 // Finalize the method lists and build stub methods for named types.
504 void
505 finalize_methods();
507 // Work out the types to use for unspecified variables and
508 // constants.
509 void
510 determine_types();
512 // Type check the program.
513 void
514 check_types();
516 // Check the types in a single block. This is used for complicated
517 // go statements.
518 void
519 check_types_in_block(Block*);
521 // Check for return statements.
522 void
523 check_return_statements();
525 // Do all exports.
526 void
527 do_exports();
529 // Add an import control function for an imported package to the
530 // list.
531 void
532 add_import_init_fn(const std::string& package_name,
533 const std::string& init_name, int prio);
535 // Turn short-cut operators (&&, ||) into explicit if statements.
536 void
537 remove_shortcuts();
539 // Use temporary variables to force order of evaluation.
540 void
541 order_evaluations();
543 // Flatten parse tree.
544 void
545 flatten();
547 // Build thunks for functions which call recover.
548 void
549 build_recover_thunks();
551 // Simplify statements which might use thunks: go and defer
552 // statements.
553 void
554 simplify_thunk_statements();
556 // Dump AST if -fgo-dump-ast is set
557 void
558 dump_ast(const char* basename);
560 // Convert named types to the backend representation.
561 void
562 convert_named_types();
564 // Convert named types in a list of bindings.
565 void
566 convert_named_types_in_bindings(Bindings*);
568 // True if named types have been converted to the backend
569 // representation.
570 bool
571 named_types_are_converted() const
572 { return this->named_types_are_converted_; }
574 // Write out the global values.
575 void
576 write_globals();
578 // Build a call to the runtime error function.
579 Expression*
580 runtime_error(int code, Location);
582 // Build required interface method tables.
583 void
584 build_interface_method_tables();
586 // Return an expression which allocates memory to hold values of type TYPE.
587 Expression*
588 allocate_memory(Type *type, Location);
590 // Get the name of the magic initialization function.
591 const std::string&
592 get_init_fn_name();
594 private:
595 // During parsing, we keep a stack of functions. Each function on
596 // the stack is one that we are currently parsing. For each
597 // function, we keep track of the current stack of blocks.
598 struct Open_function
600 // The function.
601 Named_object* function;
602 // The stack of active blocks in the function.
603 std::vector<Block*> blocks;
606 // The stack of functions.
607 typedef std::vector<Open_function> Open_functions;
609 // Set up the built-in unsafe package.
610 void
611 import_unsafe(const std::string&, bool is_exported, Location);
613 // Return the current binding contour.
614 Bindings*
615 current_bindings();
617 const Bindings*
618 current_bindings() const;
620 // Get the decl for the magic initialization function.
621 Named_object*
622 initialization_function_decl();
624 // Create the magic initialization function.
625 Named_object*
626 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
628 // Initialize imported packages.
629 void
630 init_imports(std::vector<Bstatement*>&);
632 // Register variables with the garbage collector.
633 void
634 register_gc_vars(const std::vector<Named_object*>&,
635 std::vector<Bstatement*>&);
637 // Type used to map import names to packages.
638 typedef std::map<std::string, Package*> Imports;
640 // Type used to map package names to packages.
641 typedef std::map<std::string, Package*> Packages;
643 // Type used to map variables to the function calls that set them.
644 // This is used for initialization dependency analysis.
645 typedef std::map<Variable*, Named_object*> Var_deps;
647 // Type used to map identifiers in the file block to the location
648 // where they were defined.
649 typedef Unordered_map(std::string, Location) File_block_names;
651 // Type used to queue writing a type specific function.
652 struct Specific_type_function
654 Type* type;
655 Named_type* name;
656 std::string hash_name;
657 Function_type* hash_fntype;
658 std::string equal_name;
659 Function_type* equal_fntype;
661 Specific_type_function(Type* atype, Named_type* aname,
662 const std::string& ahash_name,
663 Function_type* ahash_fntype,
664 const std::string& aequal_name,
665 Function_type* aequal_fntype)
666 : type(atype), name(aname), hash_name(ahash_name),
667 hash_fntype(ahash_fntype), equal_name(aequal_name),
668 equal_fntype(aequal_fntype)
672 // The backend generator.
673 Backend* backend_;
674 // The object used to keep track of file names and line numbers.
675 Linemap* linemap_;
676 // The package we are compiling.
677 Package* package_;
678 // The list of currently open functions during parsing.
679 Open_functions functions_;
680 // The global binding contour. This includes the builtin functions
681 // and the package we are compiling.
682 Bindings* globals_;
683 // The list of names we have seen in the file block.
684 File_block_names file_block_names_;
685 // Mapping from import file names to packages.
686 Imports imports_;
687 // Whether the magic unsafe package was imported.
688 bool imported_unsafe_;
689 // Mapping from package names we have seen to packages. This does
690 // not include the package we are compiling.
691 Packages packages_;
692 // The functions named "init", if there are any.
693 std::vector<Named_object*> init_functions_;
694 // A mapping from variables to the function calls that initialize
695 // them, if it is not stored in the variable's init or preinit.
696 // This is used for dependency analysis.
697 Var_deps var_deps_;
698 // Whether we need a magic initialization function.
699 bool need_init_fn_;
700 // The name of the magic initialization function.
701 std::string init_fn_name_;
702 // A list of import control variables for packages that we import.
703 std::set<Import_init> imported_init_fns_;
704 // The package path used for reflection data.
705 std::string pkgpath_;
706 // The package path to use for a symbol name.
707 std::string pkgpath_symbol_;
708 // The prefix to use for symbols, from the -fgo-prefix option.
709 std::string prefix_;
710 // Whether pkgpath_ has been set.
711 bool pkgpath_set_;
712 // Whether an explicit package path was set by -fgo-pkgpath.
713 bool pkgpath_from_option_;
714 // Whether an explicit prefix was set by -fgo-prefix.
715 bool prefix_from_option_;
716 // The relative import path, from the -fgo-relative-import-path
717 // option.
718 std::string relative_import_path_;
719 // A list of types to verify.
720 std::vector<Type*> verify_types_;
721 // A list of interface types defined while parsing.
722 std::vector<Interface_type*> interface_types_;
723 // Type specific functions to write out.
724 std::vector<Specific_type_function*> specific_type_functions_;
725 // Whether we are done writing out specific type functions.
726 bool specific_type_functions_are_written_;
727 // Whether named types have been converted.
728 bool named_types_are_converted_;
731 // A block of statements.
733 class Block
735 public:
736 Block(Block* enclosing, Location);
738 // Return the enclosing block.
739 const Block*
740 enclosing() const
741 { return this->enclosing_; }
743 // Return the bindings of the block.
744 Bindings*
745 bindings()
746 { return this->bindings_; }
748 const Bindings*
749 bindings() const
750 { return this->bindings_; }
752 // Look at the block's statements.
753 const std::vector<Statement*>*
754 statements() const
755 { return &this->statements_; }
757 // Return the start location. This is normally the location of the
758 // left curly brace which starts the block.
759 Location
760 start_location() const
761 { return this->start_location_; }
763 // Return the end location. This is normally the location of the
764 // right curly brace which ends the block.
765 Location
766 end_location() const
767 { return this->end_location_; }
769 // Add a statement to the block.
770 void
771 add_statement(Statement*);
773 // Add a statement to the front of the block.
774 void
775 add_statement_at_front(Statement*);
777 // Replace a statement in a block.
778 void
779 replace_statement(size_t index, Statement*);
781 // Add a Statement before statement number INDEX.
782 void
783 insert_statement_before(size_t index, Statement*);
785 // Add a Statement after statement number INDEX.
786 void
787 insert_statement_after(size_t index, Statement*);
789 // Set the end location of the block.
790 void
791 set_end_location(Location location)
792 { this->end_location_ = location; }
794 // Traverse the tree.
796 traverse(Traverse*);
798 // Set final types for unspecified variables and constants.
799 void
800 determine_types();
802 // Return true if execution of this block may fall through to the
803 // next block.
804 bool
805 may_fall_through() const;
807 // Convert the block to the backend representation.
808 Bblock*
809 get_backend(Translate_context*);
811 // Iterate over statements.
813 typedef std::vector<Statement*>::iterator iterator;
815 iterator
816 begin()
817 { return this->statements_.begin(); }
819 iterator
820 end()
821 { return this->statements_.end(); }
823 private:
824 // Enclosing block.
825 Block* enclosing_;
826 // Statements in the block.
827 std::vector<Statement*> statements_;
828 // Binding contour.
829 Bindings* bindings_;
830 // Location of start of block.
831 Location start_location_;
832 // Location of end of block.
833 Location end_location_;
836 // A function.
838 class Function
840 public:
841 Function(Function_type* type, Function*, Block*, Location);
843 // Return the function's type.
844 Function_type*
845 type() const
846 { return this->type_; }
848 // Return the enclosing function if there is one.
849 Function*
850 enclosing()
851 { return this->enclosing_; }
853 // Set the enclosing function. This is used when building thunks
854 // for functions which call recover.
855 void
856 set_enclosing(Function* enclosing)
858 go_assert(this->enclosing_ == NULL);
859 this->enclosing_ = enclosing;
862 // The result variables.
863 typedef std::vector<Named_object*> Results;
865 // Create the result variables in the outer block.
866 void
867 create_result_variables(Gogo*);
869 // Update the named result variables when cloning a function which
870 // calls recover.
871 void
872 update_result_variables();
874 // Return the result variables.
875 Results*
876 result_variables()
877 { return this->results_; }
879 bool
880 is_sink() const
881 { return this->is_sink_; }
883 void
884 set_is_sink()
885 { this->is_sink_ = true; }
887 // Whether the result variables have names.
888 bool
889 results_are_named() const
890 { return this->results_are_named_; }
892 // Whether this method should not be included in the type
893 // descriptor.
894 bool
895 nointerface() const
897 go_assert(this->is_method());
898 return this->nointerface_;
901 // Record that this method should not be included in the type
902 // descriptor.
903 void
904 set_nointerface()
906 go_assert(this->is_method());
907 this->nointerface_ = true;
910 // Record that this function is a stub method created for an unnamed
911 // type.
912 void
913 set_is_unnamed_type_stub_method()
915 go_assert(this->is_method());
916 this->is_unnamed_type_stub_method_ = true;
919 // Add a new field to the closure variable.
920 void
921 add_closure_field(Named_object* var, Location loc)
922 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
924 // Whether this function needs a closure.
925 bool
926 needs_closure() const
927 { return !this->closure_fields_.empty(); }
929 // Return the closure variable, creating it if necessary. This is
930 // passed to the function as a static chain parameter.
931 Named_object*
932 closure_var();
934 // Set the closure variable. This is used when building thunks for
935 // functions which call recover.
936 void
937 set_closure_var(Named_object* v)
939 go_assert(this->closure_var_ == NULL);
940 this->closure_var_ = v;
943 // Return the variable for a reference to field INDEX in the closure
944 // variable.
945 Named_object*
946 enclosing_var(unsigned int index)
948 go_assert(index < this->closure_fields_.size());
949 return closure_fields_[index].first;
952 // Set the type of the closure variable if there is one.
953 void
954 set_closure_type();
956 // Get the block of statements associated with the function.
957 Block*
958 block() const
959 { return this->block_; }
961 // Get the location of the start of the function.
962 Location
963 location() const
964 { return this->location_; }
966 // Return whether this function is actually a method.
967 bool
968 is_method() const;
970 // Add a label definition to the function.
971 Label*
972 add_label_definition(Gogo*, const std::string& label_name, Location);
974 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
975 // if we should report errors for a goto from the current location
976 // to the label location.
977 Label*
978 add_label_reference(Gogo*, const std::string& label_name,
979 Location, bool issue_goto_errors);
981 // Warn about labels that are defined but not used.
982 void
983 check_labels() const;
985 // Note that a new local type has been added. Return its index.
986 unsigned int
987 new_local_type_index()
988 { return this->local_type_count_++; }
990 // Whether this function calls the predeclared recover function.
991 bool
992 calls_recover() const
993 { return this->calls_recover_; }
995 // Record that this function calls the predeclared recover function.
996 // This is set during the lowering pass.
997 void
998 set_calls_recover()
999 { this->calls_recover_ = true; }
1001 // Whether this is a recover thunk function.
1002 bool
1003 is_recover_thunk() const
1004 { return this->is_recover_thunk_; }
1006 // Record that this is a thunk built for a function which calls
1007 // recover.
1008 void
1009 set_is_recover_thunk()
1010 { this->is_recover_thunk_ = true; }
1012 // Whether this function already has a recover thunk.
1013 bool
1014 has_recover_thunk() const
1015 { return this->has_recover_thunk_; }
1017 // Record that this function already has a recover thunk.
1018 void
1019 set_has_recover_thunk()
1020 { this->has_recover_thunk_ = true; }
1022 // Mark the function as going into a unique section.
1023 void
1024 set_in_unique_section()
1025 { this->in_unique_section_ = true; }
1027 // Swap with another function. Used only for the thunk which calls
1028 // recover.
1029 void
1030 swap_for_recover(Function *);
1032 // Traverse the tree.
1034 traverse(Traverse*);
1036 // Determine types in the function.
1037 void
1038 determine_types();
1040 // Return an expression for the function descriptor, given the named
1041 // object for this function. This may only be called for functions
1042 // without a closure. This will be an immutable struct with one
1043 // field that points to the function's code.
1044 Expression*
1045 descriptor(Gogo*, Named_object*);
1047 // Set the descriptor for this function. This is used when a
1048 // function declaration is followed by a function definition.
1049 void
1050 set_descriptor(Expression* descriptor)
1052 go_assert(this->descriptor_ == NULL);
1053 this->descriptor_ = descriptor;
1056 // Return the backend representation.
1057 Bfunction*
1058 get_or_make_decl(Gogo*, Named_object*);
1060 // Return the function's decl after it has been built.
1061 Bfunction*
1062 get_decl() const;
1064 // Set the function decl to hold a backend representation of the function
1065 // code.
1066 void
1067 build(Gogo*, Named_object*);
1069 // Get the statement that assigns values to this function's result struct.
1070 Bstatement*
1071 return_value(Gogo*, Named_object*, Location) const;
1073 // Get a tree for the variable holding the defer stack.
1074 Expression*
1075 defer_stack(Location);
1077 // Export the function.
1078 void
1079 export_func(Export*, const std::string& name) const;
1081 // Export a function with a type.
1082 static void
1083 export_func_with_type(Export*, const std::string& name,
1084 const Function_type*);
1086 // Import a function.
1087 static void
1088 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1089 Typed_identifier_list** pparameters,
1090 Typed_identifier_list** presults, bool* is_varargs);
1092 private:
1093 // Type for mapping from label names to Label objects.
1094 typedef Unordered_map(std::string, Label*) Labels;
1096 void
1097 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1099 typedef std::vector<std::pair<Named_object*,
1100 Location> > Closure_fields;
1102 // The function's type.
1103 Function_type* type_;
1104 // The enclosing function. This is NULL when there isn't one, which
1105 // is the normal case.
1106 Function* enclosing_;
1107 // The result variables, if any.
1108 Results* results_;
1109 // If there is a closure, this is the list of variables which appear
1110 // in the closure. This is created by the parser, and then resolved
1111 // to a real type when we lower parse trees.
1112 Closure_fields closure_fields_;
1113 // The closure variable, passed as a parameter using the static
1114 // chain parameter. Normally NULL.
1115 Named_object* closure_var_;
1116 // The outer block of statements in the function.
1117 Block* block_;
1118 // The source location of the start of the function.
1119 Location location_;
1120 // Labels defined or referenced in the function.
1121 Labels labels_;
1122 // The number of local types defined in this function.
1123 unsigned int local_type_count_;
1124 // The function descriptor, if any.
1125 Expression* descriptor_;
1126 // The function decl.
1127 Bfunction* fndecl_;
1128 // The defer stack variable. A pointer to this variable is used to
1129 // distinguish the defer stack for one function from another. This
1130 // is NULL unless we actually need a defer stack.
1131 Temporary_statement* defer_stack_;
1132 // True if this function is sink-named. No code is generated.
1133 bool is_sink_ : 1;
1134 // True if the result variables are named.
1135 bool results_are_named_ : 1;
1136 // True if this method should not be included in the type descriptor.
1137 bool nointerface_ : 1;
1138 // True if this function is a stub method created for an unnamed
1139 // type.
1140 bool is_unnamed_type_stub_method_ : 1;
1141 // True if this function calls the predeclared recover function.
1142 bool calls_recover_ : 1;
1143 // True if this a thunk built for a function which calls recover.
1144 bool is_recover_thunk_ : 1;
1145 // True if this function already has a recover thunk.
1146 bool has_recover_thunk_ : 1;
1147 // True if this function should be put in a unique section. This is
1148 // turned on for field tracking.
1149 bool in_unique_section_ : 1;
1152 // A snapshot of the current binding state.
1154 class Bindings_snapshot
1156 public:
1157 Bindings_snapshot(const Block*, Location);
1159 // Report any errors appropriate for a goto from the current binding
1160 // state of B to this one.
1161 void
1162 check_goto_from(const Block* b, Location);
1164 // Report any errors appropriate for a goto from this binding state
1165 // to the current state of B.
1166 void
1167 check_goto_to(const Block* b);
1169 private:
1170 bool
1171 check_goto_block(Location, const Block*, const Block*, size_t*);
1173 void
1174 check_goto_defs(Location, const Block*, size_t, size_t);
1176 // The current block.
1177 const Block* block_;
1178 // The number of names currently defined in each open block.
1179 // Element 0 is this->block_, element 1 is
1180 // this->block_->enclosing(), etc.
1181 std::vector<size_t> counts_;
1182 // The location where this snapshot was taken.
1183 Location location_;
1186 // A function declaration.
1188 class Function_declaration
1190 public:
1191 Function_declaration(Function_type* fntype, Location location)
1192 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1193 fndecl_(NULL)
1196 Function_type*
1197 type() const
1198 { return this->fntype_; }
1200 Location
1201 location() const
1202 { return this->location_; }
1204 const std::string&
1205 asm_name() const
1206 { return this->asm_name_; }
1208 // Set the assembler name.
1209 void
1210 set_asm_name(const std::string& asm_name)
1211 { this->asm_name_ = asm_name; }
1213 // Return an expression for the function descriptor, given the named
1214 // object for this function. This may only be called for functions
1215 // without a closure. This will be an immutable struct with one
1216 // field that points to the function's code.
1217 Expression*
1218 descriptor(Gogo*, Named_object*);
1220 // Return true if we have created a descriptor for this declaration.
1221 bool
1222 has_descriptor() const
1223 { return this->descriptor_ != NULL; }
1225 // Return a backend representation.
1226 Bfunction*
1227 get_or_make_decl(Gogo*, Named_object*);
1229 // If there is a descriptor, build it into the backend
1230 // representation.
1231 void
1232 build_backend_descriptor(Gogo*);
1234 // Export a function declaration.
1235 void
1236 export_func(Export* exp, const std::string& name) const
1237 { Function::export_func_with_type(exp, name, this->fntype_); }
1239 private:
1240 // The type of the function.
1241 Function_type* fntype_;
1242 // The location of the declaration.
1243 Location location_;
1244 // The assembler name: this is the name to use in references to the
1245 // function. This is normally empty.
1246 std::string asm_name_;
1247 // The function descriptor, if any.
1248 Expression* descriptor_;
1249 // The function decl if needed.
1250 Bfunction* fndecl_;
1253 // A variable.
1255 class Variable
1257 public:
1258 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1259 bool is_receiver, Location);
1261 // Get the type of the variable.
1262 Type*
1263 type();
1265 Type*
1266 type() const;
1268 // Return whether the type is defined yet.
1269 bool
1270 has_type() const;
1272 // Get the initial value.
1273 Expression*
1274 init() const
1275 { return this->init_; }
1277 // Return whether there are any preinit statements.
1278 bool
1279 has_pre_init() const
1280 { return this->preinit_ != NULL; }
1282 // Return the preinit statements if any.
1283 Block*
1284 preinit() const
1285 { return this->preinit_; }
1287 // Return whether this is a global variable.
1288 bool
1289 is_global() const
1290 { return this->is_global_; }
1292 // Return whether this is a function parameter.
1293 bool
1294 is_parameter() const
1295 { return this->is_parameter_; }
1297 // Return whether this is the receiver parameter of a method.
1298 bool
1299 is_receiver() const
1300 { return this->is_receiver_; }
1302 // Change this parameter to be a receiver. This is used when
1303 // creating the thunks created for functions which call recover.
1304 void
1305 set_is_receiver()
1307 go_assert(this->is_parameter_);
1308 this->is_receiver_ = true;
1311 // Change this parameter to not be a receiver. This is used when
1312 // creating the thunks created for functions which call recover.
1313 void
1314 set_is_not_receiver()
1316 go_assert(this->is_parameter_);
1317 this->is_receiver_ = false;
1320 // Return whether this is the varargs parameter of a function.
1321 bool
1322 is_varargs_parameter() const
1323 { return this->is_varargs_parameter_; }
1325 // Whether this variable's address is taken.
1326 bool
1327 is_address_taken() const
1328 { return this->is_address_taken_; }
1330 // Whether this variable should live in the heap.
1331 bool
1332 is_in_heap() const
1333 { return this->is_address_taken_ && !this->is_global_; }
1335 // Note that something takes the address of this variable.
1336 void
1337 set_address_taken()
1338 { this->is_address_taken_ = true; }
1340 // Return whether the address is taken but does not escape.
1341 bool
1342 is_non_escaping_address_taken() const
1343 { return this->is_non_escaping_address_taken_; }
1345 // Note that something takes the address of this variable such that
1346 // the address does not escape the function.
1347 void
1348 set_non_escaping_address_taken()
1349 { this->is_non_escaping_address_taken_ = true; }
1351 // Get the source location of the variable's declaration.
1352 Location
1353 location() const
1354 { return this->location_; }
1356 // Record that this is the varargs parameter of a function.
1357 void
1358 set_is_varargs_parameter()
1360 go_assert(this->is_parameter_);
1361 this->is_varargs_parameter_ = true;
1364 // Return whether the variable has been used.
1365 bool
1366 is_used() const
1367 { return this->is_used_; }
1369 // Mark that the variable has been used.
1370 void
1371 set_is_used()
1372 { this->is_used_ = true; }
1374 // Clear the initial value; used for error handling.
1375 void
1376 clear_init()
1377 { this->init_ = NULL; }
1379 // Set the initial value; used for converting shortcuts.
1380 void
1381 set_init(Expression* init)
1382 { this->init_ = init; }
1384 // Get the preinit block, a block of statements to be run before the
1385 // initialization expression.
1386 Block*
1387 preinit_block(Gogo*);
1389 // Add a statement to be run before the initialization expression.
1390 // This is only used for global variables.
1391 void
1392 add_preinit_statement(Gogo*, Statement*);
1394 // Lower the initialization expression after parsing is complete.
1395 void
1396 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1398 // Flatten the initialization expression after ordering evaluations.
1399 void
1400 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1402 // A special case: the init value is used only to determine the
1403 // type. This is used if the variable is defined using := with the
1404 // comma-ok form of a map index or a receive expression. The init
1405 // value is actually the map index expression or receive expression.
1406 // We use this because we may not know the right type at parse time.
1407 void
1408 set_type_from_init_tuple()
1409 { this->type_from_init_tuple_ = true; }
1411 // Another special case: the init value is used only to determine
1412 // the type. This is used if the variable is defined using := with
1413 // a range clause. The init value is the range expression. The
1414 // type of the variable is the index type of the range expression
1415 // (i.e., the first value returned by a range).
1416 void
1417 set_type_from_range_index()
1418 { this->type_from_range_index_ = true; }
1420 // Another special case: like set_type_from_range_index, but the
1421 // type is the value type of the range expression (i.e., the second
1422 // value returned by a range).
1423 void
1424 set_type_from_range_value()
1425 { this->type_from_range_value_ = true; }
1427 // Another special case: the init value is used only to determine
1428 // the type. This is used if the variable is defined using := with
1429 // a case in a select statement. The init value is the channel.
1430 // The type of the variable is the channel's element type.
1431 void
1432 set_type_from_chan_element()
1433 { this->type_from_chan_element_ = true; }
1435 // After we lower the select statement, we once again set the type
1436 // from the initialization expression.
1437 void
1438 clear_type_from_chan_element()
1440 go_assert(this->type_from_chan_element_);
1441 this->type_from_chan_element_ = false;
1444 // Note that this variable was created for a type switch clause.
1445 void
1446 set_is_type_switch_var()
1447 { this->is_type_switch_var_ = true; }
1449 // Mark the variable as going into a unique section.
1450 void
1451 set_in_unique_section()
1453 go_assert(this->is_global_);
1454 this->in_unique_section_ = true;
1457 // Traverse the initializer expression.
1459 traverse_expression(Traverse*, unsigned int traverse_mask);
1461 // Determine the type of the variable if necessary.
1462 void
1463 determine_type();
1465 // Get the backend representation of the variable.
1466 Bvariable*
1467 get_backend_variable(Gogo*, Named_object*, const Package*,
1468 const std::string&);
1470 // Get the initial value of the variable. This may only
1471 // be called if has_pre_init() returns false.
1472 Bexpression*
1473 get_init(Gogo*, Named_object* function);
1475 // Return a series of statements which sets the value of the
1476 // variable in DECL. This should only be called is has_pre_init()
1477 // returns true. DECL may be NULL for a sink variable.
1478 Bstatement*
1479 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1481 // Export the variable.
1482 void
1483 export_var(Export*, const std::string& name) const;
1485 // Import a variable.
1486 static void
1487 import_var(Import*, std::string* pname, Type** ptype);
1489 private:
1490 // The type of a tuple.
1491 Type*
1492 type_from_tuple(Expression*, bool) const;
1494 // The type of a range.
1495 Type*
1496 type_from_range(Expression*, bool, bool) const;
1498 // The element type of a channel.
1499 Type*
1500 type_from_chan_element(Expression*, bool) const;
1502 // The variable's type. This may be NULL if the type is set from
1503 // the expression.
1504 Type* type_;
1505 // The initial value. This may be NULL if the variable should be
1506 // initialized to the default value for the type.
1507 Expression* init_;
1508 // Statements to run before the init statement.
1509 Block* preinit_;
1510 // Location of variable definition.
1511 Location location_;
1512 // Backend representation.
1513 Bvariable* backend_;
1514 // Whether this is a global variable.
1515 bool is_global_ : 1;
1516 // Whether this is a function parameter.
1517 bool is_parameter_ : 1;
1518 // Whether this is the receiver parameter of a method.
1519 bool is_receiver_ : 1;
1520 // Whether this is the varargs parameter of a function.
1521 bool is_varargs_parameter_ : 1;
1522 // Whether this variable is ever referenced.
1523 bool is_used_ : 1;
1524 // Whether something takes the address of this variable. For a
1525 // local variable this implies that the variable has to be on the
1526 // heap.
1527 bool is_address_taken_ : 1;
1528 // Whether something takes the address of this variable such that
1529 // the address does not escape the function.
1530 bool is_non_escaping_address_taken_ : 1;
1531 // True if we have seen this variable in a traversal.
1532 bool seen_ : 1;
1533 // True if we have lowered the initialization expression.
1534 bool init_is_lowered_ : 1;
1535 // True if we have flattened the initialization expression.
1536 bool init_is_flattened_ : 1;
1537 // True if init is a tuple used to set the type.
1538 bool type_from_init_tuple_ : 1;
1539 // True if init is a range clause and the type is the index type.
1540 bool type_from_range_index_ : 1;
1541 // True if init is a range clause and the type is the value type.
1542 bool type_from_range_value_ : 1;
1543 // True if init is a channel and the type is the channel's element type.
1544 bool type_from_chan_element_ : 1;
1545 // True if this is a variable created for a type switch case.
1546 bool is_type_switch_var_ : 1;
1547 // True if we have determined types.
1548 bool determined_type_ : 1;
1549 // True if this variable should be put in a unique section. This is
1550 // used for field tracking.
1551 bool in_unique_section_ : 1;
1554 // A variable which is really the name for a function return value, or
1555 // part of one.
1557 class Result_variable
1559 public:
1560 Result_variable(Type* type, Function* function, int index,
1561 Location location)
1562 : type_(type), function_(function), index_(index), location_(location),
1563 backend_(NULL), is_address_taken_(false),
1564 is_non_escaping_address_taken_(false)
1567 // Get the type of the result variable.
1568 Type*
1569 type() const
1570 { return this->type_; }
1572 // Get the function that this is associated with.
1573 Function*
1574 function() const
1575 { return this->function_; }
1577 // Index in the list of function results.
1579 index() const
1580 { return this->index_; }
1582 // The location of the variable definition.
1583 Location
1584 location() const
1585 { return this->location_; }
1587 // Whether this variable's address is taken.
1588 bool
1589 is_address_taken() const
1590 { return this->is_address_taken_; }
1592 // Note that something takes the address of this variable.
1593 void
1594 set_address_taken()
1595 { this->is_address_taken_ = true; }
1597 // Return whether the address is taken but does not escape.
1598 bool
1599 is_non_escaping_address_taken() const
1600 { return this->is_non_escaping_address_taken_; }
1602 // Note that something takes the address of this variable such that
1603 // the address does not escape the function.
1604 void
1605 set_non_escaping_address_taken()
1606 { this->is_non_escaping_address_taken_ = true; }
1608 // Whether this variable should live in the heap.
1609 bool
1610 is_in_heap() const
1611 { return this->is_address_taken_; }
1613 // Set the function. This is used when cloning functions which call
1614 // recover.
1615 void
1616 set_function(Function* function)
1617 { this->function_ = function; }
1619 // Get the backend representation of the variable.
1620 Bvariable*
1621 get_backend_variable(Gogo*, Named_object*, const std::string&);
1623 private:
1624 // Type of result variable.
1625 Type* type_;
1626 // Function with which this is associated.
1627 Function* function_;
1628 // Index in list of results.
1629 int index_;
1630 // Where the result variable is defined.
1631 Location location_;
1632 // Backend representation.
1633 Bvariable* backend_;
1634 // Whether something takes the address of this variable.
1635 bool is_address_taken_;
1636 // Whether something takes the address of this variable such that
1637 // the address does not escape the function.
1638 bool is_non_escaping_address_taken_;
1641 // The value we keep for a named constant. This lets us hold a type
1642 // and an expression.
1644 class Named_constant
1646 public:
1647 Named_constant(Type* type, Expression* expr, int iota_value,
1648 Location location)
1649 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1650 lowering_(false), is_sink_(false), bconst_(NULL)
1653 Type*
1654 type() const
1655 { return this->type_; }
1657 Expression*
1658 expr() const
1659 { return this->expr_; }
1662 iota_value() const
1663 { return this->iota_value_; }
1665 Location
1666 location() const
1667 { return this->location_; }
1669 // Whether we are lowering.
1670 bool
1671 lowering() const
1672 { return this->lowering_; }
1674 // Set that we are lowering.
1675 void
1676 set_lowering()
1677 { this->lowering_ = true; }
1679 // We are no longer lowering.
1680 void
1681 clear_lowering()
1682 { this->lowering_ = false; }
1684 bool
1685 is_sink() const
1686 { return this->is_sink_; }
1688 void
1689 set_is_sink()
1690 { this->is_sink_ = true; }
1692 // Traverse the expression.
1694 traverse_expression(Traverse*);
1696 // Determine the type of the constant if necessary.
1697 void
1698 determine_type();
1700 // Indicate that we found and reported an error for this constant.
1701 void
1702 set_error();
1704 // Export the constant.
1705 void
1706 export_const(Export*, const std::string& name) const;
1708 // Import a constant.
1709 static void
1710 import_const(Import*, std::string*, Type**, Expression**);
1712 // Get the backend representation of the constant value.
1713 Bexpression*
1714 get_backend(Gogo*, Named_object*);
1716 private:
1717 // The type of the constant.
1718 Type* type_;
1719 // The expression for the constant.
1720 Expression* expr_;
1721 // If the predeclared constant iota is used in EXPR_, this is the
1722 // value it will have. We do this because at parse time we don't
1723 // know whether the name "iota" will refer to the predeclared
1724 // constant or to something else. We put in the right value in when
1725 // we lower.
1726 int iota_value_;
1727 // The location of the definition.
1728 Location location_;
1729 // Whether we are currently lowering this constant.
1730 bool lowering_;
1731 // Whether this constant is blank named and needs only type checking.
1732 bool is_sink_;
1733 // The backend representation of the constant value.
1734 Bexpression* bconst_;
1737 // A type declaration.
1739 class Type_declaration
1741 public:
1742 Type_declaration(Location location)
1743 : location_(location), in_function_(NULL), in_function_index_(0),
1744 methods_(), issued_warning_(false)
1747 // Return the location.
1748 Location
1749 location() const
1750 { return this->location_; }
1752 // Return the function in which this type is declared. This will
1753 // return NULL for a type declared in global scope.
1754 Named_object*
1755 in_function(unsigned int* pindex)
1757 *pindex = this->in_function_index_;
1758 return this->in_function_;
1761 // Set the function in which this type is declared.
1762 void
1763 set_in_function(Named_object* f, unsigned int index)
1765 this->in_function_ = f;
1766 this->in_function_index_ = index;
1769 // Add a method to this type. This is used when methods are defined
1770 // before the type.
1771 Named_object*
1772 add_method(const std::string& name, Function* function);
1774 // Add a method declaration to this type.
1775 Named_object*
1776 add_method_declaration(const std::string& name, Package*,
1777 Function_type* type, Location location);
1779 // Return whether any methods were defined.
1780 bool
1781 has_methods() const;
1783 // Return the methods.
1784 const std::vector<Named_object*>*
1785 methods() const
1786 { return &this->methods_; }
1788 // Define methods when the real type is known.
1789 void
1790 define_methods(Named_type*);
1792 // This is called if we are trying to use this type. It returns
1793 // true if we should issue a warning.
1794 bool
1795 using_type();
1797 private:
1798 // The location of the type declaration.
1799 Location location_;
1800 // If this type is declared in a function, a pointer back to the
1801 // function in which it is defined.
1802 Named_object* in_function_;
1803 // The index of this type in IN_FUNCTION_.
1804 unsigned int in_function_index_;
1805 // Methods defined before the type is defined.
1806 std::vector<Named_object*> methods_;
1807 // True if we have issued a warning about a use of this type
1808 // declaration when it is undefined.
1809 bool issued_warning_;
1812 // An unknown object. These are created by the parser for forward
1813 // references to names which have not been seen before. In a correct
1814 // program, these will always point to a real definition by the end of
1815 // the parse. Because they point to another Named_object, these may
1816 // only be referenced by Unknown_expression objects.
1818 class Unknown_name
1820 public:
1821 Unknown_name(Location location)
1822 : location_(location), real_named_object_(NULL)
1825 // Return the location where this name was first seen.
1826 Location
1827 location() const
1828 { return this->location_; }
1830 // Return the real named object that this points to, or NULL if it
1831 // was never resolved.
1832 Named_object*
1833 real_named_object() const
1834 { return this->real_named_object_; }
1836 // Set the real named object that this points to.
1837 void
1838 set_real_named_object(Named_object* no);
1840 private:
1841 // The location where this name was first seen.
1842 Location location_;
1843 // The real named object when it is known.
1844 Named_object*
1845 real_named_object_;
1848 // A named object named. This is the result of a declaration. We
1849 // don't use a superclass because they all have to be handled
1850 // differently.
1852 class Named_object
1854 public:
1855 enum Classification
1857 // An uninitialized Named_object. We should never see this.
1858 NAMED_OBJECT_UNINITIALIZED,
1859 // An erroneous name. This indicates a parse error, to avoid
1860 // later errors about undefined references.
1861 NAMED_OBJECT_ERRONEOUS,
1862 // An unknown name. This is used for forward references. In a
1863 // correct program, these will all be resolved by the end of the
1864 // parse.
1865 NAMED_OBJECT_UNKNOWN,
1866 // A const.
1867 NAMED_OBJECT_CONST,
1868 // A type.
1869 NAMED_OBJECT_TYPE,
1870 // A forward type declaration.
1871 NAMED_OBJECT_TYPE_DECLARATION,
1872 // A var.
1873 NAMED_OBJECT_VAR,
1874 // A result variable in a function.
1875 NAMED_OBJECT_RESULT_VAR,
1876 // The blank identifier--the special variable named _.
1877 NAMED_OBJECT_SINK,
1878 // A func.
1879 NAMED_OBJECT_FUNC,
1880 // A forward func declaration.
1881 NAMED_OBJECT_FUNC_DECLARATION,
1882 // A package.
1883 NAMED_OBJECT_PACKAGE
1886 // Return the classification.
1887 Classification
1888 classification() const
1889 { return this->classification_; }
1891 // Classifiers.
1893 bool
1894 is_erroneous() const
1895 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1897 bool
1898 is_unknown() const
1899 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1901 bool
1902 is_const() const
1903 { return this->classification_ == NAMED_OBJECT_CONST; }
1905 bool
1906 is_type() const
1907 { return this->classification_ == NAMED_OBJECT_TYPE; }
1909 bool
1910 is_type_declaration() const
1911 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1913 bool
1914 is_variable() const
1915 { return this->classification_ == NAMED_OBJECT_VAR; }
1917 bool
1918 is_result_variable() const
1919 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1921 bool
1922 is_sink() const
1923 { return this->classification_ == NAMED_OBJECT_SINK; }
1925 bool
1926 is_function() const
1927 { return this->classification_ == NAMED_OBJECT_FUNC; }
1929 bool
1930 is_function_declaration() const
1931 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1933 bool
1934 is_package() const
1935 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1937 // Creators.
1939 static Named_object*
1940 make_erroneous_name(const std::string& name)
1941 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1943 static Named_object*
1944 make_unknown_name(const std::string& name, Location);
1946 static Named_object*
1947 make_constant(const Typed_identifier&, const Package*, Expression*,
1948 int iota_value);
1950 static Named_object*
1951 make_type(const std::string&, const Package*, Type*, Location);
1953 static Named_object*
1954 make_type_declaration(const std::string&, const Package*, Location);
1956 static Named_object*
1957 make_variable(const std::string&, const Package*, Variable*);
1959 static Named_object*
1960 make_result_variable(const std::string&, Result_variable*);
1962 static Named_object*
1963 make_sink();
1965 static Named_object*
1966 make_function(const std::string&, const Package*, Function*);
1968 static Named_object*
1969 make_function_declaration(const std::string&, const Package*, Function_type*,
1970 Location);
1972 static Named_object*
1973 make_package(const std::string& alias, Package* package);
1975 // Getters.
1977 Unknown_name*
1978 unknown_value()
1980 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1981 return this->u_.unknown_value;
1984 const Unknown_name*
1985 unknown_value() const
1987 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1988 return this->u_.unknown_value;
1991 Named_constant*
1992 const_value()
1994 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1995 return this->u_.const_value;
1998 const Named_constant*
1999 const_value() const
2001 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2002 return this->u_.const_value;
2005 Named_type*
2006 type_value()
2008 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2009 return this->u_.type_value;
2012 const Named_type*
2013 type_value() const
2015 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2016 return this->u_.type_value;
2019 Type_declaration*
2020 type_declaration_value()
2022 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2023 return this->u_.type_declaration;
2026 const Type_declaration*
2027 type_declaration_value() const
2029 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2030 return this->u_.type_declaration;
2033 Variable*
2034 var_value()
2036 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2037 return this->u_.var_value;
2040 const Variable*
2041 var_value() const
2043 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2044 return this->u_.var_value;
2047 Result_variable*
2048 result_var_value()
2050 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2051 return this->u_.result_var_value;
2054 const Result_variable*
2055 result_var_value() const
2057 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2058 return this->u_.result_var_value;
2061 Function*
2062 func_value()
2064 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2065 return this->u_.func_value;
2068 const Function*
2069 func_value() const
2071 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2072 return this->u_.func_value;
2075 Function_declaration*
2076 func_declaration_value()
2078 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2079 return this->u_.func_declaration_value;
2082 const Function_declaration*
2083 func_declaration_value() const
2085 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2086 return this->u_.func_declaration_value;
2089 Package*
2090 package_value()
2092 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2093 return this->u_.package_value;
2096 const Package*
2097 package_value() const
2099 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2100 return this->u_.package_value;
2103 const std::string&
2104 name() const
2105 { return this->name_; }
2107 // Return the name to use in an error message. The difference is
2108 // that if this Named_object is defined in a different package, this
2109 // will return PACKAGE.NAME.
2110 std::string
2111 message_name() const;
2113 const Package*
2114 package() const
2115 { return this->package_; }
2117 // Resolve an unknown value if possible. This returns the same
2118 // Named_object or a new one.
2119 Named_object*
2120 resolve()
2122 Named_object* ret = this;
2123 if (this->is_unknown())
2125 Named_object* r = this->unknown_value()->real_named_object();
2126 if (r != NULL)
2127 ret = r;
2129 return ret;
2132 const Named_object*
2133 resolve() const
2135 const Named_object* ret = this;
2136 if (this->is_unknown())
2138 const Named_object* r = this->unknown_value()->real_named_object();
2139 if (r != NULL)
2140 ret = r;
2142 return ret;
2145 // The location where this object was defined or referenced.
2146 Location
2147 location() const;
2149 // Convert a variable to the backend representation.
2150 Bvariable*
2151 get_backend_variable(Gogo*, Named_object* function);
2153 // Return the external identifier for this object.
2154 std::string
2155 get_id(Gogo*);
2157 // Get the backend representation of this object.
2158 void
2159 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2160 std::vector<Bfunction*>&);
2162 // Define a type declaration.
2163 void
2164 set_type_value(Named_type*);
2166 // Define a function declaration.
2167 void
2168 set_function_value(Function*);
2170 // Declare an unknown name as a type declaration.
2171 void
2172 declare_as_type();
2174 // Export this object.
2175 void
2176 export_named_object(Export*) const;
2178 private:
2179 Named_object(const std::string&, const Package*, Classification);
2181 // The name of the object.
2182 std::string name_;
2183 // The package that this object is in. This is NULL if it is in the
2184 // file we are compiling.
2185 const Package* package_;
2186 // The type of object this is.
2187 Classification classification_;
2188 // The real data.
2189 union
2191 Unknown_name* unknown_value;
2192 Named_constant* const_value;
2193 Named_type* type_value;
2194 Type_declaration* type_declaration;
2195 Variable* var_value;
2196 Result_variable* result_var_value;
2197 Function* func_value;
2198 Function_declaration* func_declaration_value;
2199 Package* package_value;
2200 } u_;
2203 // A binding contour. This binds names to objects.
2205 class Bindings
2207 public:
2208 // Type for mapping from names to objects.
2209 typedef Unordered_map(std::string, Named_object*) Contour;
2211 Bindings(Bindings* enclosing);
2213 // Add an erroneous name.
2214 Named_object*
2215 add_erroneous_name(const std::string& name)
2216 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2218 // Add an unknown name.
2219 Named_object*
2220 add_unknown_name(const std::string& name, Location location)
2222 return this->add_named_object(Named_object::make_unknown_name(name,
2223 location));
2226 // Add a constant.
2227 Named_object*
2228 add_constant(const Typed_identifier& tid, const Package* package,
2229 Expression* expr, int iota_value)
2231 return this->add_named_object(Named_object::make_constant(tid, package,
2232 expr,
2233 iota_value));
2236 // Add a type.
2237 Named_object*
2238 add_type(const std::string& name, const Package* package, Type* type,
2239 Location location)
2241 return this->add_named_object(Named_object::make_type(name, package, type,
2242 location));
2245 // Add a named type. This is used for builtin types, and to add an
2246 // imported type to the global scope.
2247 Named_object*
2248 add_named_type(Named_type* named_type);
2250 // Add a type declaration.
2251 Named_object*
2252 add_type_declaration(const std::string& name, const Package* package,
2253 Location location)
2255 Named_object* no = Named_object::make_type_declaration(name, package,
2256 location);
2257 return this->add_named_object(no);
2260 // Add a variable.
2261 Named_object*
2262 add_variable(const std::string& name, const Package* package,
2263 Variable* variable)
2265 return this->add_named_object(Named_object::make_variable(name, package,
2266 variable));
2269 // Add a result variable.
2270 Named_object*
2271 add_result_variable(const std::string& name, Result_variable* result)
2273 return this->add_named_object(Named_object::make_result_variable(name,
2274 result));
2277 // Add a function.
2278 Named_object*
2279 add_function(const std::string& name, const Package*, Function* function);
2281 // Add a function declaration.
2282 Named_object*
2283 add_function_declaration(const std::string& name, const Package* package,
2284 Function_type* type, Location location);
2286 // Add a package. The location is the location of the import
2287 // statement.
2288 Named_object*
2289 add_package(const std::string& alias, Package* package)
2291 Named_object* no = Named_object::make_package(alias, package);
2292 return this->add_named_object(no);
2295 // Define a type which was already declared.
2296 void
2297 define_type(Named_object*, Named_type*);
2299 // Add a method to the list of objects. This is not added to the
2300 // lookup table.
2301 void
2302 add_method(Named_object*);
2304 // Add a named object to this binding.
2305 Named_object*
2306 add_named_object(Named_object* no)
2307 { return this->add_named_object_to_contour(&this->bindings_, no); }
2309 // Clear all names in file scope from the bindings.
2310 void
2311 clear_file_scope(Gogo*);
2313 // Look up a name in this binding contour and in any enclosing
2314 // binding contours. This returns NULL if the name is not found.
2315 Named_object*
2316 lookup(const std::string&) const;
2318 // Look up a name in this binding contour without looking in any
2319 // enclosing binding contours. Returns NULL if the name is not found.
2320 Named_object*
2321 lookup_local(const std::string&) const;
2323 // Remove a name.
2324 void
2325 remove_binding(Named_object*);
2327 // Mark all variables as used. This is used for some types of parse
2328 // error.
2329 void
2330 mark_locals_used();
2332 // Traverse the tree. See the Traverse class.
2334 traverse(Traverse*, bool is_global);
2336 // Iterate over definitions. This does not include things which
2337 // were only declared.
2339 typedef std::vector<Named_object*>::const_iterator
2340 const_definitions_iterator;
2342 const_definitions_iterator
2343 begin_definitions() const
2344 { return this->named_objects_.begin(); }
2346 const_definitions_iterator
2347 end_definitions() const
2348 { return this->named_objects_.end(); }
2350 // Return the number of definitions.
2351 size_t
2352 size_definitions() const
2353 { return this->named_objects_.size(); }
2355 // Return whether there are no definitions.
2356 bool
2357 empty_definitions() const
2358 { return this->named_objects_.empty(); }
2360 // Iterate over declarations. This is everything that has been
2361 // declared, which includes everything which has been defined.
2363 typedef Contour::const_iterator const_declarations_iterator;
2365 const_declarations_iterator
2366 begin_declarations() const
2367 { return this->bindings_.begin(); }
2369 const_declarations_iterator
2370 end_declarations() const
2371 { return this->bindings_.end(); }
2373 // Return the number of declarations.
2374 size_t
2375 size_declarations() const
2376 { return this->bindings_.size(); }
2378 // Return whether there are no declarations.
2379 bool
2380 empty_declarations() const
2381 { return this->bindings_.empty(); }
2383 // Return the first declaration.
2384 Named_object*
2385 first_declaration()
2386 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2388 private:
2389 Named_object*
2390 add_named_object_to_contour(Contour*, Named_object*);
2392 Named_object*
2393 new_definition(Named_object*, Named_object*);
2395 // Enclosing bindings.
2396 Bindings* enclosing_;
2397 // The list of objects.
2398 std::vector<Named_object*> named_objects_;
2399 // The mapping from names to objects.
2400 Contour bindings_;
2403 // A label.
2405 class Label
2407 public:
2408 Label(const std::string& name)
2409 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2410 refs_(), is_used_(false), blabel_(NULL)
2413 // Return the label's name.
2414 const std::string&
2415 name() const
2416 { return this->name_; }
2418 // Return whether the label has been defined.
2419 bool
2420 is_defined() const
2421 { return !Linemap::is_unknown_location(this->location_); }
2423 // Return whether the label has been used.
2424 bool
2425 is_used() const
2426 { return this->is_used_; }
2428 // Record that the label is used.
2429 void
2430 set_is_used()
2431 { this->is_used_ = true; }
2433 // Return the location of the definition.
2434 Location
2435 location() const
2436 { return this->location_; }
2438 // Return the bindings snapshot.
2439 Bindings_snapshot*
2440 snapshot() const
2441 { return this->snapshot_; }
2443 // Add a snapshot of a goto which refers to this label.
2444 void
2445 add_snapshot_ref(Bindings_snapshot* snapshot)
2447 go_assert(Linemap::is_unknown_location(this->location_));
2448 this->refs_.push_back(snapshot);
2451 // Return the list of snapshots of goto statements which refer to
2452 // this label.
2453 const std::vector<Bindings_snapshot*>&
2454 refs() const
2455 { return this->refs_; }
2457 // Clear the references.
2458 void
2459 clear_refs();
2461 // Define the label at LOCATION with the given bindings snapshot.
2462 void
2463 define(Location location, Bindings_snapshot* snapshot)
2465 go_assert(Linemap::is_unknown_location(this->location_)
2466 && this->snapshot_ == NULL);
2467 this->location_ = location;
2468 this->snapshot_ = snapshot;
2471 // Return the backend representation for this label.
2472 Blabel*
2473 get_backend_label(Translate_context*);
2475 // Return an expression for the address of this label. This is used
2476 // to get the return address of a deferred function to see whether
2477 // the function may call recover.
2478 Bexpression*
2479 get_addr(Translate_context*, Location location);
2481 private:
2482 // The name of the label.
2483 std::string name_;
2484 // The location of the definition. This is 0 if the label has not
2485 // yet been defined.
2486 Location location_;
2487 // A snapshot of the set of bindings defined at this label, used to
2488 // issue errors about invalid goto statements.
2489 Bindings_snapshot* snapshot_;
2490 // A list of snapshots of goto statements which refer to this label.
2491 std::vector<Bindings_snapshot*> refs_;
2492 // Whether the label has been used.
2493 bool is_used_;
2494 // The backend representation.
2495 Blabel* blabel_;
2498 // An unnamed label. These are used when lowering loops.
2500 class Unnamed_label
2502 public:
2503 Unnamed_label(Location location)
2504 : location_(location), blabel_(NULL)
2507 // Get the location where the label is defined.
2508 Location
2509 location() const
2510 { return this->location_; }
2512 // Set the location where the label is defined.
2513 void
2514 set_location(Location location)
2515 { this->location_ = location; }
2517 // Return a statement which defines this label.
2518 Bstatement*
2519 get_definition(Translate_context*);
2521 // Return a goto to this label from LOCATION.
2522 Bstatement*
2523 get_goto(Translate_context*, Location location);
2525 private:
2526 // Return the backend representation.
2527 Blabel*
2528 get_blabel(Translate_context*);
2530 // The location where the label is defined.
2531 Location location_;
2532 // The backend representation of this label.
2533 Blabel* blabel_;
2536 // An imported package.
2538 class Package
2540 public:
2541 Package(const std::string& pkgpath, Location location);
2543 // Get the package path used for all symbols exported from this
2544 // package.
2545 const std::string&
2546 pkgpath() const
2547 { return this->pkgpath_; }
2549 // Return the package path to use for a symbol name.
2550 const std::string&
2551 pkgpath_symbol() const
2552 { return this->pkgpath_symbol_; }
2554 // Return the location of the import statement.
2555 Location
2556 location() const
2557 { return this->location_; }
2559 // Return whether we know the name of this package yet.
2560 bool
2561 has_package_name() const
2562 { return !this->package_name_.empty(); }
2564 // The name that this package uses in its package clause. This may
2565 // be different from the name in the associated Named_object if the
2566 // import statement used an alias.
2567 const std::string&
2568 package_name() const
2570 go_assert(!this->package_name_.empty());
2571 return this->package_name_;
2574 // The priority of this package. The init function of packages with
2575 // lower priority must be run before the init function of packages
2576 // with higher priority.
2578 priority() const
2579 { return this->priority_; }
2581 // Set the priority.
2582 void
2583 set_priority(int priority);
2585 // Return the bindings.
2586 Bindings*
2587 bindings()
2588 { return this->bindings_; }
2590 // Whether some symbol from the package was used.
2591 bool
2592 used() const
2593 { return this->used_; }
2595 // Note that some symbol from this package was used.
2596 void
2597 set_used() const
2598 { this->used_ = true; }
2600 // Clear the used field for the next file.
2601 void
2602 clear_used()
2603 { this->used_ = false; }
2605 // Whether this package was imported in the current file.
2606 bool
2607 is_imported() const
2608 { return this->is_imported_; }
2610 // Note that this package was imported in the current file.
2611 void
2612 set_is_imported()
2613 { this->is_imported_ = true; }
2615 // Clear the imported field for the next file.
2616 void
2617 clear_is_imported()
2618 { this->is_imported_ = false; }
2620 // Whether this package was imported with a name of "_".
2621 bool
2622 uses_sink_alias() const
2623 { return this->uses_sink_alias_; }
2625 // Note that this package was imported with a name of "_".
2626 void
2627 set_uses_sink_alias()
2628 { this->uses_sink_alias_ = true; }
2630 // Clear the sink alias field for the next file.
2631 void
2632 clear_uses_sink_alias()
2633 { this->uses_sink_alias_ = false; }
2635 // Look up a name in the package. Returns NULL if the name is not
2636 // found.
2637 Named_object*
2638 lookup(const std::string& name) const
2639 { return this->bindings_->lookup(name); }
2641 // Set the name of the package.
2642 void
2643 set_package_name(const std::string& name, Location);
2645 // Set the location of the package. This is used to record the most
2646 // recent import location.
2647 void
2648 set_location(Location location)
2649 { this->location_ = location; }
2651 // Add a constant to the package.
2652 Named_object*
2653 add_constant(const Typed_identifier& tid, Expression* expr)
2654 { return this->bindings_->add_constant(tid, this, expr, 0); }
2656 // Add a type to the package.
2657 Named_object*
2658 add_type(const std::string& name, Type* type, Location location)
2659 { return this->bindings_->add_type(name, this, type, location); }
2661 // Add a type declaration to the package.
2662 Named_object*
2663 add_type_declaration(const std::string& name, Location location)
2664 { return this->bindings_->add_type_declaration(name, this, location); }
2666 // Add a variable to the package.
2667 Named_object*
2668 add_variable(const std::string& name, Variable* variable)
2669 { return this->bindings_->add_variable(name, this, variable); }
2671 // Add a function declaration to the package.
2672 Named_object*
2673 add_function_declaration(const std::string& name, Function_type* type,
2674 Location loc)
2675 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2677 // Determine types of constants.
2678 void
2679 determine_types();
2681 private:
2682 // The package path for type reflection data.
2683 std::string pkgpath_;
2684 // The package path for symbol names.
2685 std::string pkgpath_symbol_;
2686 // The name that this package uses in the package clause. This may
2687 // be the empty string if it is not yet known.
2688 std::string package_name_;
2689 // The names in this package.
2690 Bindings* bindings_;
2691 // The priority of this package. A package has a priority higher
2692 // than the priority of all of the packages that it imports. This
2693 // is used to run init functions in the right order.
2694 int priority_;
2695 // The location of the import statement.
2696 Location location_;
2697 // True if some name from this package was used. This is mutable
2698 // because we can use a package even if we have a const pointer to
2699 // it.
2700 mutable bool used_;
2701 // True if this package was imported in the current file.
2702 bool is_imported_;
2703 // True if this package was imported with a name of "_".
2704 bool uses_sink_alias_;
2707 // Return codes for the traversal functions. This is not an enum
2708 // because we want to be able to declare traversal functions in other
2709 // header files without including this one.
2711 // Continue traversal as usual.
2712 const int TRAVERSE_CONTINUE = -1;
2714 // Exit traversal.
2715 const int TRAVERSE_EXIT = 0;
2717 // Continue traversal, but skip components of the current object.
2718 // E.g., if this is returned by Traverse::statement, we do not
2719 // traverse the expressions in the statement even if
2720 // traverse_expressions is set in the traverse_mask.
2721 const int TRAVERSE_SKIP_COMPONENTS = 1;
2723 // This class is used when traversing the parse tree. The caller uses
2724 // a subclass which overrides functions as desired.
2726 class Traverse
2728 public:
2729 // These bitmasks say what to traverse.
2730 static const unsigned int traverse_variables = 0x1;
2731 static const unsigned int traverse_constants = 0x2;
2732 static const unsigned int traverse_functions = 0x4;
2733 static const unsigned int traverse_blocks = 0x8;
2734 static const unsigned int traverse_statements = 0x10;
2735 static const unsigned int traverse_expressions = 0x20;
2736 static const unsigned int traverse_types = 0x40;
2738 Traverse(unsigned int traverse_mask)
2739 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2742 virtual ~Traverse();
2744 // The bitmask of what to traverse.
2745 unsigned int
2746 traverse_mask() const
2747 { return this->traverse_mask_; }
2749 // Record that we are going to traverse a type. This returns true
2750 // if the type has already been seen in this traversal. This is
2751 // required because types, unlike expressions, can form a circular
2752 // graph.
2753 bool
2754 remember_type(const Type*);
2756 // Record that we are going to see an expression. This returns true
2757 // if the expression has already been seen in this traversal. This
2758 // is only needed for cases where multiple expressions can point to
2759 // a single one.
2760 bool
2761 remember_expression(const Expression*);
2763 // These functions return one of the TRAVERSE codes defined above.
2765 // If traverse_variables is set in the mask, this is called for
2766 // every variable in the tree.
2767 virtual int
2768 variable(Named_object*);
2770 // If traverse_constants is set in the mask, this is called for
2771 // every named constant in the tree. The bool parameter is true for
2772 // a global constant.
2773 virtual int
2774 constant(Named_object*, bool);
2776 // If traverse_functions is set in the mask, this is called for
2777 // every function in the tree.
2778 virtual int
2779 function(Named_object*);
2781 // If traverse_blocks is set in the mask, this is called for every
2782 // block in the tree.
2783 virtual int
2784 block(Block*);
2786 // If traverse_statements is set in the mask, this is called for
2787 // every statement in the tree.
2788 virtual int
2789 statement(Block*, size_t* index, Statement*);
2791 // If traverse_expressions is set in the mask, this is called for
2792 // every expression in the tree.
2793 virtual int
2794 expression(Expression**);
2796 // If traverse_types is set in the mask, this is called for every
2797 // type in the tree.
2798 virtual int
2799 type(Type*);
2801 private:
2802 // A hash table for types we have seen during this traversal. Note
2803 // that this uses the default hash functions for pointers rather
2804 // than Type_hash_identical and Type_identical. This is because for
2805 // traversal we care about seeing a specific type structure. If
2806 // there are two separate instances of identical types, we want to
2807 // traverse both.
2808 typedef Unordered_set(const Type*) Types_seen;
2810 typedef Unordered_set(const Expression*) Expressions_seen;
2812 // Bitmask of what sort of objects to traverse.
2813 unsigned int traverse_mask_;
2814 // Types which have been seen in this traversal.
2815 Types_seen* types_seen_;
2816 // Expressions which have been seen in this traversal.
2817 Expressions_seen* expressions_seen_;
2820 // A class which makes it easier to insert new statements before the
2821 // current statement during a traversal.
2823 class Statement_inserter
2825 public:
2826 // Empty constructor.
2827 Statement_inserter()
2828 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2831 // Constructor for a statement in a block.
2832 Statement_inserter(Block* block, size_t *pindex)
2833 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2836 // Constructor for a global variable.
2837 Statement_inserter(Gogo* gogo, Variable* var)
2838 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2839 { go_assert(var->is_global()); }
2841 // We use the default copy constructor and assignment operator.
2843 // Insert S before the statement we are traversing, or before the
2844 // initialization expression of a global variable.
2845 void
2846 insert(Statement* s);
2848 private:
2849 // The block that the statement is in.
2850 Block* block_;
2851 // The index of the statement that we are traversing.
2852 size_t* pindex_;
2853 // The IR, needed when looking at an initializer expression for a
2854 // global variable.
2855 Gogo* gogo_;
2856 // The global variable, when looking at an initializer expression.
2857 Variable* var_;
2860 // When translating the gogo IR into the backend data structure, this
2861 // is the context we pass down the blocks and statements.
2863 class Translate_context
2865 public:
2866 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2867 Bblock* bblock)
2868 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2869 block_(block), bblock_(bblock), is_const_(false)
2872 // Accessors.
2874 Gogo*
2875 gogo()
2876 { return this->gogo_; }
2878 Backend*
2879 backend()
2880 { return this->backend_; }
2882 Named_object*
2883 function()
2884 { return this->function_; }
2886 Block*
2887 block()
2888 { return this->block_; }
2890 Bblock*
2891 bblock()
2892 { return this->bblock_; }
2894 bool
2895 is_const()
2896 { return this->is_const_; }
2898 // Make a constant context.
2899 void
2900 set_is_const()
2901 { this->is_const_ = true; }
2903 private:
2904 // The IR for the entire compilation unit.
2905 Gogo* gogo_;
2906 // The generator for the backend data structures.
2907 Backend* backend_;
2908 // The function we are currently translating. NULL if not in a
2909 // function, e.g., the initializer of a global variable.
2910 Named_object* function_;
2911 // The block we are currently translating. NULL if not in a
2912 // function.
2913 Block *block_;
2914 // The backend representation of the current block. NULL if block_
2915 // is NULL.
2916 Bblock* bblock_;
2917 // Whether this is being evaluated in a constant context. This is
2918 // used for type descriptor initializers.
2919 bool is_const_;
2922 // Runtime error codes. These must match the values in
2923 // libgo/runtime/go-runtime-error.c.
2925 // Slice index out of bounds: negative or larger than the length of
2926 // the slice.
2927 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2929 // Array index out of bounds.
2930 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2932 // String index out of bounds.
2933 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2935 // Slice slice out of bounds: negative or larger than the length of
2936 // the slice or high bound less than low bound.
2937 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2939 // Array slice out of bounds.
2940 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2942 // String slice out of bounds.
2943 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2945 // Dereference of nil pointer. This is used when there is a
2946 // dereference of a pointer to a very large struct or array, to ensure
2947 // that a gigantic array is not used a proxy to access random memory
2948 // locations.
2949 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2951 // Slice length or capacity out of bounds in make: negative or
2952 // overflow or length greater than capacity.
2953 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2955 // Map capacity out of bounds in make: negative or overflow.
2956 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2958 // Channel capacity out of bounds in make: negative or overflow.
2959 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2961 // Division by zero.
2962 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2964 // This is used by some of the langhooks.
2965 extern Gogo* go_get_gogo();
2967 // Whether we have seen any errors. FIXME: Replace with a backend
2968 // interface.
2969 extern bool saw_errors();
2971 #endif // !defined(GO_GOGO_H)