Rebase.
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob01390ac28d20beef2c8136feb5ecc59e314b9897
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 whether to check for division by zero in binary operations.
221 bool
222 check_divide_by_zero() const
223 { return this->check_divide_by_zero_; }
225 // Set the option to check division by zero from a command line option.
226 void
227 set_check_divide_by_zero(bool b)
228 { this->check_divide_by_zero_ = b; }
230 // Return whether to check for division overflow in binary operations.
231 bool
232 check_divide_overflow() const
233 { return this->check_divide_overflow_; }
235 // Set the option to check division overflow from a command line option.
236 void
237 set_check_divide_overflow(bool b)
238 { this->check_divide_overflow_ = b; }
240 // Return the priority to use for the package we are compiling.
241 // This is two more than the largest priority of any package we
242 // import.
244 package_priority() const;
246 // Import a package. FILENAME is the file name argument, LOCAL_NAME
247 // is the local name to give to the package. If LOCAL_NAME is empty
248 // the declarations are added to the global scope.
249 void
250 import_package(const std::string& filename, const std::string& local_name,
251 bool is_local_name_exported, Location);
253 // Whether we are the global binding level.
254 bool
255 in_global_scope() const;
257 // Look up a name in the current binding contours.
258 Named_object*
259 lookup(const std::string&, Named_object** pfunction) const;
261 // Look up a name in the current block.
262 Named_object*
263 lookup_in_block(const std::string&) const;
265 // Look up a name in the global namespace--the universal scope.
266 Named_object*
267 lookup_global(const char*) const;
269 // Add a new imported package. REAL_NAME is the real name of the
270 // package. ALIAS is the alias of the package; this may be the same
271 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
272 // this package should be added to the global namespace; this is
273 // true if the alias is ".". LOCATION is the location of the import
274 // statement. This returns the new package, or NULL on error.
275 Package*
276 add_imported_package(const std::string& real_name, const std::string& alias,
277 bool is_alias_exported,
278 const std::string& pkgpath,
279 Location location,
280 bool* padd_to_globals);
282 // Register a package. This package may or may not be imported.
283 // This returns the Package structure for the package, creating if
284 // it necessary.
285 Package*
286 register_package(const std::string& pkgpath, Location);
288 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
289 // method function should be added to the type of its receiver.
290 Named_object*
291 start_function(const std::string& name, Function_type* type,
292 bool add_method_to_type, Location);
294 // Finish compiling a function.
295 void
296 finish_function(Location);
298 // Return the current function.
299 Named_object*
300 current_function() const;
302 // Return the current block.
303 Block*
304 current_block();
306 // Start a new block. This is not initially associated with a
307 // function.
308 void
309 start_block(Location);
311 // Finish the current block and return it.
312 Block*
313 finish_block(Location);
315 // Declare an erroneous name. This is used to avoid knock-on errors
316 // after a parsing error.
317 Named_object*
318 add_erroneous_name(const std::string& name);
320 // Declare an unknown name. This is used while parsing. The name
321 // must be resolved by the end of the parse. Unknown names are
322 // always added at the package level.
323 Named_object*
324 add_unknown_name(const std::string& name, Location);
326 // Declare a function.
327 Named_object*
328 declare_function(const std::string&, Function_type*, Location);
330 // Declare a function at the package level. This is used for
331 // functions generated for a type.
332 Named_object*
333 declare_package_function(const std::string&, Function_type*, Location);
335 // Add a label.
336 Label*
337 add_label_definition(const std::string&, Location);
339 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
340 // report errors for a goto from the current location to the label
341 // location.
342 Label*
343 add_label_reference(const std::string&, Location,
344 bool issue_goto_errors);
346 // Return a snapshot of the current binding state.
347 Bindings_snapshot*
348 bindings_snapshot(Location);
350 // Add a statement to the current block.
351 void
352 add_statement(Statement*);
354 // Add a block to the current block.
355 void
356 add_block(Block*, Location);
358 // Add a constant.
359 Named_object*
360 add_constant(const Typed_identifier&, Expression*, int iota_value);
362 // Add a type.
363 void
364 add_type(const std::string&, Type*, Location);
366 // Add a named type. This is used for builtin types, and to add an
367 // imported type to the global scope.
368 void
369 add_named_type(Named_type*);
371 // Declare a type.
372 Named_object*
373 declare_type(const std::string&, Location);
375 // Declare a type at the package level. This is used when the
376 // parser sees an unknown name where a type name is required.
377 Named_object*
378 declare_package_type(const std::string&, Location);
380 // Define a type which was already declared.
381 void
382 define_type(Named_object*, Named_type*);
384 // Add a variable.
385 Named_object*
386 add_variable(const std::string&, Variable*);
388 // Add a sink--a reference to the blank identifier _.
389 Named_object*
390 add_sink();
392 // Add a type which needs to be verified. This is used for sink
393 // types, just to give appropriate error messages.
394 void
395 add_type_to_verify(Type* type);
397 // Add a named object to the current namespace. This is used for
398 // import . "package".
399 void
400 add_dot_import_object(Named_object*);
402 // Add an identifier to the list of names seen in the file block.
403 void
404 add_file_block_name(const std::string& name, Location location)
405 { this->file_block_names_[name] = location; }
407 // Mark all local variables in current bindings as used. This is
408 // used when there is a parse error to avoid useless errors.
409 void
410 mark_locals_used();
412 // Return a name to use for an error case. This should only be used
413 // after reporting an error, and is used to avoid useless knockon
414 // errors.
415 static std::string
416 erroneous_name();
418 // Return whether the name indicates an error.
419 static bool
420 is_erroneous_name(const std::string&);
422 // Return a name to use for a thunk function. A thunk function is
423 // one we create during the compilation, for a go statement or a
424 // defer statement or a method expression.
425 static std::string
426 thunk_name();
428 // Return whether an object is a thunk.
429 static bool
430 is_thunk(const Named_object*);
432 // Note that we've seen an interface type. This is used to build
433 // all required interface method tables.
434 void
435 record_interface_type(Interface_type*);
437 // Note that we need an initialization function.
438 void
439 set_need_init_fn()
440 { this->need_init_fn_ = true; }
442 // Clear out all names in file scope. This is called when we start
443 // parsing a new file.
444 void
445 clear_file_scope();
447 // Record that VAR1 must be initialized after VAR2. This is used
448 // when VAR2 does not appear in VAR1's INIT or PREINIT.
449 void
450 record_var_depends_on(Variable* var1, Named_object* var2)
452 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
453 this->var_deps_[var1] = var2;
456 // Return the variable that VAR depends on, or NULL if none.
457 Named_object*
458 var_depends_on(Variable* var) const
460 Var_deps::const_iterator p = this->var_deps_.find(var);
461 return p != this->var_deps_.end() ? p->second : NULL;
464 // Queue up a type-specific function to be written out. This is
465 // used when a type-specific function is needed when not at the top
466 // level.
467 void
468 queue_specific_type_function(Type* type, Named_type* name,
469 const std::string& hash_name,
470 Function_type* hash_fntype,
471 const std::string& equal_name,
472 Function_type* equal_fntype);
474 // Write out queued specific type functions.
475 void
476 write_specific_type_functions();
478 // Whether we are done writing out specific type functions.
479 bool
480 specific_type_functions_are_written() const
481 { return this->specific_type_functions_are_written_; }
483 // Traverse the tree. See the Traverse class.
484 void
485 traverse(Traverse*);
487 // Define the predeclared global names.
488 void
489 define_global_names();
491 // Verify and complete all types.
492 void
493 verify_types();
495 // Lower the parse tree.
496 void
497 lower_parse_tree();
499 // Lower all the statements in a block.
500 void
501 lower_block(Named_object* function, Block*);
503 // Lower an expression.
504 void
505 lower_expression(Named_object* function, Statement_inserter*, Expression**);
507 // Lower a constant.
508 void
509 lower_constant(Named_object*);
511 // Flatten all the statements in a block.
512 void
513 flatten_block(Named_object* function, Block*);
515 // Flatten an expression.
516 void
517 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
519 // Create all necessary function descriptors.
520 void
521 create_function_descriptors();
523 // Finalize the method lists and build stub methods for named types.
524 void
525 finalize_methods();
527 // Work out the types to use for unspecified variables and
528 // constants.
529 void
530 determine_types();
532 // Type check the program.
533 void
534 check_types();
536 // Check the types in a single block. This is used for complicated
537 // go statements.
538 void
539 check_types_in_block(Block*);
541 // Check for return statements.
542 void
543 check_return_statements();
545 // Do all exports.
546 void
547 do_exports();
549 // Add an import control function for an imported package to the
550 // list.
551 void
552 add_import_init_fn(const std::string& package_name,
553 const std::string& init_name, int prio);
555 // Turn short-cut operators (&&, ||) into explicit if statements.
556 void
557 remove_shortcuts();
559 // Use temporary variables to force order of evaluation.
560 void
561 order_evaluations();
563 // Flatten parse tree.
564 void
565 flatten();
567 // Build thunks for functions which call recover.
568 void
569 build_recover_thunks();
571 // Simplify statements which might use thunks: go and defer
572 // statements.
573 void
574 simplify_thunk_statements();
576 // Dump AST if -fgo-dump-ast is set
577 void
578 dump_ast(const char* basename);
580 // Convert named types to the backend representation.
581 void
582 convert_named_types();
584 // Convert named types in a list of bindings.
585 void
586 convert_named_types_in_bindings(Bindings*);
588 // True if named types have been converted to the backend
589 // representation.
590 bool
591 named_types_are_converted() const
592 { return this->named_types_are_converted_; }
594 // Return the variable to use for the zero value of TYPE. All types
595 // shared the same zero value, and we make sure that it is large
596 // enough.
597 Named_object*
598 zero_value(Type *type);
600 // Return whether a variable is the zero value variable.
601 bool
602 is_zero_value(Variable* v) const;
604 // Create the zero value variable.
605 Bvariable*
606 backend_zero_value();
608 // Write out the global values.
609 void
610 write_globals();
612 // Build a call to the runtime error function.
613 Expression*
614 runtime_error(int code, Location);
616 // Build required interface method tables.
617 void
618 build_interface_method_tables();
620 // Return an expression which allocates memory to hold values of type TYPE.
621 Expression*
622 allocate_memory(Type *type, Location);
624 // Get the name of the magic initialization function.
625 const std::string&
626 get_init_fn_name();
628 private:
629 // During parsing, we keep a stack of functions. Each function on
630 // the stack is one that we are currently parsing. For each
631 // function, we keep track of the current stack of blocks.
632 struct Open_function
634 // The function.
635 Named_object* function;
636 // The stack of active blocks in the function.
637 std::vector<Block*> blocks;
640 // The stack of functions.
641 typedef std::vector<Open_function> Open_functions;
643 // Set up the built-in unsafe package.
644 void
645 import_unsafe(const std::string&, bool is_exported, Location);
647 // Return the current binding contour.
648 Bindings*
649 current_bindings();
651 const Bindings*
652 current_bindings() const;
654 // Get the decl for the magic initialization function.
655 Named_object*
656 initialization_function_decl();
658 // Create the magic initialization function.
659 Named_object*
660 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
662 // Initialize imported packages.
663 void
664 init_imports(std::vector<Bstatement*>&);
666 // Register variables with the garbage collector.
667 void
668 register_gc_vars(const std::vector<Named_object*>&,
669 std::vector<Bstatement*>&);
671 // Type used to map import names to packages.
672 typedef std::map<std::string, Package*> Imports;
674 // Type used to map package names to packages.
675 typedef std::map<std::string, Package*> Packages;
677 // Type used to map variables to the function calls that set them.
678 // This is used for initialization dependency analysis.
679 typedef std::map<Variable*, Named_object*> Var_deps;
681 // Type used to map identifiers in the file block to the location
682 // where they were defined.
683 typedef Unordered_map(std::string, Location) File_block_names;
685 // Type used to queue writing a type specific function.
686 struct Specific_type_function
688 Type* type;
689 Named_type* name;
690 std::string hash_name;
691 Function_type* hash_fntype;
692 std::string equal_name;
693 Function_type* equal_fntype;
695 Specific_type_function(Type* atype, Named_type* aname,
696 const std::string& ahash_name,
697 Function_type* ahash_fntype,
698 const std::string& aequal_name,
699 Function_type* aequal_fntype)
700 : type(atype), name(aname), hash_name(ahash_name),
701 hash_fntype(ahash_fntype), equal_name(aequal_name),
702 equal_fntype(aequal_fntype)
706 // The backend generator.
707 Backend* backend_;
708 // The object used to keep track of file names and line numbers.
709 Linemap* linemap_;
710 // The package we are compiling.
711 Package* package_;
712 // The list of currently open functions during parsing.
713 Open_functions functions_;
714 // The global binding contour. This includes the builtin functions
715 // and the package we are compiling.
716 Bindings* globals_;
717 // The list of names we have seen in the file block.
718 File_block_names file_block_names_;
719 // Mapping from import file names to packages.
720 Imports imports_;
721 // Whether the magic unsafe package was imported.
722 bool imported_unsafe_;
723 // Mapping from package names we have seen to packages. This does
724 // not include the package we are compiling.
725 Packages packages_;
726 // The functions named "init", if there are any.
727 std::vector<Named_object*> init_functions_;
728 // A mapping from variables to the function calls that initialize
729 // them, if it is not stored in the variable's init or preinit.
730 // This is used for dependency analysis.
731 Var_deps var_deps_;
732 // Whether we need a magic initialization function.
733 bool need_init_fn_;
734 // The name of the magic initialization function.
735 std::string init_fn_name_;
736 // A list of import control variables for packages that we import.
737 std::set<Import_init> imported_init_fns_;
738 // The package path used for reflection data.
739 std::string pkgpath_;
740 // The package path to use for a symbol name.
741 std::string pkgpath_symbol_;
742 // The prefix to use for symbols, from the -fgo-prefix option.
743 std::string prefix_;
744 // The special zero value variable.
745 Named_object* zero_value_;
746 // The size of the zero value variable.
747 unsigned long zero_value_size_;
748 // The alignment of the zero value variable, in bytes.
749 unsigned long zero_value_align_;
750 // Whether pkgpath_ has been set.
751 bool pkgpath_set_;
752 // Whether an explicit package path was set by -fgo-pkgpath.
753 bool pkgpath_from_option_;
754 // Whether an explicit prefix was set by -fgo-prefix.
755 bool prefix_from_option_;
756 // The relative import path, from the -fgo-relative-import-path
757 // option.
758 std::string relative_import_path_;
759 // Whether or not to check for division by zero, from the
760 // -fgo-check-divide-zero option.
761 bool check_divide_by_zero_;
762 // Whether or not to check for division overflow, from the
763 // -fgo-check-divide-overflow option.
764 bool check_divide_overflow_;
765 // A list of types to verify.
766 std::vector<Type*> verify_types_;
767 // A list of interface types defined while parsing.
768 std::vector<Interface_type*> interface_types_;
769 // Type specific functions to write out.
770 std::vector<Specific_type_function*> specific_type_functions_;
771 // Whether we are done writing out specific type functions.
772 bool specific_type_functions_are_written_;
773 // Whether named types have been converted.
774 bool named_types_are_converted_;
777 // A block of statements.
779 class Block
781 public:
782 Block(Block* enclosing, Location);
784 // Return the enclosing block.
785 const Block*
786 enclosing() const
787 { return this->enclosing_; }
789 // Return the bindings of the block.
790 Bindings*
791 bindings()
792 { return this->bindings_; }
794 const Bindings*
795 bindings() const
796 { return this->bindings_; }
798 // Look at the block's statements.
799 const std::vector<Statement*>*
800 statements() const
801 { return &this->statements_; }
803 // Return the start location. This is normally the location of the
804 // left curly brace which starts the block.
805 Location
806 start_location() const
807 { return this->start_location_; }
809 // Return the end location. This is normally the location of the
810 // right curly brace which ends the block.
811 Location
812 end_location() const
813 { return this->end_location_; }
815 // Add a statement to the block.
816 void
817 add_statement(Statement*);
819 // Add a statement to the front of the block.
820 void
821 add_statement_at_front(Statement*);
823 // Replace a statement in a block.
824 void
825 replace_statement(size_t index, Statement*);
827 // Add a Statement before statement number INDEX.
828 void
829 insert_statement_before(size_t index, Statement*);
831 // Add a Statement after statement number INDEX.
832 void
833 insert_statement_after(size_t index, Statement*);
835 // Set the end location of the block.
836 void
837 set_end_location(Location location)
838 { this->end_location_ = location; }
840 // Traverse the tree.
842 traverse(Traverse*);
844 // Set final types for unspecified variables and constants.
845 void
846 determine_types();
848 // Return true if execution of this block may fall through to the
849 // next block.
850 bool
851 may_fall_through() const;
853 // Convert the block to the backend representation.
854 Bblock*
855 get_backend(Translate_context*);
857 // Iterate over statements.
859 typedef std::vector<Statement*>::iterator iterator;
861 iterator
862 begin()
863 { return this->statements_.begin(); }
865 iterator
866 end()
867 { return this->statements_.end(); }
869 private:
870 // Enclosing block.
871 Block* enclosing_;
872 // Statements in the block.
873 std::vector<Statement*> statements_;
874 // Binding contour.
875 Bindings* bindings_;
876 // Location of start of block.
877 Location start_location_;
878 // Location of end of block.
879 Location end_location_;
882 // A function.
884 class Function
886 public:
887 Function(Function_type* type, Function*, Block*, Location);
889 // Return the function's type.
890 Function_type*
891 type() const
892 { return this->type_; }
894 // Return the enclosing function if there is one.
895 Function*
896 enclosing()
897 { return this->enclosing_; }
899 // Set the enclosing function. This is used when building thunks
900 // for functions which call recover.
901 void
902 set_enclosing(Function* enclosing)
904 go_assert(this->enclosing_ == NULL);
905 this->enclosing_ = enclosing;
908 // The result variables.
909 typedef std::vector<Named_object*> Results;
911 // Create the result variables in the outer block.
912 void
913 create_result_variables(Gogo*);
915 // Update the named result variables when cloning a function which
916 // calls recover.
917 void
918 update_result_variables();
920 // Return the result variables.
921 Results*
922 result_variables()
923 { return this->results_; }
925 bool
926 is_sink() const
927 { return this->is_sink_; }
929 void
930 set_is_sink()
931 { this->is_sink_ = true; }
933 // Whether the result variables have names.
934 bool
935 results_are_named() const
936 { return this->results_are_named_; }
938 // Whether this method should not be included in the type
939 // descriptor.
940 bool
941 nointerface() const
943 go_assert(this->is_method());
944 return this->nointerface_;
947 // Record that this method should not be included in the type
948 // descriptor.
949 void
950 set_nointerface()
952 go_assert(this->is_method());
953 this->nointerface_ = true;
956 // Record that this function is a stub method created for an unnamed
957 // type.
958 void
959 set_is_unnamed_type_stub_method()
961 go_assert(this->is_method());
962 this->is_unnamed_type_stub_method_ = true;
965 // Add a new field to the closure variable.
966 void
967 add_closure_field(Named_object* var, Location loc)
968 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
970 // Whether this function needs a closure.
971 bool
972 needs_closure() const
973 { return !this->closure_fields_.empty(); }
975 // Return the closure variable, creating it if necessary. This is
976 // passed to the function as a static chain parameter.
977 Named_object*
978 closure_var();
980 // Set the closure variable. This is used when building thunks for
981 // functions which call recover.
982 void
983 set_closure_var(Named_object* v)
985 go_assert(this->closure_var_ == NULL);
986 this->closure_var_ = v;
989 // Return the variable for a reference to field INDEX in the closure
990 // variable.
991 Named_object*
992 enclosing_var(unsigned int index)
994 go_assert(index < this->closure_fields_.size());
995 return closure_fields_[index].first;
998 // Set the type of the closure variable if there is one.
999 void
1000 set_closure_type();
1002 // Get the block of statements associated with the function.
1003 Block*
1004 block() const
1005 { return this->block_; }
1007 // Get the location of the start of the function.
1008 Location
1009 location() const
1010 { return this->location_; }
1012 // Return whether this function is actually a method.
1013 bool
1014 is_method() const;
1016 // Add a label definition to the function.
1017 Label*
1018 add_label_definition(Gogo*, const std::string& label_name, Location);
1020 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1021 // if we should report errors for a goto from the current location
1022 // to the label location.
1023 Label*
1024 add_label_reference(Gogo*, const std::string& label_name,
1025 Location, bool issue_goto_errors);
1027 // Warn about labels that are defined but not used.
1028 void
1029 check_labels() const;
1031 // Note that a new local type has been added. Return its index.
1032 unsigned int
1033 new_local_type_index()
1034 { return this->local_type_count_++; }
1036 // Whether this function calls the predeclared recover function.
1037 bool
1038 calls_recover() const
1039 { return this->calls_recover_; }
1041 // Record that this function calls the predeclared recover function.
1042 // This is set during the lowering pass.
1043 void
1044 set_calls_recover()
1045 { this->calls_recover_ = true; }
1047 // Whether this is a recover thunk function.
1048 bool
1049 is_recover_thunk() const
1050 { return this->is_recover_thunk_; }
1052 // Record that this is a thunk built for a function which calls
1053 // recover.
1054 void
1055 set_is_recover_thunk()
1056 { this->is_recover_thunk_ = true; }
1058 // Whether this function already has a recover thunk.
1059 bool
1060 has_recover_thunk() const
1061 { return this->has_recover_thunk_; }
1063 // Record that this function already has a recover thunk.
1064 void
1065 set_has_recover_thunk()
1066 { this->has_recover_thunk_ = true; }
1068 // Mark the function as going into a unique section.
1069 void
1070 set_in_unique_section()
1071 { this->in_unique_section_ = true; }
1073 // Swap with another function. Used only for the thunk which calls
1074 // recover.
1075 void
1076 swap_for_recover(Function *);
1078 // Traverse the tree.
1080 traverse(Traverse*);
1082 // Determine types in the function.
1083 void
1084 determine_types();
1086 // Return an expression for the function descriptor, given the named
1087 // object for this function. This may only be called for functions
1088 // without a closure. This will be an immutable struct with one
1089 // field that points to the function's code.
1090 Expression*
1091 descriptor(Gogo*, Named_object*);
1093 // Set the descriptor for this function. This is used when a
1094 // function declaration is followed by a function definition.
1095 void
1096 set_descriptor(Expression* descriptor)
1098 go_assert(this->descriptor_ == NULL);
1099 this->descriptor_ = descriptor;
1102 // Return the backend representation.
1103 Bfunction*
1104 get_or_make_decl(Gogo*, Named_object*);
1106 // Return the function's decl after it has been built.
1107 Bfunction*
1108 get_decl() const;
1110 // Set the function decl to hold a backend representation of the function
1111 // code.
1112 void
1113 build(Gogo*, Named_object*);
1115 // Get the statement that assigns values to this function's result struct.
1116 Bstatement*
1117 return_value(Gogo*, Named_object*, Location) const;
1119 // Get an expression for the variable holding the defer stack.
1120 Expression*
1121 defer_stack(Location);
1123 // Export the function.
1124 void
1125 export_func(Export*, const std::string& name) const;
1127 // Export a function with a type.
1128 static void
1129 export_func_with_type(Export*, const std::string& name,
1130 const Function_type*);
1132 // Import a function.
1133 static void
1134 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1135 Typed_identifier_list** pparameters,
1136 Typed_identifier_list** presults, bool* is_varargs);
1138 private:
1139 // Type for mapping from label names to Label objects.
1140 typedef Unordered_map(std::string, Label*) Labels;
1142 void
1143 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1145 typedef std::vector<std::pair<Named_object*,
1146 Location> > Closure_fields;
1148 // The function's type.
1149 Function_type* type_;
1150 // The enclosing function. This is NULL when there isn't one, which
1151 // is the normal case.
1152 Function* enclosing_;
1153 // The result variables, if any.
1154 Results* results_;
1155 // If there is a closure, this is the list of variables which appear
1156 // in the closure. This is created by the parser, and then resolved
1157 // to a real type when we lower parse trees.
1158 Closure_fields closure_fields_;
1159 // The closure variable, passed as a parameter using the static
1160 // chain parameter. Normally NULL.
1161 Named_object* closure_var_;
1162 // The outer block of statements in the function.
1163 Block* block_;
1164 // The source location of the start of the function.
1165 Location location_;
1166 // Labels defined or referenced in the function.
1167 Labels labels_;
1168 // The number of local types defined in this function.
1169 unsigned int local_type_count_;
1170 // The function descriptor, if any.
1171 Expression* descriptor_;
1172 // The function decl.
1173 Bfunction* fndecl_;
1174 // The defer stack variable. A pointer to this variable is used to
1175 // distinguish the defer stack for one function from another. This
1176 // is NULL unless we actually need a defer stack.
1177 Temporary_statement* defer_stack_;
1178 // True if this function is sink-named. No code is generated.
1179 bool is_sink_ : 1;
1180 // True if the result variables are named.
1181 bool results_are_named_ : 1;
1182 // True if this method should not be included in the type descriptor.
1183 bool nointerface_ : 1;
1184 // True if this function is a stub method created for an unnamed
1185 // type.
1186 bool is_unnamed_type_stub_method_ : 1;
1187 // True if this function calls the predeclared recover function.
1188 bool calls_recover_ : 1;
1189 // True if this a thunk built for a function which calls recover.
1190 bool is_recover_thunk_ : 1;
1191 // True if this function already has a recover thunk.
1192 bool has_recover_thunk_ : 1;
1193 // True if this function should be put in a unique section. This is
1194 // turned on for field tracking.
1195 bool in_unique_section_ : 1;
1198 // A snapshot of the current binding state.
1200 class Bindings_snapshot
1202 public:
1203 Bindings_snapshot(const Block*, Location);
1205 // Report any errors appropriate for a goto from the current binding
1206 // state of B to this one.
1207 void
1208 check_goto_from(const Block* b, Location);
1210 // Report any errors appropriate for a goto from this binding state
1211 // to the current state of B.
1212 void
1213 check_goto_to(const Block* b);
1215 private:
1216 bool
1217 check_goto_block(Location, const Block*, const Block*, size_t*);
1219 void
1220 check_goto_defs(Location, const Block*, size_t, size_t);
1222 // The current block.
1223 const Block* block_;
1224 // The number of names currently defined in each open block.
1225 // Element 0 is this->block_, element 1 is
1226 // this->block_->enclosing(), etc.
1227 std::vector<size_t> counts_;
1228 // The location where this snapshot was taken.
1229 Location location_;
1232 // A function declaration.
1234 class Function_declaration
1236 public:
1237 Function_declaration(Function_type* fntype, Location location)
1238 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1239 fndecl_(NULL)
1242 Function_type*
1243 type() const
1244 { return this->fntype_; }
1246 Location
1247 location() const
1248 { return this->location_; }
1250 const std::string&
1251 asm_name() const
1252 { return this->asm_name_; }
1254 // Set the assembler name.
1255 void
1256 set_asm_name(const std::string& asm_name)
1257 { this->asm_name_ = asm_name; }
1259 // Return an expression for the function descriptor, given the named
1260 // object for this function. This may only be called for functions
1261 // without a closure. This will be an immutable struct with one
1262 // field that points to the function's code.
1263 Expression*
1264 descriptor(Gogo*, Named_object*);
1266 // Return true if we have created a descriptor for this declaration.
1267 bool
1268 has_descriptor() const
1269 { return this->descriptor_ != NULL; }
1271 // Return a backend representation.
1272 Bfunction*
1273 get_or_make_decl(Gogo*, Named_object*);
1275 // If there is a descriptor, build it into the backend
1276 // representation.
1277 void
1278 build_backend_descriptor(Gogo*);
1280 // Export a function declaration.
1281 void
1282 export_func(Export* exp, const std::string& name) const
1283 { Function::export_func_with_type(exp, name, this->fntype_); }
1285 private:
1286 // The type of the function.
1287 Function_type* fntype_;
1288 // The location of the declaration.
1289 Location location_;
1290 // The assembler name: this is the name to use in references to the
1291 // function. This is normally empty.
1292 std::string asm_name_;
1293 // The function descriptor, if any.
1294 Expression* descriptor_;
1295 // The function decl if needed.
1296 Bfunction* fndecl_;
1299 // A variable.
1301 class Variable
1303 public:
1304 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1305 bool is_receiver, Location);
1307 // Get the type of the variable.
1308 Type*
1309 type();
1311 Type*
1312 type() const;
1314 // Return whether the type is defined yet.
1315 bool
1316 has_type() const;
1318 // Get the initial value.
1319 Expression*
1320 init() const
1321 { return this->init_; }
1323 // Return whether there are any preinit statements.
1324 bool
1325 has_pre_init() const
1326 { return this->preinit_ != NULL; }
1328 // Return the preinit statements if any.
1329 Block*
1330 preinit() const
1331 { return this->preinit_; }
1333 // Return whether this is a global variable.
1334 bool
1335 is_global() const
1336 { return this->is_global_; }
1338 // Return whether this is a function parameter.
1339 bool
1340 is_parameter() const
1341 { return this->is_parameter_; }
1343 // Return whether this is the receiver parameter of a method.
1344 bool
1345 is_receiver() const
1346 { return this->is_receiver_; }
1348 // Change this parameter to be a receiver. This is used when
1349 // creating the thunks created for functions which call recover.
1350 void
1351 set_is_receiver()
1353 go_assert(this->is_parameter_);
1354 this->is_receiver_ = true;
1357 // Change this parameter to not be a receiver. This is used when
1358 // creating the thunks created for functions which call recover.
1359 void
1360 set_is_not_receiver()
1362 go_assert(this->is_parameter_);
1363 this->is_receiver_ = false;
1366 // Return whether this is the varargs parameter of a function.
1367 bool
1368 is_varargs_parameter() const
1369 { return this->is_varargs_parameter_; }
1371 // Whether this variable's address is taken.
1372 bool
1373 is_address_taken() const
1374 { return this->is_address_taken_; }
1376 // Whether this variable should live in the heap.
1377 bool
1378 is_in_heap() const
1379 { return this->is_address_taken_ && !this->is_global_; }
1381 // Note that something takes the address of this variable.
1382 void
1383 set_address_taken()
1384 { this->is_address_taken_ = true; }
1386 // Return whether the address is taken but does not escape.
1387 bool
1388 is_non_escaping_address_taken() const
1389 { return this->is_non_escaping_address_taken_; }
1391 // Note that something takes the address of this variable such that
1392 // the address does not escape the function.
1393 void
1394 set_non_escaping_address_taken()
1395 { this->is_non_escaping_address_taken_ = true; }
1397 // Get the source location of the variable's declaration.
1398 Location
1399 location() const
1400 { return this->location_; }
1402 // Record that this is the varargs parameter of a function.
1403 void
1404 set_is_varargs_parameter()
1406 go_assert(this->is_parameter_);
1407 this->is_varargs_parameter_ = true;
1410 // Return whether the variable has been used.
1411 bool
1412 is_used() const
1413 { return this->is_used_; }
1415 // Mark that the variable has been used.
1416 void
1417 set_is_used()
1418 { this->is_used_ = true; }
1420 // Clear the initial value; used for error handling.
1421 void
1422 clear_init()
1423 { this->init_ = NULL; }
1425 // Set the initial value; used for converting shortcuts.
1426 void
1427 set_init(Expression* init)
1428 { this->init_ = init; }
1430 // Get the preinit block, a block of statements to be run before the
1431 // initialization expression.
1432 Block*
1433 preinit_block(Gogo*);
1435 // Add a statement to be run before the initialization expression.
1436 // This is only used for global variables.
1437 void
1438 add_preinit_statement(Gogo*, Statement*);
1440 // Lower the initialization expression after parsing is complete.
1441 void
1442 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1444 // Flatten the initialization expression after ordering evaluations.
1445 void
1446 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1448 // A special case: the init value is used only to determine the
1449 // type. This is used if the variable is defined using := with the
1450 // comma-ok form of a map index or a receive expression. The init
1451 // value is actually the map index expression or receive expression.
1452 // We use this because we may not know the right type at parse time.
1453 void
1454 set_type_from_init_tuple()
1455 { this->type_from_init_tuple_ = true; }
1457 // Another special case: the init value is used only to determine
1458 // the type. This is used if the variable is defined using := with
1459 // a range clause. The init value is the range expression. The
1460 // type of the variable is the index type of the range expression
1461 // (i.e., the first value returned by a range).
1462 void
1463 set_type_from_range_index()
1464 { this->type_from_range_index_ = true; }
1466 // Another special case: like set_type_from_range_index, but the
1467 // type is the value type of the range expression (i.e., the second
1468 // value returned by a range).
1469 void
1470 set_type_from_range_value()
1471 { this->type_from_range_value_ = true; }
1473 // Another special case: the init value is used only to determine
1474 // the type. This is used if the variable is defined using := with
1475 // a case in a select statement. The init value is the channel.
1476 // The type of the variable is the channel's element type.
1477 void
1478 set_type_from_chan_element()
1479 { this->type_from_chan_element_ = true; }
1481 // After we lower the select statement, we once again set the type
1482 // from the initialization expression.
1483 void
1484 clear_type_from_chan_element()
1486 go_assert(this->type_from_chan_element_);
1487 this->type_from_chan_element_ = false;
1490 // Note that this variable was created for a type switch clause.
1491 void
1492 set_is_type_switch_var()
1493 { this->is_type_switch_var_ = true; }
1495 // Mark the variable as going into a unique section.
1496 void
1497 set_in_unique_section()
1499 go_assert(this->is_global_);
1500 this->in_unique_section_ = true;
1503 // Traverse the initializer expression.
1505 traverse_expression(Traverse*, unsigned int traverse_mask);
1507 // Determine the type of the variable if necessary.
1508 void
1509 determine_type();
1511 // Get the backend representation of the variable.
1512 Bvariable*
1513 get_backend_variable(Gogo*, Named_object*, const Package*,
1514 const std::string&);
1516 // Get the initial value of the variable. This may only
1517 // be called if has_pre_init() returns false.
1518 Bexpression*
1519 get_init(Gogo*, Named_object* function);
1521 // Return a series of statements which sets the value of the
1522 // variable in DECL. This should only be called is has_pre_init()
1523 // returns true. DECL may be NULL for a sink variable.
1524 Bstatement*
1525 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1527 // Export the variable.
1528 void
1529 export_var(Export*, const std::string& name) const;
1531 // Import a variable.
1532 static void
1533 import_var(Import*, std::string* pname, Type** ptype);
1535 private:
1536 // The type of a tuple.
1537 Type*
1538 type_from_tuple(Expression*, bool) const;
1540 // The type of a range.
1541 Type*
1542 type_from_range(Expression*, bool, bool) const;
1544 // The element type of a channel.
1545 Type*
1546 type_from_chan_element(Expression*, bool) const;
1548 // The variable's type. This may be NULL if the type is set from
1549 // the expression.
1550 Type* type_;
1551 // The initial value. This may be NULL if the variable should be
1552 // initialized to the default value for the type.
1553 Expression* init_;
1554 // Statements to run before the init statement.
1555 Block* preinit_;
1556 // Location of variable definition.
1557 Location location_;
1558 // Backend representation.
1559 Bvariable* backend_;
1560 // Whether this is a global variable.
1561 bool is_global_ : 1;
1562 // Whether this is a function parameter.
1563 bool is_parameter_ : 1;
1564 // Whether this is the receiver parameter of a method.
1565 bool is_receiver_ : 1;
1566 // Whether this is the varargs parameter of a function.
1567 bool is_varargs_parameter_ : 1;
1568 // Whether this variable is ever referenced.
1569 bool is_used_ : 1;
1570 // Whether something takes the address of this variable. For a
1571 // local variable this implies that the variable has to be on the
1572 // heap.
1573 bool is_address_taken_ : 1;
1574 // Whether something takes the address of this variable such that
1575 // the address does not escape the function.
1576 bool is_non_escaping_address_taken_ : 1;
1577 // True if we have seen this variable in a traversal.
1578 bool seen_ : 1;
1579 // True if we have lowered the initialization expression.
1580 bool init_is_lowered_ : 1;
1581 // True if we have flattened the initialization expression.
1582 bool init_is_flattened_ : 1;
1583 // True if init is a tuple used to set the type.
1584 bool type_from_init_tuple_ : 1;
1585 // True if init is a range clause and the type is the index type.
1586 bool type_from_range_index_ : 1;
1587 // True if init is a range clause and the type is the value type.
1588 bool type_from_range_value_ : 1;
1589 // True if init is a channel and the type is the channel's element type.
1590 bool type_from_chan_element_ : 1;
1591 // True if this is a variable created for a type switch case.
1592 bool is_type_switch_var_ : 1;
1593 // True if we have determined types.
1594 bool determined_type_ : 1;
1595 // True if this variable should be put in a unique section. This is
1596 // used for field tracking.
1597 bool in_unique_section_ : 1;
1600 // A variable which is really the name for a function return value, or
1601 // part of one.
1603 class Result_variable
1605 public:
1606 Result_variable(Type* type, Function* function, int index,
1607 Location location)
1608 : type_(type), function_(function), index_(index), location_(location),
1609 backend_(NULL), is_address_taken_(false),
1610 is_non_escaping_address_taken_(false)
1613 // Get the type of the result variable.
1614 Type*
1615 type() const
1616 { return this->type_; }
1618 // Get the function that this is associated with.
1619 Function*
1620 function() const
1621 { return this->function_; }
1623 // Index in the list of function results.
1625 index() const
1626 { return this->index_; }
1628 // The location of the variable definition.
1629 Location
1630 location() const
1631 { return this->location_; }
1633 // Whether this variable's address is taken.
1634 bool
1635 is_address_taken() const
1636 { return this->is_address_taken_; }
1638 // Note that something takes the address of this variable.
1639 void
1640 set_address_taken()
1641 { this->is_address_taken_ = true; }
1643 // Return whether the address is taken but does not escape.
1644 bool
1645 is_non_escaping_address_taken() const
1646 { return this->is_non_escaping_address_taken_; }
1648 // Note that something takes the address of this variable such that
1649 // the address does not escape the function.
1650 void
1651 set_non_escaping_address_taken()
1652 { this->is_non_escaping_address_taken_ = true; }
1654 // Whether this variable should live in the heap.
1655 bool
1656 is_in_heap() const
1657 { return this->is_address_taken_; }
1659 // Set the function. This is used when cloning functions which call
1660 // recover.
1661 void
1662 set_function(Function* function)
1663 { this->function_ = function; }
1665 // Get the backend representation of the variable.
1666 Bvariable*
1667 get_backend_variable(Gogo*, Named_object*, const std::string&);
1669 private:
1670 // Type of result variable.
1671 Type* type_;
1672 // Function with which this is associated.
1673 Function* function_;
1674 // Index in list of results.
1675 int index_;
1676 // Where the result variable is defined.
1677 Location location_;
1678 // Backend representation.
1679 Bvariable* backend_;
1680 // Whether something takes the address of this variable.
1681 bool is_address_taken_;
1682 // Whether something takes the address of this variable such that
1683 // the address does not escape the function.
1684 bool is_non_escaping_address_taken_;
1687 // The value we keep for a named constant. This lets us hold a type
1688 // and an expression.
1690 class Named_constant
1692 public:
1693 Named_constant(Type* type, Expression* expr, int iota_value,
1694 Location location)
1695 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1696 lowering_(false), is_sink_(false), bconst_(NULL)
1699 Type*
1700 type() const
1701 { return this->type_; }
1703 Expression*
1704 expr() const
1705 { return this->expr_; }
1708 iota_value() const
1709 { return this->iota_value_; }
1711 Location
1712 location() const
1713 { return this->location_; }
1715 // Whether we are lowering.
1716 bool
1717 lowering() const
1718 { return this->lowering_; }
1720 // Set that we are lowering.
1721 void
1722 set_lowering()
1723 { this->lowering_ = true; }
1725 // We are no longer lowering.
1726 void
1727 clear_lowering()
1728 { this->lowering_ = false; }
1730 bool
1731 is_sink() const
1732 { return this->is_sink_; }
1734 void
1735 set_is_sink()
1736 { this->is_sink_ = true; }
1738 // Traverse the expression.
1740 traverse_expression(Traverse*);
1742 // Determine the type of the constant if necessary.
1743 void
1744 determine_type();
1746 // Indicate that we found and reported an error for this constant.
1747 void
1748 set_error();
1750 // Export the constant.
1751 void
1752 export_const(Export*, const std::string& name) const;
1754 // Import a constant.
1755 static void
1756 import_const(Import*, std::string*, Type**, Expression**);
1758 // Get the backend representation of the constant value.
1759 Bexpression*
1760 get_backend(Gogo*, Named_object*);
1762 private:
1763 // The type of the constant.
1764 Type* type_;
1765 // The expression for the constant.
1766 Expression* expr_;
1767 // If the predeclared constant iota is used in EXPR_, this is the
1768 // value it will have. We do this because at parse time we don't
1769 // know whether the name "iota" will refer to the predeclared
1770 // constant or to something else. We put in the right value in when
1771 // we lower.
1772 int iota_value_;
1773 // The location of the definition.
1774 Location location_;
1775 // Whether we are currently lowering this constant.
1776 bool lowering_;
1777 // Whether this constant is blank named and needs only type checking.
1778 bool is_sink_;
1779 // The backend representation of the constant value.
1780 Bexpression* bconst_;
1783 // A type declaration.
1785 class Type_declaration
1787 public:
1788 Type_declaration(Location location)
1789 : location_(location), in_function_(NULL), in_function_index_(0),
1790 methods_(), issued_warning_(false)
1793 // Return the location.
1794 Location
1795 location() const
1796 { return this->location_; }
1798 // Return the function in which this type is declared. This will
1799 // return NULL for a type declared in global scope.
1800 Named_object*
1801 in_function(unsigned int* pindex)
1803 *pindex = this->in_function_index_;
1804 return this->in_function_;
1807 // Set the function in which this type is declared.
1808 void
1809 set_in_function(Named_object* f, unsigned int index)
1811 this->in_function_ = f;
1812 this->in_function_index_ = index;
1815 // Add a method to this type. This is used when methods are defined
1816 // before the type.
1817 Named_object*
1818 add_method(const std::string& name, Function* function);
1820 // Add a method declaration to this type.
1821 Named_object*
1822 add_method_declaration(const std::string& name, Package*,
1823 Function_type* type, Location location);
1825 // Return whether any methods were defined.
1826 bool
1827 has_methods() const;
1829 // Return the methods.
1830 const std::vector<Named_object*>*
1831 methods() const
1832 { return &this->methods_; }
1834 // Define methods when the real type is known.
1835 void
1836 define_methods(Named_type*);
1838 // This is called if we are trying to use this type. It returns
1839 // true if we should issue a warning.
1840 bool
1841 using_type();
1843 private:
1844 // The location of the type declaration.
1845 Location location_;
1846 // If this type is declared in a function, a pointer back to the
1847 // function in which it is defined.
1848 Named_object* in_function_;
1849 // The index of this type in IN_FUNCTION_.
1850 unsigned int in_function_index_;
1851 // Methods defined before the type is defined.
1852 std::vector<Named_object*> methods_;
1853 // True if we have issued a warning about a use of this type
1854 // declaration when it is undefined.
1855 bool issued_warning_;
1858 // An unknown object. These are created by the parser for forward
1859 // references to names which have not been seen before. In a correct
1860 // program, these will always point to a real definition by the end of
1861 // the parse. Because they point to another Named_object, these may
1862 // only be referenced by Unknown_expression objects.
1864 class Unknown_name
1866 public:
1867 Unknown_name(Location location)
1868 : location_(location), real_named_object_(NULL)
1871 // Return the location where this name was first seen.
1872 Location
1873 location() const
1874 { return this->location_; }
1876 // Return the real named object that this points to, or NULL if it
1877 // was never resolved.
1878 Named_object*
1879 real_named_object() const
1880 { return this->real_named_object_; }
1882 // Set the real named object that this points to.
1883 void
1884 set_real_named_object(Named_object* no);
1886 private:
1887 // The location where this name was first seen.
1888 Location location_;
1889 // The real named object when it is known.
1890 Named_object*
1891 real_named_object_;
1894 // A named object named. This is the result of a declaration. We
1895 // don't use a superclass because they all have to be handled
1896 // differently.
1898 class Named_object
1900 public:
1901 enum Classification
1903 // An uninitialized Named_object. We should never see this.
1904 NAMED_OBJECT_UNINITIALIZED,
1905 // An erroneous name. This indicates a parse error, to avoid
1906 // later errors about undefined references.
1907 NAMED_OBJECT_ERRONEOUS,
1908 // An unknown name. This is used for forward references. In a
1909 // correct program, these will all be resolved by the end of the
1910 // parse.
1911 NAMED_OBJECT_UNKNOWN,
1912 // A const.
1913 NAMED_OBJECT_CONST,
1914 // A type.
1915 NAMED_OBJECT_TYPE,
1916 // A forward type declaration.
1917 NAMED_OBJECT_TYPE_DECLARATION,
1918 // A var.
1919 NAMED_OBJECT_VAR,
1920 // A result variable in a function.
1921 NAMED_OBJECT_RESULT_VAR,
1922 // The blank identifier--the special variable named _.
1923 NAMED_OBJECT_SINK,
1924 // A func.
1925 NAMED_OBJECT_FUNC,
1926 // A forward func declaration.
1927 NAMED_OBJECT_FUNC_DECLARATION,
1928 // A package.
1929 NAMED_OBJECT_PACKAGE
1932 // Return the classification.
1933 Classification
1934 classification() const
1935 { return this->classification_; }
1937 // Classifiers.
1939 bool
1940 is_erroneous() const
1941 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1943 bool
1944 is_unknown() const
1945 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1947 bool
1948 is_const() const
1949 { return this->classification_ == NAMED_OBJECT_CONST; }
1951 bool
1952 is_type() const
1953 { return this->classification_ == NAMED_OBJECT_TYPE; }
1955 bool
1956 is_type_declaration() const
1957 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1959 bool
1960 is_variable() const
1961 { return this->classification_ == NAMED_OBJECT_VAR; }
1963 bool
1964 is_result_variable() const
1965 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1967 bool
1968 is_sink() const
1969 { return this->classification_ == NAMED_OBJECT_SINK; }
1971 bool
1972 is_function() const
1973 { return this->classification_ == NAMED_OBJECT_FUNC; }
1975 bool
1976 is_function_declaration() const
1977 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1979 bool
1980 is_package() const
1981 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1983 // Creators.
1985 static Named_object*
1986 make_erroneous_name(const std::string& name)
1987 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1989 static Named_object*
1990 make_unknown_name(const std::string& name, Location);
1992 static Named_object*
1993 make_constant(const Typed_identifier&, const Package*, Expression*,
1994 int iota_value);
1996 static Named_object*
1997 make_type(const std::string&, const Package*, Type*, Location);
1999 static Named_object*
2000 make_type_declaration(const std::string&, const Package*, Location);
2002 static Named_object*
2003 make_variable(const std::string&, const Package*, Variable*);
2005 static Named_object*
2006 make_result_variable(const std::string&, Result_variable*);
2008 static Named_object*
2009 make_sink();
2011 static Named_object*
2012 make_function(const std::string&, const Package*, Function*);
2014 static Named_object*
2015 make_function_declaration(const std::string&, const Package*, Function_type*,
2016 Location);
2018 static Named_object*
2019 make_package(const std::string& alias, Package* package);
2021 // Getters.
2023 Unknown_name*
2024 unknown_value()
2026 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2027 return this->u_.unknown_value;
2030 const Unknown_name*
2031 unknown_value() const
2033 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2034 return this->u_.unknown_value;
2037 Named_constant*
2038 const_value()
2040 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2041 return this->u_.const_value;
2044 const Named_constant*
2045 const_value() const
2047 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2048 return this->u_.const_value;
2051 Named_type*
2052 type_value()
2054 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2055 return this->u_.type_value;
2058 const Named_type*
2059 type_value() const
2061 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2062 return this->u_.type_value;
2065 Type_declaration*
2066 type_declaration_value()
2068 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2069 return this->u_.type_declaration;
2072 const Type_declaration*
2073 type_declaration_value() const
2075 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2076 return this->u_.type_declaration;
2079 Variable*
2080 var_value()
2082 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2083 return this->u_.var_value;
2086 const Variable*
2087 var_value() const
2089 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2090 return this->u_.var_value;
2093 Result_variable*
2094 result_var_value()
2096 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2097 return this->u_.result_var_value;
2100 const Result_variable*
2101 result_var_value() const
2103 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2104 return this->u_.result_var_value;
2107 Function*
2108 func_value()
2110 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2111 return this->u_.func_value;
2114 const Function*
2115 func_value() const
2117 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2118 return this->u_.func_value;
2121 Function_declaration*
2122 func_declaration_value()
2124 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2125 return this->u_.func_declaration_value;
2128 const Function_declaration*
2129 func_declaration_value() const
2131 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2132 return this->u_.func_declaration_value;
2135 Package*
2136 package_value()
2138 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2139 return this->u_.package_value;
2142 const Package*
2143 package_value() const
2145 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2146 return this->u_.package_value;
2149 const std::string&
2150 name() const
2151 { return this->name_; }
2153 // Return the name to use in an error message. The difference is
2154 // that if this Named_object is defined in a different package, this
2155 // will return PACKAGE.NAME.
2156 std::string
2157 message_name() const;
2159 const Package*
2160 package() const
2161 { return this->package_; }
2163 // Resolve an unknown value if possible. This returns the same
2164 // Named_object or a new one.
2165 Named_object*
2166 resolve()
2168 Named_object* ret = this;
2169 if (this->is_unknown())
2171 Named_object* r = this->unknown_value()->real_named_object();
2172 if (r != NULL)
2173 ret = r;
2175 return ret;
2178 const Named_object*
2179 resolve() const
2181 const Named_object* ret = this;
2182 if (this->is_unknown())
2184 const Named_object* r = this->unknown_value()->real_named_object();
2185 if (r != NULL)
2186 ret = r;
2188 return ret;
2191 // The location where this object was defined or referenced.
2192 Location
2193 location() const;
2195 // Convert a variable to the backend representation.
2196 Bvariable*
2197 get_backend_variable(Gogo*, Named_object* function);
2199 // Return the external identifier for this object.
2200 std::string
2201 get_id(Gogo*);
2203 // Get the backend representation of this object.
2204 void
2205 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2206 std::vector<Bfunction*>&);
2208 // Define a type declaration.
2209 void
2210 set_type_value(Named_type*);
2212 // Define a function declaration.
2213 void
2214 set_function_value(Function*);
2216 // Declare an unknown name as a type declaration.
2217 void
2218 declare_as_type();
2220 // Export this object.
2221 void
2222 export_named_object(Export*) const;
2224 private:
2225 Named_object(const std::string&, const Package*, Classification);
2227 // The name of the object.
2228 std::string name_;
2229 // The package that this object is in. This is NULL if it is in the
2230 // file we are compiling.
2231 const Package* package_;
2232 // The type of object this is.
2233 Classification classification_;
2234 // The real data.
2235 union
2237 Unknown_name* unknown_value;
2238 Named_constant* const_value;
2239 Named_type* type_value;
2240 Type_declaration* type_declaration;
2241 Variable* var_value;
2242 Result_variable* result_var_value;
2243 Function* func_value;
2244 Function_declaration* func_declaration_value;
2245 Package* package_value;
2246 } u_;
2249 // A binding contour. This binds names to objects.
2251 class Bindings
2253 public:
2254 // Type for mapping from names to objects.
2255 typedef Unordered_map(std::string, Named_object*) Contour;
2257 Bindings(Bindings* enclosing);
2259 // Add an erroneous name.
2260 Named_object*
2261 add_erroneous_name(const std::string& name)
2262 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2264 // Add an unknown name.
2265 Named_object*
2266 add_unknown_name(const std::string& name, Location location)
2268 return this->add_named_object(Named_object::make_unknown_name(name,
2269 location));
2272 // Add a constant.
2273 Named_object*
2274 add_constant(const Typed_identifier& tid, const Package* package,
2275 Expression* expr, int iota_value)
2277 return this->add_named_object(Named_object::make_constant(tid, package,
2278 expr,
2279 iota_value));
2282 // Add a type.
2283 Named_object*
2284 add_type(const std::string& name, const Package* package, Type* type,
2285 Location location)
2287 return this->add_named_object(Named_object::make_type(name, package, type,
2288 location));
2291 // Add a named type. This is used for builtin types, and to add an
2292 // imported type to the global scope.
2293 Named_object*
2294 add_named_type(Named_type* named_type);
2296 // Add a type declaration.
2297 Named_object*
2298 add_type_declaration(const std::string& name, const Package* package,
2299 Location location)
2301 Named_object* no = Named_object::make_type_declaration(name, package,
2302 location);
2303 return this->add_named_object(no);
2306 // Add a variable.
2307 Named_object*
2308 add_variable(const std::string& name, const Package* package,
2309 Variable* variable)
2311 return this->add_named_object(Named_object::make_variable(name, package,
2312 variable));
2315 // Add a result variable.
2316 Named_object*
2317 add_result_variable(const std::string& name, Result_variable* result)
2319 return this->add_named_object(Named_object::make_result_variable(name,
2320 result));
2323 // Add a function.
2324 Named_object*
2325 add_function(const std::string& name, const Package*, Function* function);
2327 // Add a function declaration.
2328 Named_object*
2329 add_function_declaration(const std::string& name, const Package* package,
2330 Function_type* type, Location location);
2332 // Add a package. The location is the location of the import
2333 // statement.
2334 Named_object*
2335 add_package(const std::string& alias, Package* package)
2337 Named_object* no = Named_object::make_package(alias, package);
2338 return this->add_named_object(no);
2341 // Define a type which was already declared.
2342 void
2343 define_type(Named_object*, Named_type*);
2345 // Add a method to the list of objects. This is not added to the
2346 // lookup table.
2347 void
2348 add_method(Named_object*);
2350 // Add a named object to this binding.
2351 Named_object*
2352 add_named_object(Named_object* no)
2353 { return this->add_named_object_to_contour(&this->bindings_, no); }
2355 // Clear all names in file scope from the bindings.
2356 void
2357 clear_file_scope(Gogo*);
2359 // Look up a name in this binding contour and in any enclosing
2360 // binding contours. This returns NULL if the name is not found.
2361 Named_object*
2362 lookup(const std::string&) const;
2364 // Look up a name in this binding contour without looking in any
2365 // enclosing binding contours. Returns NULL if the name is not found.
2366 Named_object*
2367 lookup_local(const std::string&) const;
2369 // Remove a name.
2370 void
2371 remove_binding(Named_object*);
2373 // Mark all variables as used. This is used for some types of parse
2374 // error.
2375 void
2376 mark_locals_used();
2378 // Traverse the tree. See the Traverse class.
2380 traverse(Traverse*, bool is_global);
2382 // Iterate over definitions. This does not include things which
2383 // were only declared.
2385 typedef std::vector<Named_object*>::const_iterator
2386 const_definitions_iterator;
2388 const_definitions_iterator
2389 begin_definitions() const
2390 { return this->named_objects_.begin(); }
2392 const_definitions_iterator
2393 end_definitions() const
2394 { return this->named_objects_.end(); }
2396 // Return the number of definitions.
2397 size_t
2398 size_definitions() const
2399 { return this->named_objects_.size(); }
2401 // Return whether there are no definitions.
2402 bool
2403 empty_definitions() const
2404 { return this->named_objects_.empty(); }
2406 // Iterate over declarations. This is everything that has been
2407 // declared, which includes everything which has been defined.
2409 typedef Contour::const_iterator const_declarations_iterator;
2411 const_declarations_iterator
2412 begin_declarations() const
2413 { return this->bindings_.begin(); }
2415 const_declarations_iterator
2416 end_declarations() const
2417 { return this->bindings_.end(); }
2419 // Return the number of declarations.
2420 size_t
2421 size_declarations() const
2422 { return this->bindings_.size(); }
2424 // Return whether there are no declarations.
2425 bool
2426 empty_declarations() const
2427 { return this->bindings_.empty(); }
2429 // Return the first declaration.
2430 Named_object*
2431 first_declaration()
2432 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2434 private:
2435 Named_object*
2436 add_named_object_to_contour(Contour*, Named_object*);
2438 Named_object*
2439 new_definition(Named_object*, Named_object*);
2441 // Enclosing bindings.
2442 Bindings* enclosing_;
2443 // The list of objects.
2444 std::vector<Named_object*> named_objects_;
2445 // The mapping from names to objects.
2446 Contour bindings_;
2449 // A label.
2451 class Label
2453 public:
2454 Label(const std::string& name)
2455 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2456 refs_(), is_used_(false), blabel_(NULL)
2459 // Return the label's name.
2460 const std::string&
2461 name() const
2462 { return this->name_; }
2464 // Return whether the label has been defined.
2465 bool
2466 is_defined() const
2467 { return !Linemap::is_unknown_location(this->location_); }
2469 // Return whether the label has been used.
2470 bool
2471 is_used() const
2472 { return this->is_used_; }
2474 // Record that the label is used.
2475 void
2476 set_is_used()
2477 { this->is_used_ = true; }
2479 // Return the location of the definition.
2480 Location
2481 location() const
2482 { return this->location_; }
2484 // Return the bindings snapshot.
2485 Bindings_snapshot*
2486 snapshot() const
2487 { return this->snapshot_; }
2489 // Add a snapshot of a goto which refers to this label.
2490 void
2491 add_snapshot_ref(Bindings_snapshot* snapshot)
2493 go_assert(Linemap::is_unknown_location(this->location_));
2494 this->refs_.push_back(snapshot);
2497 // Return the list of snapshots of goto statements which refer to
2498 // this label.
2499 const std::vector<Bindings_snapshot*>&
2500 refs() const
2501 { return this->refs_; }
2503 // Clear the references.
2504 void
2505 clear_refs();
2507 // Define the label at LOCATION with the given bindings snapshot.
2508 void
2509 define(Location location, Bindings_snapshot* snapshot)
2511 go_assert(Linemap::is_unknown_location(this->location_)
2512 && this->snapshot_ == NULL);
2513 this->location_ = location;
2514 this->snapshot_ = snapshot;
2517 // Return the backend representation for this label.
2518 Blabel*
2519 get_backend_label(Translate_context*);
2521 // Return an expression for the address of this label. This is used
2522 // to get the return address of a deferred function to see whether
2523 // the function may call recover.
2524 Bexpression*
2525 get_addr(Translate_context*, Location location);
2527 private:
2528 // The name of the label.
2529 std::string name_;
2530 // The location of the definition. This is 0 if the label has not
2531 // yet been defined.
2532 Location location_;
2533 // A snapshot of the set of bindings defined at this label, used to
2534 // issue errors about invalid goto statements.
2535 Bindings_snapshot* snapshot_;
2536 // A list of snapshots of goto statements which refer to this label.
2537 std::vector<Bindings_snapshot*> refs_;
2538 // Whether the label has been used.
2539 bool is_used_;
2540 // The backend representation.
2541 Blabel* blabel_;
2544 // An unnamed label. These are used when lowering loops.
2546 class Unnamed_label
2548 public:
2549 Unnamed_label(Location location)
2550 : location_(location), blabel_(NULL)
2553 // Get the location where the label is defined.
2554 Location
2555 location() const
2556 { return this->location_; }
2558 // Set the location where the label is defined.
2559 void
2560 set_location(Location location)
2561 { this->location_ = location; }
2563 // Return a statement which defines this label.
2564 Bstatement*
2565 get_definition(Translate_context*);
2567 // Return a goto to this label from LOCATION.
2568 Bstatement*
2569 get_goto(Translate_context*, Location location);
2571 private:
2572 // Return the backend representation.
2573 Blabel*
2574 get_blabel(Translate_context*);
2576 // The location where the label is defined.
2577 Location location_;
2578 // The backend representation of this label.
2579 Blabel* blabel_;
2582 // An imported package.
2584 class Package
2586 public:
2587 Package(const std::string& pkgpath, Location location);
2589 // Get the package path used for all symbols exported from this
2590 // package.
2591 const std::string&
2592 pkgpath() const
2593 { return this->pkgpath_; }
2595 // Return the package path to use for a symbol name.
2596 const std::string&
2597 pkgpath_symbol() const
2598 { return this->pkgpath_symbol_; }
2600 // Return the location of the import statement.
2601 Location
2602 location() const
2603 { return this->location_; }
2605 // Return whether we know the name of this package yet.
2606 bool
2607 has_package_name() const
2608 { return !this->package_name_.empty(); }
2610 // The name that this package uses in its package clause. This may
2611 // be different from the name in the associated Named_object if the
2612 // import statement used an alias.
2613 const std::string&
2614 package_name() const
2616 go_assert(!this->package_name_.empty());
2617 return this->package_name_;
2620 // The priority of this package. The init function of packages with
2621 // lower priority must be run before the init function of packages
2622 // with higher priority.
2624 priority() const
2625 { return this->priority_; }
2627 // Set the priority.
2628 void
2629 set_priority(int priority);
2631 // Return the bindings.
2632 Bindings*
2633 bindings()
2634 { return this->bindings_; }
2636 // Whether some symbol from the package was used.
2637 bool
2638 used() const
2639 { return this->used_; }
2641 // Note that some symbol from this package was used.
2642 void
2643 set_used() const
2644 { this->used_ = true; }
2646 // Clear the used field for the next file.
2647 void
2648 clear_used()
2649 { this->used_ = false; }
2651 // Whether this package was imported in the current file.
2652 bool
2653 is_imported() const
2654 { return this->is_imported_; }
2656 // Note that this package was imported in the current file.
2657 void
2658 set_is_imported()
2659 { this->is_imported_ = true; }
2661 // Clear the imported field for the next file.
2662 void
2663 clear_is_imported()
2664 { this->is_imported_ = false; }
2666 // Whether this package was imported with a name of "_".
2667 bool
2668 uses_sink_alias() const
2669 { return this->uses_sink_alias_; }
2671 // Note that this package was imported with a name of "_".
2672 void
2673 set_uses_sink_alias()
2674 { this->uses_sink_alias_ = true; }
2676 // Clear the sink alias field for the next file.
2677 void
2678 clear_uses_sink_alias()
2679 { this->uses_sink_alias_ = false; }
2681 // Look up a name in the package. Returns NULL if the name is not
2682 // found.
2683 Named_object*
2684 lookup(const std::string& name) const
2685 { return this->bindings_->lookup(name); }
2687 // Set the name of the package.
2688 void
2689 set_package_name(const std::string& name, Location);
2691 // Set the location of the package. This is used to record the most
2692 // recent import location.
2693 void
2694 set_location(Location location)
2695 { this->location_ = location; }
2697 // Add a constant to the package.
2698 Named_object*
2699 add_constant(const Typed_identifier& tid, Expression* expr)
2700 { return this->bindings_->add_constant(tid, this, expr, 0); }
2702 // Add a type to the package.
2703 Named_object*
2704 add_type(const std::string& name, Type* type, Location location)
2705 { return this->bindings_->add_type(name, this, type, location); }
2707 // Add a type declaration to the package.
2708 Named_object*
2709 add_type_declaration(const std::string& name, Location location)
2710 { return this->bindings_->add_type_declaration(name, this, location); }
2712 // Add a variable to the package.
2713 Named_object*
2714 add_variable(const std::string& name, Variable* variable)
2715 { return this->bindings_->add_variable(name, this, variable); }
2717 // Add a function declaration to the package.
2718 Named_object*
2719 add_function_declaration(const std::string& name, Function_type* type,
2720 Location loc)
2721 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2723 // Determine types of constants.
2724 void
2725 determine_types();
2727 private:
2728 // The package path for type reflection data.
2729 std::string pkgpath_;
2730 // The package path for symbol names.
2731 std::string pkgpath_symbol_;
2732 // The name that this package uses in the package clause. This may
2733 // be the empty string if it is not yet known.
2734 std::string package_name_;
2735 // The names in this package.
2736 Bindings* bindings_;
2737 // The priority of this package. A package has a priority higher
2738 // than the priority of all of the packages that it imports. This
2739 // is used to run init functions in the right order.
2740 int priority_;
2741 // The location of the import statement.
2742 Location location_;
2743 // True if some name from this package was used. This is mutable
2744 // because we can use a package even if we have a const pointer to
2745 // it.
2746 mutable bool used_;
2747 // True if this package was imported in the current file.
2748 bool is_imported_;
2749 // True if this package was imported with a name of "_".
2750 bool uses_sink_alias_;
2753 // Return codes for the traversal functions. This is not an enum
2754 // because we want to be able to declare traversal functions in other
2755 // header files without including this one.
2757 // Continue traversal as usual.
2758 const int TRAVERSE_CONTINUE = -1;
2760 // Exit traversal.
2761 const int TRAVERSE_EXIT = 0;
2763 // Continue traversal, but skip components of the current object.
2764 // E.g., if this is returned by Traverse::statement, we do not
2765 // traverse the expressions in the statement even if
2766 // traverse_expressions is set in the traverse_mask.
2767 const int TRAVERSE_SKIP_COMPONENTS = 1;
2769 // This class is used when traversing the parse tree. The caller uses
2770 // a subclass which overrides functions as desired.
2772 class Traverse
2774 public:
2775 // These bitmasks say what to traverse.
2776 static const unsigned int traverse_variables = 0x1;
2777 static const unsigned int traverse_constants = 0x2;
2778 static const unsigned int traverse_functions = 0x4;
2779 static const unsigned int traverse_blocks = 0x8;
2780 static const unsigned int traverse_statements = 0x10;
2781 static const unsigned int traverse_expressions = 0x20;
2782 static const unsigned int traverse_types = 0x40;
2784 Traverse(unsigned int traverse_mask)
2785 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2788 virtual ~Traverse();
2790 // The bitmask of what to traverse.
2791 unsigned int
2792 traverse_mask() const
2793 { return this->traverse_mask_; }
2795 // Record that we are going to traverse a type. This returns true
2796 // if the type has already been seen in this traversal. This is
2797 // required because types, unlike expressions, can form a circular
2798 // graph.
2799 bool
2800 remember_type(const Type*);
2802 // Record that we are going to see an expression. This returns true
2803 // if the expression has already been seen in this traversal. This
2804 // is only needed for cases where multiple expressions can point to
2805 // a single one.
2806 bool
2807 remember_expression(const Expression*);
2809 // These functions return one of the TRAVERSE codes defined above.
2811 // If traverse_variables is set in the mask, this is called for
2812 // every variable in the tree.
2813 virtual int
2814 variable(Named_object*);
2816 // If traverse_constants is set in the mask, this is called for
2817 // every named constant in the tree. The bool parameter is true for
2818 // a global constant.
2819 virtual int
2820 constant(Named_object*, bool);
2822 // If traverse_functions is set in the mask, this is called for
2823 // every function in the tree.
2824 virtual int
2825 function(Named_object*);
2827 // If traverse_blocks is set in the mask, this is called for every
2828 // block in the tree.
2829 virtual int
2830 block(Block*);
2832 // If traverse_statements is set in the mask, this is called for
2833 // every statement in the tree.
2834 virtual int
2835 statement(Block*, size_t* index, Statement*);
2837 // If traverse_expressions is set in the mask, this is called for
2838 // every expression in the tree.
2839 virtual int
2840 expression(Expression**);
2842 // If traverse_types is set in the mask, this is called for every
2843 // type in the tree.
2844 virtual int
2845 type(Type*);
2847 private:
2848 // A hash table for types we have seen during this traversal. Note
2849 // that this uses the default hash functions for pointers rather
2850 // than Type_hash_identical and Type_identical. This is because for
2851 // traversal we care about seeing a specific type structure. If
2852 // there are two separate instances of identical types, we want to
2853 // traverse both.
2854 typedef Unordered_set(const Type*) Types_seen;
2856 typedef Unordered_set(const Expression*) Expressions_seen;
2858 // Bitmask of what sort of objects to traverse.
2859 unsigned int traverse_mask_;
2860 // Types which have been seen in this traversal.
2861 Types_seen* types_seen_;
2862 // Expressions which have been seen in this traversal.
2863 Expressions_seen* expressions_seen_;
2866 // A class which makes it easier to insert new statements before the
2867 // current statement during a traversal.
2869 class Statement_inserter
2871 public:
2872 // Empty constructor.
2873 Statement_inserter()
2874 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2877 // Constructor for a statement in a block.
2878 Statement_inserter(Block* block, size_t *pindex)
2879 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2882 // Constructor for a global variable.
2883 Statement_inserter(Gogo* gogo, Variable* var)
2884 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2885 { go_assert(var->is_global()); }
2887 // We use the default copy constructor and assignment operator.
2889 // Insert S before the statement we are traversing, or before the
2890 // initialization expression of a global variable.
2891 void
2892 insert(Statement* s);
2894 private:
2895 // The block that the statement is in.
2896 Block* block_;
2897 // The index of the statement that we are traversing.
2898 size_t* pindex_;
2899 // The IR, needed when looking at an initializer expression for a
2900 // global variable.
2901 Gogo* gogo_;
2902 // The global variable, when looking at an initializer expression.
2903 Variable* var_;
2906 // When translating the gogo IR into the backend data structure, this
2907 // is the context we pass down the blocks and statements.
2909 class Translate_context
2911 public:
2912 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2913 Bblock* bblock)
2914 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2915 block_(block), bblock_(bblock), is_const_(false)
2918 // Accessors.
2920 Gogo*
2921 gogo()
2922 { return this->gogo_; }
2924 Backend*
2925 backend()
2926 { return this->backend_; }
2928 Named_object*
2929 function()
2930 { return this->function_; }
2932 Block*
2933 block()
2934 { return this->block_; }
2936 Bblock*
2937 bblock()
2938 { return this->bblock_; }
2940 bool
2941 is_const()
2942 { return this->is_const_; }
2944 // Make a constant context.
2945 void
2946 set_is_const()
2947 { this->is_const_ = true; }
2949 private:
2950 // The IR for the entire compilation unit.
2951 Gogo* gogo_;
2952 // The generator for the backend data structures.
2953 Backend* backend_;
2954 // The function we are currently translating. NULL if not in a
2955 // function, e.g., the initializer of a global variable.
2956 Named_object* function_;
2957 // The block we are currently translating. NULL if not in a
2958 // function.
2959 Block *block_;
2960 // The backend representation of the current block. NULL if block_
2961 // is NULL.
2962 Bblock* bblock_;
2963 // Whether this is being evaluated in a constant context. This is
2964 // used for type descriptor initializers.
2965 bool is_const_;
2968 // Runtime error codes. These must match the values in
2969 // libgo/runtime/go-runtime-error.c.
2971 // Slice index out of bounds: negative or larger than the length of
2972 // the slice.
2973 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2975 // Array index out of bounds.
2976 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2978 // String index out of bounds.
2979 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2981 // Slice slice out of bounds: negative or larger than the length of
2982 // the slice or high bound less than low bound.
2983 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2985 // Array slice out of bounds.
2986 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2988 // String slice out of bounds.
2989 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2991 // Dereference of nil pointer. This is used when there is a
2992 // dereference of a pointer to a very large struct or array, to ensure
2993 // that a gigantic array is not used a proxy to access random memory
2994 // locations.
2995 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2997 // Slice length or capacity out of bounds in make: negative or
2998 // overflow or length greater than capacity.
2999 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3001 // Map capacity out of bounds in make: negative or overflow.
3002 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3004 // Channel capacity out of bounds in make: negative or overflow.
3005 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3007 // Division by zero.
3008 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3010 // This is used by some of the langhooks.
3011 extern Gogo* go_get_gogo();
3013 // Whether we have seen any errors. FIXME: Replace with a backend
3014 // interface.
3015 extern bool saw_errors();
3017 #endif // !defined(GO_GOGO_H)