compiler: Use backend interface for expressions.
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blobb2076d71b7c5c3a4f749ea8be4d1f05fb2a1ae6e
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_named_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 // Write out the global values.
595 void
596 write_globals();
598 // Build a call to the runtime error function.
599 Expression*
600 runtime_error(int code, Location);
602 // Build required interface method tables.
603 void
604 build_interface_method_tables();
606 // Return an expression which allocates memory to hold values of type TYPE.
607 Expression*
608 allocate_memory(Type *type, Location);
610 // Get the name of the magic initialization function.
611 const std::string&
612 get_init_fn_name();
614 private:
615 // During parsing, we keep a stack of functions. Each function on
616 // the stack is one that we are currently parsing. For each
617 // function, we keep track of the current stack of blocks.
618 struct Open_function
620 // The function.
621 Named_object* function;
622 // The stack of active blocks in the function.
623 std::vector<Block*> blocks;
626 // The stack of functions.
627 typedef std::vector<Open_function> Open_functions;
629 // Set up the built-in unsafe package.
630 void
631 import_unsafe(const std::string&, bool is_exported, Location);
633 // Return the current binding contour.
634 Bindings*
635 current_bindings();
637 const Bindings*
638 current_bindings() const;
640 // Get the decl for the magic initialization function.
641 Named_object*
642 initialization_function_decl();
644 // Create the magic initialization function.
645 Named_object*
646 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
648 // Initialize imported packages.
649 void
650 init_imports(std::vector<Bstatement*>&);
652 // Register variables with the garbage collector.
653 void
654 register_gc_vars(const std::vector<Named_object*>&,
655 std::vector<Bstatement*>&);
657 // Type used to map import names to packages.
658 typedef std::map<std::string, Package*> Imports;
660 // Type used to map package names to packages.
661 typedef std::map<std::string, Package*> Packages;
663 // Type used to map variables to the function calls that set them.
664 // This is used for initialization dependency analysis.
665 typedef std::map<Variable*, Named_object*> Var_deps;
667 // Type used to map identifiers in the file block to the location
668 // where they were defined.
669 typedef Unordered_map(std::string, Location) File_block_names;
671 // Type used to queue writing a type specific function.
672 struct Specific_type_function
674 Type* type;
675 Named_type* name;
676 std::string hash_name;
677 Function_type* hash_fntype;
678 std::string equal_name;
679 Function_type* equal_fntype;
681 Specific_type_function(Type* atype, Named_type* aname,
682 const std::string& ahash_name,
683 Function_type* ahash_fntype,
684 const std::string& aequal_name,
685 Function_type* aequal_fntype)
686 : type(atype), name(aname), hash_name(ahash_name),
687 hash_fntype(ahash_fntype), equal_name(aequal_name),
688 equal_fntype(aequal_fntype)
692 // The backend generator.
693 Backend* backend_;
694 // The object used to keep track of file names and line numbers.
695 Linemap* linemap_;
696 // The package we are compiling.
697 Package* package_;
698 // The list of currently open functions during parsing.
699 Open_functions functions_;
700 // The global binding contour. This includes the builtin functions
701 // and the package we are compiling.
702 Bindings* globals_;
703 // The list of names we have seen in the file block.
704 File_block_names file_block_names_;
705 // Mapping from import file names to packages.
706 Imports imports_;
707 // Whether the magic unsafe package was imported.
708 bool imported_unsafe_;
709 // Mapping from package names we have seen to packages. This does
710 // not include the package we are compiling.
711 Packages packages_;
712 // The functions named "init", if there are any.
713 std::vector<Named_object*> init_functions_;
714 // A mapping from variables to the function calls that initialize
715 // them, if it is not stored in the variable's init or preinit.
716 // This is used for dependency analysis.
717 Var_deps var_deps_;
718 // Whether we need a magic initialization function.
719 bool need_init_fn_;
720 // The name of the magic initialization function.
721 std::string init_fn_name_;
722 // A list of import control variables for packages that we import.
723 std::set<Import_init> imported_init_fns_;
724 // The package path used for reflection data.
725 std::string pkgpath_;
726 // The package path to use for a symbol name.
727 std::string pkgpath_symbol_;
728 // The prefix to use for symbols, from the -fgo-prefix option.
729 std::string prefix_;
730 // Whether pkgpath_ has been set.
731 bool pkgpath_set_;
732 // Whether an explicit package path was set by -fgo-pkgpath.
733 bool pkgpath_from_option_;
734 // Whether an explicit prefix was set by -fgo-prefix.
735 bool prefix_from_option_;
736 // The relative import path, from the -fgo-relative-import-path
737 // option.
738 std::string relative_import_path_;
739 // Whether or not to check for division by zero, from the
740 // -fgo-check-divide-zero option.
741 bool check_divide_by_zero_;
742 // Whether or not to check for division overflow, from the
743 // -fgo-check-divide-overflow option.
744 bool check_divide_overflow_;
745 // A list of types to verify.
746 std::vector<Type*> verify_types_;
747 // A list of interface types defined while parsing.
748 std::vector<Interface_type*> interface_types_;
749 // Type specific functions to write out.
750 std::vector<Specific_type_function*> specific_type_functions_;
751 // Whether we are done writing out specific type functions.
752 bool specific_type_functions_are_written_;
753 // Whether named types have been converted.
754 bool named_types_are_converted_;
757 // A block of statements.
759 class Block
761 public:
762 Block(Block* enclosing, Location);
764 // Return the enclosing block.
765 const Block*
766 enclosing() const
767 { return this->enclosing_; }
769 // Return the bindings of the block.
770 Bindings*
771 bindings()
772 { return this->bindings_; }
774 const Bindings*
775 bindings() const
776 { return this->bindings_; }
778 // Look at the block's statements.
779 const std::vector<Statement*>*
780 statements() const
781 { return &this->statements_; }
783 // Return the start location. This is normally the location of the
784 // left curly brace which starts the block.
785 Location
786 start_location() const
787 { return this->start_location_; }
789 // Return the end location. This is normally the location of the
790 // right curly brace which ends the block.
791 Location
792 end_location() const
793 { return this->end_location_; }
795 // Add a statement to the block.
796 void
797 add_statement(Statement*);
799 // Add a statement to the front of the block.
800 void
801 add_statement_at_front(Statement*);
803 // Replace a statement in a block.
804 void
805 replace_statement(size_t index, Statement*);
807 // Add a Statement before statement number INDEX.
808 void
809 insert_statement_before(size_t index, Statement*);
811 // Add a Statement after statement number INDEX.
812 void
813 insert_statement_after(size_t index, Statement*);
815 // Set the end location of the block.
816 void
817 set_end_location(Location location)
818 { this->end_location_ = location; }
820 // Traverse the tree.
822 traverse(Traverse*);
824 // Set final types for unspecified variables and constants.
825 void
826 determine_types();
828 // Return true if execution of this block may fall through to the
829 // next block.
830 bool
831 may_fall_through() const;
833 // Convert the block to the backend representation.
834 Bblock*
835 get_backend(Translate_context*);
837 // Iterate over statements.
839 typedef std::vector<Statement*>::iterator iterator;
841 iterator
842 begin()
843 { return this->statements_.begin(); }
845 iterator
846 end()
847 { return this->statements_.end(); }
849 private:
850 // Enclosing block.
851 Block* enclosing_;
852 // Statements in the block.
853 std::vector<Statement*> statements_;
854 // Binding contour.
855 Bindings* bindings_;
856 // Location of start of block.
857 Location start_location_;
858 // Location of end of block.
859 Location end_location_;
862 // A function.
864 class Function
866 public:
867 Function(Function_type* type, Function*, Block*, Location);
869 // Return the function's type.
870 Function_type*
871 type() const
872 { return this->type_; }
874 // Return the enclosing function if there is one.
875 Function*
876 enclosing()
877 { return this->enclosing_; }
879 // Set the enclosing function. This is used when building thunks
880 // for functions which call recover.
881 void
882 set_enclosing(Function* enclosing)
884 go_assert(this->enclosing_ == NULL);
885 this->enclosing_ = enclosing;
888 // The result variables.
889 typedef std::vector<Named_object*> Results;
891 // Create the result variables in the outer block.
892 void
893 create_result_variables(Gogo*);
895 // Update the named result variables when cloning a function which
896 // calls recover.
897 void
898 update_result_variables();
900 // Return the result variables.
901 Results*
902 result_variables()
903 { return this->results_; }
905 bool
906 is_sink() const
907 { return this->is_sink_; }
909 void
910 set_is_sink()
911 { this->is_sink_ = true; }
913 // Whether the result variables have names.
914 bool
915 results_are_named() const
916 { return this->results_are_named_; }
918 // Whether this method should not be included in the type
919 // descriptor.
920 bool
921 nointerface() const
923 go_assert(this->is_method());
924 return this->nointerface_;
927 // Record that this method should not be included in the type
928 // descriptor.
929 void
930 set_nointerface()
932 go_assert(this->is_method());
933 this->nointerface_ = true;
936 // Record that this function is a stub method created for an unnamed
937 // type.
938 void
939 set_is_unnamed_type_stub_method()
941 go_assert(this->is_method());
942 this->is_unnamed_type_stub_method_ = true;
945 // Add a new field to the closure variable.
946 void
947 add_closure_field(Named_object* var, Location loc)
948 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
950 // Whether this function needs a closure.
951 bool
952 needs_closure() const
953 { return !this->closure_fields_.empty(); }
955 // Return the closure variable, creating it if necessary. This is
956 // passed to the function as a static chain parameter.
957 Named_object*
958 closure_var();
960 // Set the closure variable. This is used when building thunks for
961 // functions which call recover.
962 void
963 set_closure_var(Named_object* v)
965 go_assert(this->closure_var_ == NULL);
966 this->closure_var_ = v;
969 // Return the variable for a reference to field INDEX in the closure
970 // variable.
971 Named_object*
972 enclosing_var(unsigned int index)
974 go_assert(index < this->closure_fields_.size());
975 return closure_fields_[index].first;
978 // Set the type of the closure variable if there is one.
979 void
980 set_closure_type();
982 // Get the block of statements associated with the function.
983 Block*
984 block() const
985 { return this->block_; }
987 // Get the location of the start of the function.
988 Location
989 location() const
990 { return this->location_; }
992 // Return whether this function is actually a method.
993 bool
994 is_method() const;
996 // Add a label definition to the function.
997 Label*
998 add_label_definition(Gogo*, const std::string& label_name, Location);
1000 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1001 // if we should report errors for a goto from the current location
1002 // to the label location.
1003 Label*
1004 add_label_reference(Gogo*, const std::string& label_name,
1005 Location, bool issue_goto_errors);
1007 // Warn about labels that are defined but not used.
1008 void
1009 check_labels() const;
1011 // Note that a new local type has been added. Return its index.
1012 unsigned int
1013 new_local_type_index()
1014 { return this->local_type_count_++; }
1016 // Whether this function calls the predeclared recover function.
1017 bool
1018 calls_recover() const
1019 { return this->calls_recover_; }
1021 // Record that this function calls the predeclared recover function.
1022 // This is set during the lowering pass.
1023 void
1024 set_calls_recover()
1025 { this->calls_recover_ = true; }
1027 // Whether this is a recover thunk function.
1028 bool
1029 is_recover_thunk() const
1030 { return this->is_recover_thunk_; }
1032 // Record that this is a thunk built for a function which calls
1033 // recover.
1034 void
1035 set_is_recover_thunk()
1036 { this->is_recover_thunk_ = true; }
1038 // Whether this function already has a recover thunk.
1039 bool
1040 has_recover_thunk() const
1041 { return this->has_recover_thunk_; }
1043 // Record that this function already has a recover thunk.
1044 void
1045 set_has_recover_thunk()
1046 { this->has_recover_thunk_ = true; }
1048 // Mark the function as going into a unique section.
1049 void
1050 set_in_unique_section()
1051 { this->in_unique_section_ = true; }
1053 // Swap with another function. Used only for the thunk which calls
1054 // recover.
1055 void
1056 swap_for_recover(Function *);
1058 // Traverse the tree.
1060 traverse(Traverse*);
1062 // Determine types in the function.
1063 void
1064 determine_types();
1066 // Return an expression for the function descriptor, given the named
1067 // object for this function. This may only be called for functions
1068 // without a closure. This will be an immutable struct with one
1069 // field that points to the function's code.
1070 Expression*
1071 descriptor(Gogo*, Named_object*);
1073 // Set the descriptor for this function. This is used when a
1074 // function declaration is followed by a function definition.
1075 void
1076 set_descriptor(Expression* descriptor)
1078 go_assert(this->descriptor_ == NULL);
1079 this->descriptor_ = descriptor;
1082 // Return the backend representation.
1083 Bfunction*
1084 get_or_make_decl(Gogo*, Named_object*);
1086 // Return the function's decl after it has been built.
1087 Bfunction*
1088 get_decl() const;
1090 // Set the function decl to hold a backend representation of the function
1091 // code.
1092 void
1093 build(Gogo*, Named_object*);
1095 // Get the statement that assigns values to this function's result struct.
1096 Bstatement*
1097 return_value(Gogo*, Named_object*, Location) const;
1099 // Get an expression for the variable holding the defer stack.
1100 Expression*
1101 defer_stack(Location);
1103 // Export the function.
1104 void
1105 export_func(Export*, const std::string& name) const;
1107 // Export a function with a type.
1108 static void
1109 export_func_with_type(Export*, const std::string& name,
1110 const Function_type*);
1112 // Import a function.
1113 static void
1114 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1115 Typed_identifier_list** pparameters,
1116 Typed_identifier_list** presults, bool* is_varargs);
1118 private:
1119 // Type for mapping from label names to Label objects.
1120 typedef Unordered_map(std::string, Label*) Labels;
1122 void
1123 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1125 typedef std::vector<std::pair<Named_object*,
1126 Location> > Closure_fields;
1128 // The function's type.
1129 Function_type* type_;
1130 // The enclosing function. This is NULL when there isn't one, which
1131 // is the normal case.
1132 Function* enclosing_;
1133 // The result variables, if any.
1134 Results* results_;
1135 // If there is a closure, this is the list of variables which appear
1136 // in the closure. This is created by the parser, and then resolved
1137 // to a real type when we lower parse trees.
1138 Closure_fields closure_fields_;
1139 // The closure variable, passed as a parameter using the static
1140 // chain parameter. Normally NULL.
1141 Named_object* closure_var_;
1142 // The outer block of statements in the function.
1143 Block* block_;
1144 // The source location of the start of the function.
1145 Location location_;
1146 // Labels defined or referenced in the function.
1147 Labels labels_;
1148 // The number of local types defined in this function.
1149 unsigned int local_type_count_;
1150 // The function descriptor, if any.
1151 Expression* descriptor_;
1152 // The function decl.
1153 Bfunction* fndecl_;
1154 // The defer stack variable. A pointer to this variable is used to
1155 // distinguish the defer stack for one function from another. This
1156 // is NULL unless we actually need a defer stack.
1157 Temporary_statement* defer_stack_;
1158 // True if this function is sink-named. No code is generated.
1159 bool is_sink_ : 1;
1160 // True if the result variables are named.
1161 bool results_are_named_ : 1;
1162 // True if this method should not be included in the type descriptor.
1163 bool nointerface_ : 1;
1164 // True if this function is a stub method created for an unnamed
1165 // type.
1166 bool is_unnamed_type_stub_method_ : 1;
1167 // True if this function calls the predeclared recover function.
1168 bool calls_recover_ : 1;
1169 // True if this a thunk built for a function which calls recover.
1170 bool is_recover_thunk_ : 1;
1171 // True if this function already has a recover thunk.
1172 bool has_recover_thunk_ : 1;
1173 // True if this function should be put in a unique section. This is
1174 // turned on for field tracking.
1175 bool in_unique_section_ : 1;
1178 // A snapshot of the current binding state.
1180 class Bindings_snapshot
1182 public:
1183 Bindings_snapshot(const Block*, Location);
1185 // Report any errors appropriate for a goto from the current binding
1186 // state of B to this one.
1187 void
1188 check_goto_from(const Block* b, Location);
1190 // Report any errors appropriate for a goto from this binding state
1191 // to the current state of B.
1192 void
1193 check_goto_to(const Block* b);
1195 private:
1196 bool
1197 check_goto_block(Location, const Block*, const Block*, size_t*);
1199 void
1200 check_goto_defs(Location, const Block*, size_t, size_t);
1202 // The current block.
1203 const Block* block_;
1204 // The number of names currently defined in each open block.
1205 // Element 0 is this->block_, element 1 is
1206 // this->block_->enclosing(), etc.
1207 std::vector<size_t> counts_;
1208 // The location where this snapshot was taken.
1209 Location location_;
1212 // A function declaration.
1214 class Function_declaration
1216 public:
1217 Function_declaration(Function_type* fntype, Location location)
1218 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1219 fndecl_(NULL)
1222 Function_type*
1223 type() const
1224 { return this->fntype_; }
1226 Location
1227 location() const
1228 { return this->location_; }
1230 const std::string&
1231 asm_name() const
1232 { return this->asm_name_; }
1234 // Set the assembler name.
1235 void
1236 set_asm_name(const std::string& asm_name)
1237 { this->asm_name_ = asm_name; }
1239 // Return an expression for the function descriptor, given the named
1240 // object for this function. This may only be called for functions
1241 // without a closure. This will be an immutable struct with one
1242 // field that points to the function's code.
1243 Expression*
1244 descriptor(Gogo*, Named_object*);
1246 // Return true if we have created a descriptor for this declaration.
1247 bool
1248 has_descriptor() const
1249 { return this->descriptor_ != NULL; }
1251 // Return a backend representation.
1252 Bfunction*
1253 get_or_make_decl(Gogo*, Named_object*);
1255 // If there is a descriptor, build it into the backend
1256 // representation.
1257 void
1258 build_backend_descriptor(Gogo*);
1260 // Export a function declaration.
1261 void
1262 export_func(Export* exp, const std::string& name) const
1263 { Function::export_func_with_type(exp, name, this->fntype_); }
1265 private:
1266 // The type of the function.
1267 Function_type* fntype_;
1268 // The location of the declaration.
1269 Location location_;
1270 // The assembler name: this is the name to use in references to the
1271 // function. This is normally empty.
1272 std::string asm_name_;
1273 // The function descriptor, if any.
1274 Expression* descriptor_;
1275 // The function decl if needed.
1276 Bfunction* fndecl_;
1279 // A variable.
1281 class Variable
1283 public:
1284 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1285 bool is_receiver, Location);
1287 // Get the type of the variable.
1288 Type*
1289 type();
1291 Type*
1292 type() const;
1294 // Return whether the type is defined yet.
1295 bool
1296 has_type() const;
1298 // Get the initial value.
1299 Expression*
1300 init() const
1301 { return this->init_; }
1303 // Return whether there are any preinit statements.
1304 bool
1305 has_pre_init() const
1306 { return this->preinit_ != NULL; }
1308 // Return the preinit statements if any.
1309 Block*
1310 preinit() const
1311 { return this->preinit_; }
1313 // Return whether this is a global variable.
1314 bool
1315 is_global() const
1316 { return this->is_global_; }
1318 // Return whether this is a function parameter.
1319 bool
1320 is_parameter() const
1321 { return this->is_parameter_; }
1323 // Return whether this is the receiver parameter of a method.
1324 bool
1325 is_receiver() const
1326 { return this->is_receiver_; }
1328 // Change this parameter to be a receiver. This is used when
1329 // creating the thunks created for functions which call recover.
1330 void
1331 set_is_receiver()
1333 go_assert(this->is_parameter_);
1334 this->is_receiver_ = true;
1337 // Change this parameter to not be a receiver. This is used when
1338 // creating the thunks created for functions which call recover.
1339 void
1340 set_is_not_receiver()
1342 go_assert(this->is_parameter_);
1343 this->is_receiver_ = false;
1346 // Return whether this is the varargs parameter of a function.
1347 bool
1348 is_varargs_parameter() const
1349 { return this->is_varargs_parameter_; }
1351 // Whether this variable's address is taken.
1352 bool
1353 is_address_taken() const
1354 { return this->is_address_taken_; }
1356 // Whether this variable should live in the heap.
1357 bool
1358 is_in_heap() const
1359 { return this->is_address_taken_ && !this->is_global_; }
1361 // Note that something takes the address of this variable.
1362 void
1363 set_address_taken()
1364 { this->is_address_taken_ = true; }
1366 // Return whether the address is taken but does not escape.
1367 bool
1368 is_non_escaping_address_taken() const
1369 { return this->is_non_escaping_address_taken_; }
1371 // Note that something takes the address of this variable such that
1372 // the address does not escape the function.
1373 void
1374 set_non_escaping_address_taken()
1375 { this->is_non_escaping_address_taken_ = true; }
1377 // Get the source location of the variable's declaration.
1378 Location
1379 location() const
1380 { return this->location_; }
1382 // Record that this is the varargs parameter of a function.
1383 void
1384 set_is_varargs_parameter()
1386 go_assert(this->is_parameter_);
1387 this->is_varargs_parameter_ = true;
1390 // Return whether the variable has been used.
1391 bool
1392 is_used() const
1393 { return this->is_used_; }
1395 // Mark that the variable has been used.
1396 void
1397 set_is_used()
1398 { this->is_used_ = true; }
1400 // Clear the initial value; used for error handling.
1401 void
1402 clear_init()
1403 { this->init_ = NULL; }
1405 // Set the initial value; used for converting shortcuts.
1406 void
1407 set_init(Expression* init)
1408 { this->init_ = init; }
1410 // Get the preinit block, a block of statements to be run before the
1411 // initialization expression.
1412 Block*
1413 preinit_block(Gogo*);
1415 // Add a statement to be run before the initialization expression.
1416 // This is only used for global variables.
1417 void
1418 add_preinit_statement(Gogo*, Statement*);
1420 // Lower the initialization expression after parsing is complete.
1421 void
1422 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1424 // Flatten the initialization expression after ordering evaluations.
1425 void
1426 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1428 // A special case: the init value is used only to determine the
1429 // type. This is used if the variable is defined using := with the
1430 // comma-ok form of a map index or a receive expression. The init
1431 // value is actually the map index expression or receive expression.
1432 // We use this because we may not know the right type at parse time.
1433 void
1434 set_type_from_init_tuple()
1435 { this->type_from_init_tuple_ = true; }
1437 // Another special case: the init value is used only to determine
1438 // the type. This is used if the variable is defined using := with
1439 // a range clause. The init value is the range expression. The
1440 // type of the variable is the index type of the range expression
1441 // (i.e., the first value returned by a range).
1442 void
1443 set_type_from_range_index()
1444 { this->type_from_range_index_ = true; }
1446 // Another special case: like set_type_from_range_index, but the
1447 // type is the value type of the range expression (i.e., the second
1448 // value returned by a range).
1449 void
1450 set_type_from_range_value()
1451 { this->type_from_range_value_ = true; }
1453 // Another special case: the init value is used only to determine
1454 // the type. This is used if the variable is defined using := with
1455 // a case in a select statement. The init value is the channel.
1456 // The type of the variable is the channel's element type.
1457 void
1458 set_type_from_chan_element()
1459 { this->type_from_chan_element_ = true; }
1461 // After we lower the select statement, we once again set the type
1462 // from the initialization expression.
1463 void
1464 clear_type_from_chan_element()
1466 go_assert(this->type_from_chan_element_);
1467 this->type_from_chan_element_ = false;
1470 // Note that this variable was created for a type switch clause.
1471 void
1472 set_is_type_switch_var()
1473 { this->is_type_switch_var_ = true; }
1475 // Mark the variable as going into a unique section.
1476 void
1477 set_in_unique_section()
1479 go_assert(this->is_global_);
1480 this->in_unique_section_ = true;
1483 // Traverse the initializer expression.
1485 traverse_expression(Traverse*, unsigned int traverse_mask);
1487 // Determine the type of the variable if necessary.
1488 void
1489 determine_type();
1491 // Get the backend representation of the variable.
1492 Bvariable*
1493 get_backend_variable(Gogo*, Named_object*, const Package*,
1494 const std::string&);
1496 // Get the initial value of the variable. This may only
1497 // be called if has_pre_init() returns false.
1498 Bexpression*
1499 get_init(Gogo*, Named_object* function);
1501 // Return a series of statements which sets the value of the
1502 // variable in DECL. This should only be called is has_pre_init()
1503 // returns true. DECL may be NULL for a sink variable.
1504 Bstatement*
1505 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1507 // Export the variable.
1508 void
1509 export_var(Export*, const std::string& name) const;
1511 // Import a variable.
1512 static void
1513 import_var(Import*, std::string* pname, Type** ptype);
1515 private:
1516 // The type of a tuple.
1517 Type*
1518 type_from_tuple(Expression*, bool) const;
1520 // The type of a range.
1521 Type*
1522 type_from_range(Expression*, bool, bool) const;
1524 // The element type of a channel.
1525 Type*
1526 type_from_chan_element(Expression*, bool) const;
1528 // The variable's type. This may be NULL if the type is set from
1529 // the expression.
1530 Type* type_;
1531 // The initial value. This may be NULL if the variable should be
1532 // initialized to the default value for the type.
1533 Expression* init_;
1534 // Statements to run before the init statement.
1535 Block* preinit_;
1536 // Location of variable definition.
1537 Location location_;
1538 // Backend representation.
1539 Bvariable* backend_;
1540 // Whether this is a global variable.
1541 bool is_global_ : 1;
1542 // Whether this is a function parameter.
1543 bool is_parameter_ : 1;
1544 // Whether this is the receiver parameter of a method.
1545 bool is_receiver_ : 1;
1546 // Whether this is the varargs parameter of a function.
1547 bool is_varargs_parameter_ : 1;
1548 // Whether this variable is ever referenced.
1549 bool is_used_ : 1;
1550 // Whether something takes the address of this variable. For a
1551 // local variable this implies that the variable has to be on the
1552 // heap.
1553 bool is_address_taken_ : 1;
1554 // Whether something takes the address of this variable such that
1555 // the address does not escape the function.
1556 bool is_non_escaping_address_taken_ : 1;
1557 // True if we have seen this variable in a traversal.
1558 bool seen_ : 1;
1559 // True if we have lowered the initialization expression.
1560 bool init_is_lowered_ : 1;
1561 // True if we have flattened the initialization expression.
1562 bool init_is_flattened_ : 1;
1563 // True if init is a tuple used to set the type.
1564 bool type_from_init_tuple_ : 1;
1565 // True if init is a range clause and the type is the index type.
1566 bool type_from_range_index_ : 1;
1567 // True if init is a range clause and the type is the value type.
1568 bool type_from_range_value_ : 1;
1569 // True if init is a channel and the type is the channel's element type.
1570 bool type_from_chan_element_ : 1;
1571 // True if this is a variable created for a type switch case.
1572 bool is_type_switch_var_ : 1;
1573 // True if we have determined types.
1574 bool determined_type_ : 1;
1575 // True if this variable should be put in a unique section. This is
1576 // used for field tracking.
1577 bool in_unique_section_ : 1;
1580 // A variable which is really the name for a function return value, or
1581 // part of one.
1583 class Result_variable
1585 public:
1586 Result_variable(Type* type, Function* function, int index,
1587 Location location)
1588 : type_(type), function_(function), index_(index), location_(location),
1589 backend_(NULL), is_address_taken_(false),
1590 is_non_escaping_address_taken_(false)
1593 // Get the type of the result variable.
1594 Type*
1595 type() const
1596 { return this->type_; }
1598 // Get the function that this is associated with.
1599 Function*
1600 function() const
1601 { return this->function_; }
1603 // Index in the list of function results.
1605 index() const
1606 { return this->index_; }
1608 // The location of the variable definition.
1609 Location
1610 location() const
1611 { return this->location_; }
1613 // Whether this variable's address is taken.
1614 bool
1615 is_address_taken() const
1616 { return this->is_address_taken_; }
1618 // Note that something takes the address of this variable.
1619 void
1620 set_address_taken()
1621 { this->is_address_taken_ = true; }
1623 // Return whether the address is taken but does not escape.
1624 bool
1625 is_non_escaping_address_taken() const
1626 { return this->is_non_escaping_address_taken_; }
1628 // Note that something takes the address of this variable such that
1629 // the address does not escape the function.
1630 void
1631 set_non_escaping_address_taken()
1632 { this->is_non_escaping_address_taken_ = true; }
1634 // Whether this variable should live in the heap.
1635 bool
1636 is_in_heap() const
1637 { return this->is_address_taken_; }
1639 // Set the function. This is used when cloning functions which call
1640 // recover.
1641 void
1642 set_function(Function* function)
1643 { this->function_ = function; }
1645 // Get the backend representation of the variable.
1646 Bvariable*
1647 get_backend_variable(Gogo*, Named_object*, const std::string&);
1649 private:
1650 // Type of result variable.
1651 Type* type_;
1652 // Function with which this is associated.
1653 Function* function_;
1654 // Index in list of results.
1655 int index_;
1656 // Where the result variable is defined.
1657 Location location_;
1658 // Backend representation.
1659 Bvariable* backend_;
1660 // Whether something takes the address of this variable.
1661 bool is_address_taken_;
1662 // Whether something takes the address of this variable such that
1663 // the address does not escape the function.
1664 bool is_non_escaping_address_taken_;
1667 // The value we keep for a named constant. This lets us hold a type
1668 // and an expression.
1670 class Named_constant
1672 public:
1673 Named_constant(Type* type, Expression* expr, int iota_value,
1674 Location location)
1675 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1676 lowering_(false), is_sink_(false), bconst_(NULL)
1679 Type*
1680 type() const
1681 { return this->type_; }
1683 Expression*
1684 expr() const
1685 { return this->expr_; }
1688 iota_value() const
1689 { return this->iota_value_; }
1691 Location
1692 location() const
1693 { return this->location_; }
1695 // Whether we are lowering.
1696 bool
1697 lowering() const
1698 { return this->lowering_; }
1700 // Set that we are lowering.
1701 void
1702 set_lowering()
1703 { this->lowering_ = true; }
1705 // We are no longer lowering.
1706 void
1707 clear_lowering()
1708 { this->lowering_ = false; }
1710 bool
1711 is_sink() const
1712 { return this->is_sink_; }
1714 void
1715 set_is_sink()
1716 { this->is_sink_ = true; }
1718 // Traverse the expression.
1720 traverse_expression(Traverse*);
1722 // Determine the type of the constant if necessary.
1723 void
1724 determine_type();
1726 // Indicate that we found and reported an error for this constant.
1727 void
1728 set_error();
1730 // Export the constant.
1731 void
1732 export_const(Export*, const std::string& name) const;
1734 // Import a constant.
1735 static void
1736 import_const(Import*, std::string*, Type**, Expression**);
1738 // Get the backend representation of the constant value.
1739 Bexpression*
1740 get_backend(Gogo*, Named_object*);
1742 private:
1743 // The type of the constant.
1744 Type* type_;
1745 // The expression for the constant.
1746 Expression* expr_;
1747 // If the predeclared constant iota is used in EXPR_, this is the
1748 // value it will have. We do this because at parse time we don't
1749 // know whether the name "iota" will refer to the predeclared
1750 // constant or to something else. We put in the right value in when
1751 // we lower.
1752 int iota_value_;
1753 // The location of the definition.
1754 Location location_;
1755 // Whether we are currently lowering this constant.
1756 bool lowering_;
1757 // Whether this constant is blank named and needs only type checking.
1758 bool is_sink_;
1759 // The backend representation of the constant value.
1760 Bexpression* bconst_;
1763 // A type declaration.
1765 class Type_declaration
1767 public:
1768 Type_declaration(Location location)
1769 : location_(location), in_function_(NULL), in_function_index_(0),
1770 methods_(), issued_warning_(false)
1773 // Return the location.
1774 Location
1775 location() const
1776 { return this->location_; }
1778 // Return the function in which this type is declared. This will
1779 // return NULL for a type declared in global scope.
1780 Named_object*
1781 in_function(unsigned int* pindex)
1783 *pindex = this->in_function_index_;
1784 return this->in_function_;
1787 // Set the function in which this type is declared.
1788 void
1789 set_in_function(Named_object* f, unsigned int index)
1791 this->in_function_ = f;
1792 this->in_function_index_ = index;
1795 // Add a method to this type. This is used when methods are defined
1796 // before the type.
1797 Named_object*
1798 add_method(const std::string& name, Function* function);
1800 // Add a method declaration to this type.
1801 Named_object*
1802 add_method_declaration(const std::string& name, Package*,
1803 Function_type* type, Location location);
1805 // Return whether any methods were defined.
1806 bool
1807 has_methods() const;
1809 // Return the methods.
1810 const std::vector<Named_object*>*
1811 methods() const
1812 { return &this->methods_; }
1814 // Define methods when the real type is known.
1815 void
1816 define_methods(Named_type*);
1818 // This is called if we are trying to use this type. It returns
1819 // true if we should issue a warning.
1820 bool
1821 using_type();
1823 private:
1824 // The location of the type declaration.
1825 Location location_;
1826 // If this type is declared in a function, a pointer back to the
1827 // function in which it is defined.
1828 Named_object* in_function_;
1829 // The index of this type in IN_FUNCTION_.
1830 unsigned int in_function_index_;
1831 // Methods defined before the type is defined.
1832 std::vector<Named_object*> methods_;
1833 // True if we have issued a warning about a use of this type
1834 // declaration when it is undefined.
1835 bool issued_warning_;
1838 // An unknown object. These are created by the parser for forward
1839 // references to names which have not been seen before. In a correct
1840 // program, these will always point to a real definition by the end of
1841 // the parse. Because they point to another Named_object, these may
1842 // only be referenced by Unknown_expression objects.
1844 class Unknown_name
1846 public:
1847 Unknown_name(Location location)
1848 : location_(location), real_named_object_(NULL)
1851 // Return the location where this name was first seen.
1852 Location
1853 location() const
1854 { return this->location_; }
1856 // Return the real named object that this points to, or NULL if it
1857 // was never resolved.
1858 Named_object*
1859 real_named_object() const
1860 { return this->real_named_object_; }
1862 // Set the real named object that this points to.
1863 void
1864 set_real_named_object(Named_object* no);
1866 private:
1867 // The location where this name was first seen.
1868 Location location_;
1869 // The real named object when it is known.
1870 Named_object*
1871 real_named_object_;
1874 // A named object named. This is the result of a declaration. We
1875 // don't use a superclass because they all have to be handled
1876 // differently.
1878 class Named_object
1880 public:
1881 enum Classification
1883 // An uninitialized Named_object. We should never see this.
1884 NAMED_OBJECT_UNINITIALIZED,
1885 // An erroneous name. This indicates a parse error, to avoid
1886 // later errors about undefined references.
1887 NAMED_OBJECT_ERRONEOUS,
1888 // An unknown name. This is used for forward references. In a
1889 // correct program, these will all be resolved by the end of the
1890 // parse.
1891 NAMED_OBJECT_UNKNOWN,
1892 // A const.
1893 NAMED_OBJECT_CONST,
1894 // A type.
1895 NAMED_OBJECT_TYPE,
1896 // A forward type declaration.
1897 NAMED_OBJECT_TYPE_DECLARATION,
1898 // A var.
1899 NAMED_OBJECT_VAR,
1900 // A result variable in a function.
1901 NAMED_OBJECT_RESULT_VAR,
1902 // The blank identifier--the special variable named _.
1903 NAMED_OBJECT_SINK,
1904 // A func.
1905 NAMED_OBJECT_FUNC,
1906 // A forward func declaration.
1907 NAMED_OBJECT_FUNC_DECLARATION,
1908 // A package.
1909 NAMED_OBJECT_PACKAGE
1912 // Return the classification.
1913 Classification
1914 classification() const
1915 { return this->classification_; }
1917 // Classifiers.
1919 bool
1920 is_erroneous() const
1921 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1923 bool
1924 is_unknown() const
1925 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1927 bool
1928 is_const() const
1929 { return this->classification_ == NAMED_OBJECT_CONST; }
1931 bool
1932 is_type() const
1933 { return this->classification_ == NAMED_OBJECT_TYPE; }
1935 bool
1936 is_type_declaration() const
1937 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1939 bool
1940 is_variable() const
1941 { return this->classification_ == NAMED_OBJECT_VAR; }
1943 bool
1944 is_result_variable() const
1945 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1947 bool
1948 is_sink() const
1949 { return this->classification_ == NAMED_OBJECT_SINK; }
1951 bool
1952 is_function() const
1953 { return this->classification_ == NAMED_OBJECT_FUNC; }
1955 bool
1956 is_function_declaration() const
1957 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1959 bool
1960 is_package() const
1961 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1963 // Creators.
1965 static Named_object*
1966 make_erroneous_name(const std::string& name)
1967 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1969 static Named_object*
1970 make_unknown_name(const std::string& name, Location);
1972 static Named_object*
1973 make_constant(const Typed_identifier&, const Package*, Expression*,
1974 int iota_value);
1976 static Named_object*
1977 make_type(const std::string&, const Package*, Type*, Location);
1979 static Named_object*
1980 make_type_declaration(const std::string&, const Package*, Location);
1982 static Named_object*
1983 make_variable(const std::string&, const Package*, Variable*);
1985 static Named_object*
1986 make_result_variable(const std::string&, Result_variable*);
1988 static Named_object*
1989 make_sink();
1991 static Named_object*
1992 make_function(const std::string&, const Package*, Function*);
1994 static Named_object*
1995 make_function_declaration(const std::string&, const Package*, Function_type*,
1996 Location);
1998 static Named_object*
1999 make_package(const std::string& alias, Package* package);
2001 // Getters.
2003 Unknown_name*
2004 unknown_value()
2006 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2007 return this->u_.unknown_value;
2010 const Unknown_name*
2011 unknown_value() const
2013 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2014 return this->u_.unknown_value;
2017 Named_constant*
2018 const_value()
2020 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2021 return this->u_.const_value;
2024 const Named_constant*
2025 const_value() const
2027 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2028 return this->u_.const_value;
2031 Named_type*
2032 type_value()
2034 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2035 return this->u_.type_value;
2038 const Named_type*
2039 type_value() const
2041 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2042 return this->u_.type_value;
2045 Type_declaration*
2046 type_declaration_value()
2048 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2049 return this->u_.type_declaration;
2052 const Type_declaration*
2053 type_declaration_value() const
2055 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2056 return this->u_.type_declaration;
2059 Variable*
2060 var_value()
2062 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2063 return this->u_.var_value;
2066 const Variable*
2067 var_value() const
2069 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2070 return this->u_.var_value;
2073 Result_variable*
2074 result_var_value()
2076 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2077 return this->u_.result_var_value;
2080 const Result_variable*
2081 result_var_value() const
2083 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2084 return this->u_.result_var_value;
2087 Function*
2088 func_value()
2090 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2091 return this->u_.func_value;
2094 const Function*
2095 func_value() const
2097 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2098 return this->u_.func_value;
2101 Function_declaration*
2102 func_declaration_value()
2104 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2105 return this->u_.func_declaration_value;
2108 const Function_declaration*
2109 func_declaration_value() const
2111 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2112 return this->u_.func_declaration_value;
2115 Package*
2116 package_value()
2118 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2119 return this->u_.package_value;
2122 const Package*
2123 package_value() const
2125 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2126 return this->u_.package_value;
2129 const std::string&
2130 name() const
2131 { return this->name_; }
2133 // Return the name to use in an error message. The difference is
2134 // that if this Named_object is defined in a different package, this
2135 // will return PACKAGE.NAME.
2136 std::string
2137 message_name() const;
2139 const Package*
2140 package() const
2141 { return this->package_; }
2143 // Resolve an unknown value if possible. This returns the same
2144 // Named_object or a new one.
2145 Named_object*
2146 resolve()
2148 Named_object* ret = this;
2149 if (this->is_unknown())
2151 Named_object* r = this->unknown_value()->real_named_object();
2152 if (r != NULL)
2153 ret = r;
2155 return ret;
2158 const Named_object*
2159 resolve() const
2161 const Named_object* ret = this;
2162 if (this->is_unknown())
2164 const Named_object* r = this->unknown_value()->real_named_object();
2165 if (r != NULL)
2166 ret = r;
2168 return ret;
2171 // The location where this object was defined or referenced.
2172 Location
2173 location() const;
2175 // Convert a variable to the backend representation.
2176 Bvariable*
2177 get_backend_variable(Gogo*, Named_object* function);
2179 // Return the external identifier for this object.
2180 std::string
2181 get_id(Gogo*);
2183 // Get the backend representation of this object.
2184 void
2185 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2186 std::vector<Bfunction*>&);
2188 // Define a type declaration.
2189 void
2190 set_type_value(Named_type*);
2192 // Define a function declaration.
2193 void
2194 set_function_value(Function*);
2196 // Declare an unknown name as a type declaration.
2197 void
2198 declare_as_type();
2200 // Export this object.
2201 void
2202 export_named_object(Export*) const;
2204 private:
2205 Named_object(const std::string&, const Package*, Classification);
2207 // The name of the object.
2208 std::string name_;
2209 // The package that this object is in. This is NULL if it is in the
2210 // file we are compiling.
2211 const Package* package_;
2212 // The type of object this is.
2213 Classification classification_;
2214 // The real data.
2215 union
2217 Unknown_name* unknown_value;
2218 Named_constant* const_value;
2219 Named_type* type_value;
2220 Type_declaration* type_declaration;
2221 Variable* var_value;
2222 Result_variable* result_var_value;
2223 Function* func_value;
2224 Function_declaration* func_declaration_value;
2225 Package* package_value;
2226 } u_;
2229 // A binding contour. This binds names to objects.
2231 class Bindings
2233 public:
2234 // Type for mapping from names to objects.
2235 typedef Unordered_map(std::string, Named_object*) Contour;
2237 Bindings(Bindings* enclosing);
2239 // Add an erroneous name.
2240 Named_object*
2241 add_erroneous_name(const std::string& name)
2242 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2244 // Add an unknown name.
2245 Named_object*
2246 add_unknown_name(const std::string& name, Location location)
2248 return this->add_named_object(Named_object::make_unknown_name(name,
2249 location));
2252 // Add a constant.
2253 Named_object*
2254 add_constant(const Typed_identifier& tid, const Package* package,
2255 Expression* expr, int iota_value)
2257 return this->add_named_object(Named_object::make_constant(tid, package,
2258 expr,
2259 iota_value));
2262 // Add a type.
2263 Named_object*
2264 add_type(const std::string& name, const Package* package, Type* type,
2265 Location location)
2267 return this->add_named_object(Named_object::make_type(name, package, type,
2268 location));
2271 // Add a named type. This is used for builtin types, and to add an
2272 // imported type to the global scope.
2273 Named_object*
2274 add_named_type(Named_type* named_type);
2276 // Add a type declaration.
2277 Named_object*
2278 add_type_declaration(const std::string& name, const Package* package,
2279 Location location)
2281 Named_object* no = Named_object::make_type_declaration(name, package,
2282 location);
2283 return this->add_named_object(no);
2286 // Add a variable.
2287 Named_object*
2288 add_variable(const std::string& name, const Package* package,
2289 Variable* variable)
2291 return this->add_named_object(Named_object::make_variable(name, package,
2292 variable));
2295 // Add a result variable.
2296 Named_object*
2297 add_result_variable(const std::string& name, Result_variable* result)
2299 return this->add_named_object(Named_object::make_result_variable(name,
2300 result));
2303 // Add a function.
2304 Named_object*
2305 add_function(const std::string& name, const Package*, Function* function);
2307 // Add a function declaration.
2308 Named_object*
2309 add_function_declaration(const std::string& name, const Package* package,
2310 Function_type* type, Location location);
2312 // Add a package. The location is the location of the import
2313 // statement.
2314 Named_object*
2315 add_package(const std::string& alias, Package* package)
2317 Named_object* no = Named_object::make_package(alias, package);
2318 return this->add_named_object(no);
2321 // Define a type which was already declared.
2322 void
2323 define_type(Named_object*, Named_type*);
2325 // Add a method to the list of objects. This is not added to the
2326 // lookup table.
2327 void
2328 add_method(Named_object*);
2330 // Add a named object to this binding.
2331 Named_object*
2332 add_named_object(Named_object* no)
2333 { return this->add_named_object_to_contour(&this->bindings_, no); }
2335 // Clear all names in file scope from the bindings.
2336 void
2337 clear_file_scope(Gogo*);
2339 // Look up a name in this binding contour and in any enclosing
2340 // binding contours. This returns NULL if the name is not found.
2341 Named_object*
2342 lookup(const std::string&) const;
2344 // Look up a name in this binding contour without looking in any
2345 // enclosing binding contours. Returns NULL if the name is not found.
2346 Named_object*
2347 lookup_local(const std::string&) const;
2349 // Remove a name.
2350 void
2351 remove_binding(Named_object*);
2353 // Mark all variables as used. This is used for some types of parse
2354 // error.
2355 void
2356 mark_locals_used();
2358 // Traverse the tree. See the Traverse class.
2360 traverse(Traverse*, bool is_global);
2362 // Iterate over definitions. This does not include things which
2363 // were only declared.
2365 typedef std::vector<Named_object*>::const_iterator
2366 const_definitions_iterator;
2368 const_definitions_iterator
2369 begin_definitions() const
2370 { return this->named_objects_.begin(); }
2372 const_definitions_iterator
2373 end_definitions() const
2374 { return this->named_objects_.end(); }
2376 // Return the number of definitions.
2377 size_t
2378 size_definitions() const
2379 { return this->named_objects_.size(); }
2381 // Return whether there are no definitions.
2382 bool
2383 empty_definitions() const
2384 { return this->named_objects_.empty(); }
2386 // Iterate over declarations. This is everything that has been
2387 // declared, which includes everything which has been defined.
2389 typedef Contour::const_iterator const_declarations_iterator;
2391 const_declarations_iterator
2392 begin_declarations() const
2393 { return this->bindings_.begin(); }
2395 const_declarations_iterator
2396 end_declarations() const
2397 { return this->bindings_.end(); }
2399 // Return the number of declarations.
2400 size_t
2401 size_declarations() const
2402 { return this->bindings_.size(); }
2404 // Return whether there are no declarations.
2405 bool
2406 empty_declarations() const
2407 { return this->bindings_.empty(); }
2409 // Return the first declaration.
2410 Named_object*
2411 first_declaration()
2412 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2414 private:
2415 Named_object*
2416 add_named_object_to_contour(Contour*, Named_object*);
2418 Named_object*
2419 new_definition(Named_object*, Named_object*);
2421 // Enclosing bindings.
2422 Bindings* enclosing_;
2423 // The list of objects.
2424 std::vector<Named_object*> named_objects_;
2425 // The mapping from names to objects.
2426 Contour bindings_;
2429 // A label.
2431 class Label
2433 public:
2434 Label(const std::string& name)
2435 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2436 refs_(), is_used_(false), blabel_(NULL)
2439 // Return the label's name.
2440 const std::string&
2441 name() const
2442 { return this->name_; }
2444 // Return whether the label has been defined.
2445 bool
2446 is_defined() const
2447 { return !Linemap::is_unknown_location(this->location_); }
2449 // Return whether the label has been used.
2450 bool
2451 is_used() const
2452 { return this->is_used_; }
2454 // Record that the label is used.
2455 void
2456 set_is_used()
2457 { this->is_used_ = true; }
2459 // Return the location of the definition.
2460 Location
2461 location() const
2462 { return this->location_; }
2464 // Return the bindings snapshot.
2465 Bindings_snapshot*
2466 snapshot() const
2467 { return this->snapshot_; }
2469 // Add a snapshot of a goto which refers to this label.
2470 void
2471 add_snapshot_ref(Bindings_snapshot* snapshot)
2473 go_assert(Linemap::is_unknown_location(this->location_));
2474 this->refs_.push_back(snapshot);
2477 // Return the list of snapshots of goto statements which refer to
2478 // this label.
2479 const std::vector<Bindings_snapshot*>&
2480 refs() const
2481 { return this->refs_; }
2483 // Clear the references.
2484 void
2485 clear_refs();
2487 // Define the label at LOCATION with the given bindings snapshot.
2488 void
2489 define(Location location, Bindings_snapshot* snapshot)
2491 go_assert(Linemap::is_unknown_location(this->location_)
2492 && this->snapshot_ == NULL);
2493 this->location_ = location;
2494 this->snapshot_ = snapshot;
2497 // Return the backend representation for this label.
2498 Blabel*
2499 get_backend_label(Translate_context*);
2501 // Return an expression for the address of this label. This is used
2502 // to get the return address of a deferred function to see whether
2503 // the function may call recover.
2504 Bexpression*
2505 get_addr(Translate_context*, Location location);
2507 private:
2508 // The name of the label.
2509 std::string name_;
2510 // The location of the definition. This is 0 if the label has not
2511 // yet been defined.
2512 Location location_;
2513 // A snapshot of the set of bindings defined at this label, used to
2514 // issue errors about invalid goto statements.
2515 Bindings_snapshot* snapshot_;
2516 // A list of snapshots of goto statements which refer to this label.
2517 std::vector<Bindings_snapshot*> refs_;
2518 // Whether the label has been used.
2519 bool is_used_;
2520 // The backend representation.
2521 Blabel* blabel_;
2524 // An unnamed label. These are used when lowering loops.
2526 class Unnamed_label
2528 public:
2529 Unnamed_label(Location location)
2530 : location_(location), blabel_(NULL)
2533 // Get the location where the label is defined.
2534 Location
2535 location() const
2536 { return this->location_; }
2538 // Set the location where the label is defined.
2539 void
2540 set_location(Location location)
2541 { this->location_ = location; }
2543 // Return a statement which defines this label.
2544 Bstatement*
2545 get_definition(Translate_context*);
2547 // Return a goto to this label from LOCATION.
2548 Bstatement*
2549 get_goto(Translate_context*, Location location);
2551 private:
2552 // Return the backend representation.
2553 Blabel*
2554 get_blabel(Translate_context*);
2556 // The location where the label is defined.
2557 Location location_;
2558 // The backend representation of this label.
2559 Blabel* blabel_;
2562 // An imported package.
2564 class Package
2566 public:
2567 Package(const std::string& pkgpath, Location location);
2569 // Get the package path used for all symbols exported from this
2570 // package.
2571 const std::string&
2572 pkgpath() const
2573 { return this->pkgpath_; }
2575 // Return the package path to use for a symbol name.
2576 const std::string&
2577 pkgpath_symbol() const
2578 { return this->pkgpath_symbol_; }
2580 // Return the location of the import statement.
2581 Location
2582 location() const
2583 { return this->location_; }
2585 // Return whether we know the name of this package yet.
2586 bool
2587 has_package_name() const
2588 { return !this->package_name_.empty(); }
2590 // The name that this package uses in its package clause. This may
2591 // be different from the name in the associated Named_object if the
2592 // import statement used an alias.
2593 const std::string&
2594 package_name() const
2596 go_assert(!this->package_name_.empty());
2597 return this->package_name_;
2600 // The priority of this package. The init function of packages with
2601 // lower priority must be run before the init function of packages
2602 // with higher priority.
2604 priority() const
2605 { return this->priority_; }
2607 // Set the priority.
2608 void
2609 set_priority(int priority);
2611 // Return the bindings.
2612 Bindings*
2613 bindings()
2614 { return this->bindings_; }
2616 // Whether some symbol from the package was used.
2617 bool
2618 used() const
2619 { return this->used_; }
2621 // Note that some symbol from this package was used.
2622 void
2623 set_used() const
2624 { this->used_ = true; }
2626 // Clear the used field for the next file.
2627 void
2628 clear_used()
2629 { this->used_ = false; }
2631 // Whether this package was imported in the current file.
2632 bool
2633 is_imported() const
2634 { return this->is_imported_; }
2636 // Note that this package was imported in the current file.
2637 void
2638 set_is_imported()
2639 { this->is_imported_ = true; }
2641 // Clear the imported field for the next file.
2642 void
2643 clear_is_imported()
2644 { this->is_imported_ = false; }
2646 // Whether this package was imported with a name of "_".
2647 bool
2648 uses_sink_alias() const
2649 { return this->uses_sink_alias_; }
2651 // Note that this package was imported with a name of "_".
2652 void
2653 set_uses_sink_alias()
2654 { this->uses_sink_alias_ = true; }
2656 // Clear the sink alias field for the next file.
2657 void
2658 clear_uses_sink_alias()
2659 { this->uses_sink_alias_ = false; }
2661 // Look up a name in the package. Returns NULL if the name is not
2662 // found.
2663 Named_object*
2664 lookup(const std::string& name) const
2665 { return this->bindings_->lookup(name); }
2667 // Set the name of the package.
2668 void
2669 set_package_name(const std::string& name, Location);
2671 // Set the location of the package. This is used to record the most
2672 // recent import location.
2673 void
2674 set_location(Location location)
2675 { this->location_ = location; }
2677 // Add a constant to the package.
2678 Named_object*
2679 add_constant(const Typed_identifier& tid, Expression* expr)
2680 { return this->bindings_->add_constant(tid, this, expr, 0); }
2682 // Add a type to the package.
2683 Named_object*
2684 add_type(const std::string& name, Type* type, Location location)
2685 { return this->bindings_->add_type(name, this, type, location); }
2687 // Add a type declaration to the package.
2688 Named_object*
2689 add_type_declaration(const std::string& name, Location location)
2690 { return this->bindings_->add_type_declaration(name, this, location); }
2692 // Add a variable to the package.
2693 Named_object*
2694 add_variable(const std::string& name, Variable* variable)
2695 { return this->bindings_->add_variable(name, this, variable); }
2697 // Add a function declaration to the package.
2698 Named_object*
2699 add_function_declaration(const std::string& name, Function_type* type,
2700 Location loc)
2701 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2703 // Determine types of constants.
2704 void
2705 determine_types();
2707 private:
2708 // The package path for type reflection data.
2709 std::string pkgpath_;
2710 // The package path for symbol names.
2711 std::string pkgpath_symbol_;
2712 // The name that this package uses in the package clause. This may
2713 // be the empty string if it is not yet known.
2714 std::string package_name_;
2715 // The names in this package.
2716 Bindings* bindings_;
2717 // The priority of this package. A package has a priority higher
2718 // than the priority of all of the packages that it imports. This
2719 // is used to run init functions in the right order.
2720 int priority_;
2721 // The location of the import statement.
2722 Location location_;
2723 // True if some name from this package was used. This is mutable
2724 // because we can use a package even if we have a const pointer to
2725 // it.
2726 mutable bool used_;
2727 // True if this package was imported in the current file.
2728 bool is_imported_;
2729 // True if this package was imported with a name of "_".
2730 bool uses_sink_alias_;
2733 // Return codes for the traversal functions. This is not an enum
2734 // because we want to be able to declare traversal functions in other
2735 // header files without including this one.
2737 // Continue traversal as usual.
2738 const int TRAVERSE_CONTINUE = -1;
2740 // Exit traversal.
2741 const int TRAVERSE_EXIT = 0;
2743 // Continue traversal, but skip components of the current object.
2744 // E.g., if this is returned by Traverse::statement, we do not
2745 // traverse the expressions in the statement even if
2746 // traverse_expressions is set in the traverse_mask.
2747 const int TRAVERSE_SKIP_COMPONENTS = 1;
2749 // This class is used when traversing the parse tree. The caller uses
2750 // a subclass which overrides functions as desired.
2752 class Traverse
2754 public:
2755 // These bitmasks say what to traverse.
2756 static const unsigned int traverse_variables = 0x1;
2757 static const unsigned int traverse_constants = 0x2;
2758 static const unsigned int traverse_functions = 0x4;
2759 static const unsigned int traverse_blocks = 0x8;
2760 static const unsigned int traverse_statements = 0x10;
2761 static const unsigned int traverse_expressions = 0x20;
2762 static const unsigned int traverse_types = 0x40;
2764 Traverse(unsigned int traverse_mask)
2765 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2768 virtual ~Traverse();
2770 // The bitmask of what to traverse.
2771 unsigned int
2772 traverse_mask() const
2773 { return this->traverse_mask_; }
2775 // Record that we are going to traverse a type. This returns true
2776 // if the type has already been seen in this traversal. This is
2777 // required because types, unlike expressions, can form a circular
2778 // graph.
2779 bool
2780 remember_type(const Type*);
2782 // Record that we are going to see an expression. This returns true
2783 // if the expression has already been seen in this traversal. This
2784 // is only needed for cases where multiple expressions can point to
2785 // a single one.
2786 bool
2787 remember_expression(const Expression*);
2789 // These functions return one of the TRAVERSE codes defined above.
2791 // If traverse_variables is set in the mask, this is called for
2792 // every variable in the tree.
2793 virtual int
2794 variable(Named_object*);
2796 // If traverse_constants is set in the mask, this is called for
2797 // every named constant in the tree. The bool parameter is true for
2798 // a global constant.
2799 virtual int
2800 constant(Named_object*, bool);
2802 // If traverse_functions is set in the mask, this is called for
2803 // every function in the tree.
2804 virtual int
2805 function(Named_object*);
2807 // If traverse_blocks is set in the mask, this is called for every
2808 // block in the tree.
2809 virtual int
2810 block(Block*);
2812 // If traverse_statements is set in the mask, this is called for
2813 // every statement in the tree.
2814 virtual int
2815 statement(Block*, size_t* index, Statement*);
2817 // If traverse_expressions is set in the mask, this is called for
2818 // every expression in the tree.
2819 virtual int
2820 expression(Expression**);
2822 // If traverse_types is set in the mask, this is called for every
2823 // type in the tree.
2824 virtual int
2825 type(Type*);
2827 private:
2828 // A hash table for types we have seen during this traversal. Note
2829 // that this uses the default hash functions for pointers rather
2830 // than Type_hash_identical and Type_identical. This is because for
2831 // traversal we care about seeing a specific type structure. If
2832 // there are two separate instances of identical types, we want to
2833 // traverse both.
2834 typedef Unordered_set(const Type*) Types_seen;
2836 typedef Unordered_set(const Expression*) Expressions_seen;
2838 // Bitmask of what sort of objects to traverse.
2839 unsigned int traverse_mask_;
2840 // Types which have been seen in this traversal.
2841 Types_seen* types_seen_;
2842 // Expressions which have been seen in this traversal.
2843 Expressions_seen* expressions_seen_;
2846 // A class which makes it easier to insert new statements before the
2847 // current statement during a traversal.
2849 class Statement_inserter
2851 public:
2852 // Empty constructor.
2853 Statement_inserter()
2854 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2857 // Constructor for a statement in a block.
2858 Statement_inserter(Block* block, size_t *pindex)
2859 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2862 // Constructor for a global variable.
2863 Statement_inserter(Gogo* gogo, Variable* var)
2864 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2865 { go_assert(var->is_global()); }
2867 // We use the default copy constructor and assignment operator.
2869 // Insert S before the statement we are traversing, or before the
2870 // initialization expression of a global variable.
2871 void
2872 insert(Statement* s);
2874 private:
2875 // The block that the statement is in.
2876 Block* block_;
2877 // The index of the statement that we are traversing.
2878 size_t* pindex_;
2879 // The IR, needed when looking at an initializer expression for a
2880 // global variable.
2881 Gogo* gogo_;
2882 // The global variable, when looking at an initializer expression.
2883 Variable* var_;
2886 // When translating the gogo IR into the backend data structure, this
2887 // is the context we pass down the blocks and statements.
2889 class Translate_context
2891 public:
2892 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2893 Bblock* bblock)
2894 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2895 block_(block), bblock_(bblock), is_const_(false)
2898 // Accessors.
2900 Gogo*
2901 gogo()
2902 { return this->gogo_; }
2904 Backend*
2905 backend()
2906 { return this->backend_; }
2908 Named_object*
2909 function()
2910 { return this->function_; }
2912 Block*
2913 block()
2914 { return this->block_; }
2916 Bblock*
2917 bblock()
2918 { return this->bblock_; }
2920 bool
2921 is_const()
2922 { return this->is_const_; }
2924 // Make a constant context.
2925 void
2926 set_is_const()
2927 { this->is_const_ = true; }
2929 private:
2930 // The IR for the entire compilation unit.
2931 Gogo* gogo_;
2932 // The generator for the backend data structures.
2933 Backend* backend_;
2934 // The function we are currently translating. NULL if not in a
2935 // function, e.g., the initializer of a global variable.
2936 Named_object* function_;
2937 // The block we are currently translating. NULL if not in a
2938 // function.
2939 Block *block_;
2940 // The backend representation of the current block. NULL if block_
2941 // is NULL.
2942 Bblock* bblock_;
2943 // Whether this is being evaluated in a constant context. This is
2944 // used for type descriptor initializers.
2945 bool is_const_;
2948 // Runtime error codes. These must match the values in
2949 // libgo/runtime/go-runtime-error.c.
2951 // Slice index out of bounds: negative or larger than the length of
2952 // the slice.
2953 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2955 // Array index out of bounds.
2956 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2958 // String index out of bounds.
2959 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2961 // Slice slice out of bounds: negative or larger than the length of
2962 // the slice or high bound less than low bound.
2963 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2965 // Array slice out of bounds.
2966 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2968 // String slice out of bounds.
2969 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2971 // Dereference of nil pointer. This is used when there is a
2972 // dereference of a pointer to a very large struct or array, to ensure
2973 // that a gigantic array is not used a proxy to access random memory
2974 // locations.
2975 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2977 // Slice length or capacity out of bounds in make: negative or
2978 // overflow or length greater than capacity.
2979 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2981 // Map capacity out of bounds in make: negative or overflow.
2982 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2984 // Channel capacity out of bounds in make: negative or overflow.
2985 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2987 // Division by zero.
2988 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2990 // This is used by some of the langhooks.
2991 extern Gogo* go_get_gogo();
2993 // Whether we have seen any errors. FIXME: Replace with a backend
2994 // interface.
2995 extern bool saw_errors();
2997 #endif // !defined(GO_GOGO_H)