compiler: Only make function descriptors if needed.
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob4a840758cac7c4384658e33f5db1a90ffa894571
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 Bstatement;
48 class Bblock;
49 class Bvariable;
50 class Blabel;
52 // This file declares the basic classes used to hold the internal
53 // representation of Go which is built by the parser.
55 // An initialization function for an imported package. This is a
56 // magic function which initializes variables and runs the "init"
57 // function.
59 class Import_init
61 public:
62 Import_init(const std::string& package_name, const std::string& init_name,
63 int priority)
64 : package_name_(package_name), init_name_(init_name), priority_(priority)
65 { }
67 // The name of the package being imported.
68 const std::string&
69 package_name() const
70 { return this->package_name_; }
72 // The name of the package's init function.
73 const std::string&
74 init_name() const
75 { return this->init_name_; }
77 // The priority of the initialization function. Functions with a
78 // lower priority number must be run first.
79 int
80 priority() const
81 { return this->priority_; }
83 private:
84 // The name of the package being imported.
85 std::string package_name_;
86 // The name of the package's init function.
87 std::string init_name_;
88 // The priority.
89 int priority_;
92 // For sorting purposes.
94 inline bool
95 operator<(const Import_init& i1, const Import_init& i2)
97 if (i1.priority() < i2.priority())
98 return true;
99 if (i1.priority() > i2.priority())
100 return false;
101 if (i1.package_name() != i2.package_name())
102 return i1.package_name() < i2.package_name();
103 return i1.init_name() < i2.init_name();
106 // The holder for the internal representation of the entire
107 // compilation unit.
109 class Gogo
111 public:
112 // Create the IR, passing in the sizes of the types "int" and
113 // "uintptr" in bits.
114 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
116 // Get the backend generator.
117 Backend*
118 backend()
119 { return this->backend_; }
121 // Get the Location generator.
122 Linemap*
123 linemap()
124 { return this->linemap_; }
126 // Get the package name.
127 const std::string&
128 package_name() const;
130 // Set the package name.
131 void
132 set_package_name(const std::string&, Location);
134 // Return whether this is the "main" package.
135 bool
136 is_main_package() const;
138 // If necessary, adjust the name to use for a hidden symbol. We add
139 // the package name, so that hidden symbols in different packages do
140 // not collide.
141 std::string
142 pack_hidden_name(const std::string& name, bool is_exported) const
144 return (is_exported
145 ? name
146 : '.' + this->pkgpath() + '.' + name);
149 // Unpack a name which may have been hidden. Returns the
150 // user-visible name of the object.
151 static std::string
152 unpack_hidden_name(const std::string& name)
153 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
155 // Return whether a possibly packed name is hidden.
156 static bool
157 is_hidden_name(const std::string& name)
158 { return name[0] == '.'; }
160 // Return the package path of a hidden name.
161 static std::string
162 hidden_name_pkgpath(const std::string& name)
164 go_assert(Gogo::is_hidden_name(name));
165 return name.substr(1, name.rfind('.') - 1);
168 // Given a name which may or may not have been hidden, return the
169 // name to use in an error message.
170 static std::string
171 message_name(const std::string& name);
173 // Return whether a name is the blank identifier _.
174 static bool
175 is_sink_name(const std::string& name)
177 return (name[0] == '.'
178 && name[name.length() - 1] == '_'
179 && name[name.length() - 2] == '.');
182 // Convert a pkgpath into a string suitable for a symbol
183 static std::string
184 pkgpath_for_symbol(const std::string& pkgpath);
186 // Return the package path to use for reflect.Type.PkgPath.
187 const std::string&
188 pkgpath() const;
190 // Return the package path to use for a symbol name.
191 const std::string&
192 pkgpath_symbol() const;
194 // Set the package path from a command line option.
195 void
196 set_pkgpath(const std::string&);
198 // Set the prefix from a command line option.
199 void
200 set_prefix(const std::string&);
202 // Return whether pkgpath was set from a command line option.
203 bool
204 pkgpath_from_option() const
205 { return this->pkgpath_from_option_; }
207 // Return the relative import path as set from the command line.
208 // Returns an empty string if it was not set.
209 const std::string&
210 relative_import_path() const
211 { return this->relative_import_path_; }
213 // Set the relative import path from a command line option.
214 void
215 set_relative_import_path(const std::string& s)
216 {this->relative_import_path_ = s; }
218 // Return the priority to use for the package we are compiling.
219 // This is two more than the largest priority of any package we
220 // import.
222 package_priority() const;
224 // Import a package. FILENAME is the file name argument, LOCAL_NAME
225 // is the local name to give to the package. If LOCAL_NAME is empty
226 // the declarations are added to the global scope.
227 void
228 import_package(const std::string& filename, const std::string& local_name,
229 bool is_local_name_exported, Location);
231 // Whether we are the global binding level.
232 bool
233 in_global_scope() const;
235 // Look up a name in the current binding contours.
236 Named_object*
237 lookup(const std::string&, Named_object** pfunction) const;
239 // Look up a name in the current block.
240 Named_object*
241 lookup_in_block(const std::string&) const;
243 // Look up a name in the global namespace--the universal scope.
244 Named_object*
245 lookup_global(const char*) const;
247 // Add a new imported package. REAL_NAME is the real name of the
248 // package. ALIAS is the alias of the package; this may be the same
249 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
250 // this package should be added to the global namespace; this is
251 // true if the alias is ".". LOCATION is the location of the import
252 // statement. This returns the new package, or NULL on error.
253 Package*
254 add_imported_package(const std::string& real_name, const std::string& alias,
255 bool is_alias_exported,
256 const std::string& pkgpath,
257 Location location,
258 bool* padd_to_globals);
260 // Register a package. This package may or may not be imported.
261 // This returns the Package structure for the package, creating if
262 // it necessary.
263 Package*
264 register_package(const std::string& pkgpath, Location);
266 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
267 // method function should be added to the type of its receiver.
268 Named_object*
269 start_function(const std::string& name, Function_type* type,
270 bool add_method_to_type, Location);
272 // Finish compiling a function.
273 void
274 finish_function(Location);
276 // Return the current function.
277 Named_object*
278 current_function() const;
280 // Return the current block.
281 Block*
282 current_block();
284 // Start a new block. This is not initially associated with a
285 // function.
286 void
287 start_block(Location);
289 // Finish the current block and return it.
290 Block*
291 finish_block(Location);
293 // Declare an erroneous name. This is used to avoid knock-on errors
294 // after a parsing error.
295 Named_object*
296 add_erroneous_name(const std::string& name);
298 // Declare an unknown name. This is used while parsing. The name
299 // must be resolved by the end of the parse. Unknown names are
300 // always added at the package level.
301 Named_object*
302 add_unknown_name(const std::string& name, Location);
304 // Declare a function.
305 Named_object*
306 declare_function(const std::string&, Function_type*, Location);
308 // Declare a function at the package level. This is used for
309 // functions generated for a type.
310 Named_object*
311 declare_package_function(const std::string&, Function_type*, Location);
313 // Add a label.
314 Label*
315 add_label_definition(const std::string&, Location);
317 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
318 // report errors for a goto from the current location to the label
319 // location.
320 Label*
321 add_label_reference(const std::string&, Location,
322 bool issue_goto_errors);
324 // Return a snapshot of the current binding state.
325 Bindings_snapshot*
326 bindings_snapshot(Location);
328 // Add a statement to the current block.
329 void
330 add_statement(Statement*);
332 // Add a block to the current block.
333 void
334 add_block(Block*, Location);
336 // Add a constant.
337 Named_object*
338 add_constant(const Typed_identifier&, Expression*, int iota_value);
340 // Add a type.
341 void
342 add_type(const std::string&, Type*, Location);
344 // Add a named type. This is used for builtin types, and to add an
345 // imported type to the global scope.
346 void
347 add_named_type(Named_type*);
349 // Declare a type.
350 Named_object*
351 declare_type(const std::string&, Location);
353 // Declare a type at the package level. This is used when the
354 // parser sees an unknown name where a type name is required.
355 Named_object*
356 declare_package_type(const std::string&, Location);
358 // Define a type which was already declared.
359 void
360 define_type(Named_object*, Named_type*);
362 // Add a variable.
363 Named_object*
364 add_variable(const std::string&, Variable*);
366 // Add a sink--a reference to the blank identifier _.
367 Named_object*
368 add_sink();
370 // Add a type which needs to be verified. This is used for sink
371 // types, just to give appropriate error messages.
372 void
373 add_type_to_verify(Type* type);
375 // Add a named object to the current namespace. This is used for
376 // import . "package".
377 void
378 add_named_object(Named_object*);
380 // Add an identifier to the list of names seen in the file block.
381 void
382 add_file_block_name(const std::string& name, Location location)
383 { this->file_block_names_[name] = location; }
385 // Mark all local variables in current bindings as used. This is
386 // used when there is a parse error to avoid useless errors.
387 void
388 mark_locals_used();
390 // Return a name to use for a thunk function. A thunk function is
391 // one we create during the compilation, for a go statement or a
392 // defer statement or a method expression.
393 static std::string
394 thunk_name();
396 // Return whether an object is a thunk.
397 static bool
398 is_thunk(const Named_object*);
400 // Note that we've seen an interface type. This is used to build
401 // all required interface method tables.
402 void
403 record_interface_type(Interface_type*);
405 // Note that we need an initialization function.
406 void
407 set_need_init_fn()
408 { this->need_init_fn_ = true; }
410 // Clear out all names in file scope. This is called when we start
411 // parsing a new file.
412 void
413 clear_file_scope();
415 // Record that VAR1 must be initialized after VAR2. This is used
416 // when VAR2 does not appear in VAR1's INIT or PREINIT.
417 void
418 record_var_depends_on(Variable* var1, Named_object* var2)
420 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
421 this->var_deps_[var1] = var2;
424 // Return the variable that VAR depends on, or NULL if none.
425 Named_object*
426 var_depends_on(Variable* var) const
428 Var_deps::const_iterator p = this->var_deps_.find(var);
429 return p != this->var_deps_.end() ? p->second : NULL;
432 // Queue up a type-specific function to be written out. This is
433 // used when a type-specific function is needed when not at the top
434 // level.
435 void
436 queue_specific_type_function(Type* type, Named_type* name,
437 const std::string& hash_name,
438 Function_type* hash_fntype,
439 const std::string& equal_name,
440 Function_type* equal_fntype);
442 // Write out queued specific type functions.
443 void
444 write_specific_type_functions();
446 // Whether we are done writing out specific type functions.
447 bool
448 specific_type_functions_are_written() const
449 { return this->specific_type_functions_are_written_; }
451 // Traverse the tree. See the Traverse class.
452 void
453 traverse(Traverse*);
455 // Define the predeclared global names.
456 void
457 define_global_names();
459 // Verify and complete all types.
460 void
461 verify_types();
463 // Lower the parse tree.
464 void
465 lower_parse_tree();
467 // Lower all the statements in a block.
468 void
469 lower_block(Named_object* function, Block*);
471 // Lower an expression.
472 void
473 lower_expression(Named_object* function, Statement_inserter*, Expression**);
475 // Lower a constant.
476 void
477 lower_constant(Named_object*);
479 // Create all necessary function descriptors.
480 void
481 create_function_descriptors();
483 // Finalize the method lists and build stub methods for named types.
484 void
485 finalize_methods();
487 // Work out the types to use for unspecified variables and
488 // constants.
489 void
490 determine_types();
492 // Type check the program.
493 void
494 check_types();
496 // Check the types in a single block. This is used for complicated
497 // go statements.
498 void
499 check_types_in_block(Block*);
501 // Check for return statements.
502 void
503 check_return_statements();
505 // Do all exports.
506 void
507 do_exports();
509 // Add an import control function for an imported package to the
510 // list.
511 void
512 add_import_init_fn(const std::string& package_name,
513 const std::string& init_name, int prio);
515 // Turn short-cut operators (&&, ||) into explicit if statements.
516 void
517 remove_shortcuts();
519 // Use temporary variables to force order of evaluation.
520 void
521 order_evaluations();
523 // Build thunks for functions which call recover.
524 void
525 build_recover_thunks();
527 // Simplify statements which might use thunks: go and defer
528 // statements.
529 void
530 simplify_thunk_statements();
532 // Dump AST if -fgo-dump-ast is set
533 void
534 dump_ast(const char* basename);
536 // Convert named types to the backend representation.
537 void
538 convert_named_types();
540 // Convert named types in a list of bindings.
541 void
542 convert_named_types_in_bindings(Bindings*);
544 // True if named types have been converted to the backend
545 // representation.
546 bool
547 named_types_are_converted() const
548 { return this->named_types_are_converted_; }
550 // Write out the global values.
551 void
552 write_globals();
554 // Create trees for implicit builtin functions.
555 void
556 define_builtin_function_trees();
558 // Build a call to a builtin function. PDECL should point to a NULL
559 // initialized static pointer which will hold the fndecl. NAME is
560 // the name of the function. NARGS is the number of arguments.
561 // RETTYPE is the return type. It is followed by NARGS pairs of
562 // type and argument (both trees).
563 static tree
564 call_builtin(tree* pdecl, Location, const char* name, int nargs,
565 tree rettype, ...);
567 // Build a call to the runtime error function.
568 tree
569 runtime_error(int code, Location);
571 // Build a builtin struct with a list of fields.
572 static tree
573 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
574 int nfields, ...);
576 // Mark a function declaration as a builtin library function.
577 static void
578 mark_fndecl_as_builtin_library(tree fndecl);
580 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
581 // the slice. VALUES points to the values. COUNT is the size,
582 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
583 // COUNT.
584 static tree
585 slice_constructor(tree slice_type_tree, tree values, tree count,
586 tree capacity);
588 // Build required interface method tables.
589 void
590 build_interface_method_tables();
592 // Build an interface method table for a type: a list of function
593 // pointers, one for each interface method. This returns a decl.
594 tree
595 interface_method_table_for_type(const Interface_type*, Type*,
596 bool is_pointer);
598 // Return a tree which allocate SIZE bytes to hold values of type
599 // TYPE.
600 tree
601 allocate_memory(Type *type, tree size, Location);
603 // Return a type to use for pointer to const char.
604 static tree
605 const_char_pointer_type_tree();
607 // Build a string constant with the right type.
608 static tree
609 string_constant_tree(const std::string&);
611 // Build a Go string constant. This returns a pointer to the
612 // constant.
613 tree
614 go_string_constant_tree(const std::string&);
616 // Receive a value from a channel.
617 static tree
618 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
619 Location);
621 private:
622 // During parsing, we keep a stack of functions. Each function on
623 // the stack is one that we are currently parsing. For each
624 // function, we keep track of the current stack of blocks.
625 struct Open_function
627 // The function.
628 Named_object* function;
629 // The stack of active blocks in the function.
630 std::vector<Block*> blocks;
633 // The stack of functions.
634 typedef std::vector<Open_function> Open_functions;
636 // Set up the built-in unsafe package.
637 void
638 import_unsafe(const std::string&, bool is_exported, Location);
640 // Return the current binding contour.
641 Bindings*
642 current_bindings();
644 const Bindings*
645 current_bindings() const;
647 // Get the name of the magic initialization function.
648 const std::string&
649 get_init_fn_name();
651 // Get the decl for the magic initialization function.
652 tree
653 initialization_function_decl();
655 // Write the magic initialization function.
656 void
657 write_initialization_function(tree fndecl, tree init_stmt_list);
659 // Initialize imported packages.
660 void
661 init_imports(tree*);
663 // Register variables with the garbage collector.
664 void
665 register_gc_vars(const std::vector<Named_object*>&, tree*);
667 // Build a pointer to a Go string constant. This returns a pointer
668 // to the pointer.
669 tree
670 ptr_go_string_constant_tree(const std::string&);
672 // Type used to map import names to packages.
673 typedef std::map<std::string, Package*> Imports;
675 // Type used to map package names to packages.
676 typedef std::map<std::string, Package*> Packages;
678 // Type used to map variables to the function calls that set them.
679 // This is used for initialization dependency analysis.
680 typedef std::map<Variable*, Named_object*> Var_deps;
682 // Type used to map identifiers in the file block to the location
683 // where they were defined.
684 typedef Unordered_map(std::string, Location) File_block_names;
686 // Type used to queue writing a type specific function.
687 struct Specific_type_function
689 Type* type;
690 Named_type* name;
691 std::string hash_name;
692 Function_type* hash_fntype;
693 std::string equal_name;
694 Function_type* equal_fntype;
696 Specific_type_function(Type* atype, Named_type* aname,
697 const std::string& ahash_name,
698 Function_type* ahash_fntype,
699 const std::string& aequal_name,
700 Function_type* aequal_fntype)
701 : type(atype), name(aname), hash_name(ahash_name),
702 hash_fntype(ahash_fntype), equal_name(aequal_name),
703 equal_fntype(aequal_fntype)
707 // The backend generator.
708 Backend* backend_;
709 // The object used to keep track of file names and line numbers.
710 Linemap* linemap_;
711 // The package we are compiling.
712 Package* package_;
713 // The list of currently open functions during parsing.
714 Open_functions functions_;
715 // The global binding contour. This includes the builtin functions
716 // and the package we are compiling.
717 Bindings* globals_;
718 // The list of names we have seen in the file block.
719 File_block_names file_block_names_;
720 // Mapping from import file names to packages.
721 Imports imports_;
722 // Whether the magic unsafe package was imported.
723 bool imported_unsafe_;
724 // Mapping from package names we have seen to packages. This does
725 // not include the package we are compiling.
726 Packages packages_;
727 // The functions named "init", if there are any.
728 std::vector<Named_object*> init_functions_;
729 // A mapping from variables to the function calls that initialize
730 // them, if it is not stored in the variable's init or preinit.
731 // This is used for dependency analysis.
732 Var_deps var_deps_;
733 // Whether we need a magic initialization function.
734 bool need_init_fn_;
735 // The name of the magic initialization function.
736 std::string init_fn_name_;
737 // A list of import control variables for packages that we import.
738 std::set<Import_init> imported_init_fns_;
739 // The package path used for reflection data.
740 std::string pkgpath_;
741 // The package path to use for a symbol name.
742 std::string pkgpath_symbol_;
743 // The prefix to use for symbols, from the -fgo-prefix option.
744 std::string prefix_;
745 // Whether pkgpath_ has been set.
746 bool pkgpath_set_;
747 // Whether an explicit package path was set by -fgo-pkgpath.
748 bool pkgpath_from_option_;
749 // Whether an explicit prefix was set by -fgo-prefix.
750 bool prefix_from_option_;
751 // The relative import path, from the -fgo-relative-import-path
752 // option.
753 std::string relative_import_path_;
754 // A list of types to verify.
755 std::vector<Type*> verify_types_;
756 // A list of interface types defined while parsing.
757 std::vector<Interface_type*> interface_types_;
758 // Type specific functions to write out.
759 std::vector<Specific_type_function*> specific_type_functions_;
760 // Whether we are done writing out specific type functions.
761 bool specific_type_functions_are_written_;
762 // Whether named types have been converted.
763 bool named_types_are_converted_;
766 // A block of statements.
768 class Block
770 public:
771 Block(Block* enclosing, Location);
773 // Return the enclosing block.
774 const Block*
775 enclosing() const
776 { return this->enclosing_; }
778 // Return the bindings of the block.
779 Bindings*
780 bindings()
781 { return this->bindings_; }
783 const Bindings*
784 bindings() const
785 { return this->bindings_; }
787 // Look at the block's statements.
788 const std::vector<Statement*>*
789 statements() const
790 { return &this->statements_; }
792 // Return the start location. This is normally the location of the
793 // left curly brace which starts the block.
794 Location
795 start_location() const
796 { return this->start_location_; }
798 // Return the end location. This is normally the location of the
799 // right curly brace which ends the block.
800 Location
801 end_location() const
802 { return this->end_location_; }
804 // Add a statement to the block.
805 void
806 add_statement(Statement*);
808 // Add a statement to the front of the block.
809 void
810 add_statement_at_front(Statement*);
812 // Replace a statement in a block.
813 void
814 replace_statement(size_t index, Statement*);
816 // Add a Statement before statement number INDEX.
817 void
818 insert_statement_before(size_t index, Statement*);
820 // Add a Statement after statement number INDEX.
821 void
822 insert_statement_after(size_t index, Statement*);
824 // Set the end location of the block.
825 void
826 set_end_location(Location location)
827 { this->end_location_ = location; }
829 // Traverse the tree.
831 traverse(Traverse*);
833 // Set final types for unspecified variables and constants.
834 void
835 determine_types();
837 // Return true if execution of this block may fall through to the
838 // next block.
839 bool
840 may_fall_through() const;
842 // Convert the block to the backend representation.
843 Bblock*
844 get_backend(Translate_context*);
846 // Iterate over statements.
848 typedef std::vector<Statement*>::iterator iterator;
850 iterator
851 begin()
852 { return this->statements_.begin(); }
854 iterator
855 end()
856 { return this->statements_.end(); }
858 private:
859 // Enclosing block.
860 Block* enclosing_;
861 // Statements in the block.
862 std::vector<Statement*> statements_;
863 // Binding contour.
864 Bindings* bindings_;
865 // Location of start of block.
866 Location start_location_;
867 // Location of end of block.
868 Location end_location_;
871 // A function.
873 class Function
875 public:
876 Function(Function_type* type, Function*, Block*, Location);
878 // Return the function's type.
879 Function_type*
880 type() const
881 { return this->type_; }
883 // Return the enclosing function if there is one.
884 Function*
885 enclosing()
886 { return this->enclosing_; }
888 // Set the enclosing function. This is used when building thunks
889 // for functions which call recover.
890 void
891 set_enclosing(Function* enclosing)
893 go_assert(this->enclosing_ == NULL);
894 this->enclosing_ = enclosing;
897 // The result variables.
898 typedef std::vector<Named_object*> Results;
900 // Create the result variables in the outer block.
901 void
902 create_result_variables(Gogo*);
904 // Update the named result variables when cloning a function which
905 // calls recover.
906 void
907 update_result_variables();
909 // Return the result variables.
910 Results*
911 result_variables()
912 { return this->results_; }
914 // Whether the result variables have names.
915 bool
916 results_are_named() const
917 { return this->results_are_named_; }
919 // Whether this method should not be included in the type
920 // descriptor.
921 bool
922 nointerface() const
924 go_assert(this->is_method());
925 return this->nointerface_;
928 // Record that this method should not be included in the type
929 // descriptor.
930 void
931 set_nointerface()
933 go_assert(this->is_method());
934 this->nointerface_ = true;
937 // Add a new field to the closure variable.
938 void
939 add_closure_field(Named_object* var, Location loc)
940 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
942 // Whether this function needs a closure.
943 bool
944 needs_closure() const
945 { return !this->closure_fields_.empty(); }
947 // Return the closure variable, creating it if necessary. This is
948 // passed to the function as a static chain parameter.
949 Named_object*
950 closure_var();
952 // Set the closure variable. This is used when building thunks for
953 // functions which call recover.
954 void
955 set_closure_var(Named_object* v)
957 go_assert(this->closure_var_ == NULL);
958 this->closure_var_ = v;
961 // Return the variable for a reference to field INDEX in the closure
962 // variable.
963 Named_object*
964 enclosing_var(unsigned int index)
966 go_assert(index < this->closure_fields_.size());
967 return closure_fields_[index].first;
970 // Set the type of the closure variable if there is one.
971 void
972 set_closure_type();
974 // Get the block of statements associated with the function.
975 Block*
976 block() const
977 { return this->block_; }
979 // Get the location of the start of the function.
980 Location
981 location() const
982 { return this->location_; }
984 // Return whether this function is actually a method.
985 bool
986 is_method() const;
988 // Add a label definition to the function.
989 Label*
990 add_label_definition(Gogo*, const std::string& label_name, Location);
992 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
993 // if we should report errors for a goto from the current location
994 // to the label location.
995 Label*
996 add_label_reference(Gogo*, const std::string& label_name,
997 Location, bool issue_goto_errors);
999 // Warn about labels that are defined but not used.
1000 void
1001 check_labels() const;
1003 // Note that a new local type has been added. Return its index.
1004 unsigned int
1005 new_local_type_index()
1006 { return this->local_type_count_++; }
1008 // Whether this function calls the predeclared recover function.
1009 bool
1010 calls_recover() const
1011 { return this->calls_recover_; }
1013 // Record that this function calls the predeclared recover function.
1014 // This is set during the lowering pass.
1015 void
1016 set_calls_recover()
1017 { this->calls_recover_ = true; }
1019 // Whether this is a recover thunk function.
1020 bool
1021 is_recover_thunk() const
1022 { return this->is_recover_thunk_; }
1024 // Record that this is a thunk built for a function which calls
1025 // recover.
1026 void
1027 set_is_recover_thunk()
1028 { this->is_recover_thunk_ = true; }
1030 // Whether this function already has a recover thunk.
1031 bool
1032 has_recover_thunk() const
1033 { return this->has_recover_thunk_; }
1035 // Record that this function already has a recover thunk.
1036 void
1037 set_has_recover_thunk()
1038 { this->has_recover_thunk_ = true; }
1040 // Mark the function as going into a unique section.
1041 void
1042 set_in_unique_section()
1043 { this->in_unique_section_ = true; }
1045 // Whether this function was created as a descriptor wrapper for
1046 // another function.
1047 bool
1048 is_descriptor_wrapper() const
1049 { return this->is_descriptor_wrapper_; }
1051 // Swap with another function. Used only for the thunk which calls
1052 // recover.
1053 void
1054 swap_for_recover(Function *);
1056 // Traverse the tree.
1058 traverse(Traverse*);
1060 // Determine types in the function.
1061 void
1062 determine_types();
1064 // Return an expression for the function descriptor, given the named
1065 // object for this function. This may only be called for functions
1066 // without a closure. This will be an immutable struct with one
1067 // field that points to the function's code.
1068 Expression*
1069 descriptor(Gogo*, Named_object*);
1071 // Set the descriptor for this function. This is used when a
1072 // function declaration is followed by a function definition.
1073 void
1074 set_descriptor(Expression* descriptor)
1076 go_assert(this->descriptor_ == NULL);
1077 this->descriptor_ = descriptor;
1080 // Build a descriptor wrapper function.
1081 static Named_object*
1082 make_descriptor_wrapper(Gogo*, Named_object*, Function_type*);
1084 // Return the function's decl given an identifier.
1085 tree
1086 get_or_make_decl(Gogo*, Named_object*, tree id);
1088 // Return the function's decl after it has been built.
1089 tree
1090 get_decl() const
1092 go_assert(this->fndecl_ != NULL);
1093 return this->fndecl_;
1096 // Set the function decl to hold a tree of the function code.
1097 void
1098 build_tree(Gogo*, Named_object*);
1100 // Get the value to return when not explicitly specified. May also
1101 // add statements to execute first to STMT_LIST.
1102 tree
1103 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1105 // Get a tree for the variable holding the defer stack.
1106 Expression*
1107 defer_stack(Location);
1109 // Export the function.
1110 void
1111 export_func(Export*, const std::string& name) const;
1113 // Export a function with a type.
1114 static void
1115 export_func_with_type(Export*, const std::string& name,
1116 const Function_type*);
1118 // Import a function.
1119 static void
1120 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1121 Typed_identifier_list** pparameters,
1122 Typed_identifier_list** presults, bool* is_varargs);
1124 private:
1125 // Type for mapping from label names to Label objects.
1126 typedef Unordered_map(std::string, Label*) Labels;
1128 tree
1129 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1131 tree
1132 copy_parm_to_heap(Gogo*, Named_object*, tree);
1134 void
1135 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1137 typedef std::vector<std::pair<Named_object*,
1138 Location> > Closure_fields;
1140 // The function's type.
1141 Function_type* type_;
1142 // The enclosing function. This is NULL when there isn't one, which
1143 // is the normal case.
1144 Function* enclosing_;
1145 // The result variables, if any.
1146 Results* results_;
1147 // If there is a closure, this is the list of variables which appear
1148 // in the closure. This is created by the parser, and then resolved
1149 // to a real type when we lower parse trees.
1150 Closure_fields closure_fields_;
1151 // The closure variable, passed as a parameter using the static
1152 // chain parameter. Normally NULL.
1153 Named_object* closure_var_;
1154 // The outer block of statements in the function.
1155 Block* block_;
1156 // The source location of the start of the function.
1157 Location location_;
1158 // Labels defined or referenced in the function.
1159 Labels labels_;
1160 // The number of local types defined in this function.
1161 unsigned int local_type_count_;
1162 // The function descriptor, if any.
1163 Expression* descriptor_;
1164 // The function decl.
1165 tree fndecl_;
1166 // The defer stack variable. A pointer to this variable is used to
1167 // distinguish the defer stack for one function from another. This
1168 // is NULL unless we actually need a defer stack.
1169 Temporary_statement* defer_stack_;
1170 // True if the result variables are named.
1171 bool results_are_named_ : 1;
1172 // True if this method should not be included in the type descriptor.
1173 bool nointerface_ : 1;
1174 // True if this function calls the predeclared recover function.
1175 bool calls_recover_ : 1;
1176 // True if this a thunk built for a function which calls recover.
1177 bool is_recover_thunk_ : 1;
1178 // True if this function already has a recover thunk.
1179 bool has_recover_thunk_ : 1;
1180 // True if this function should be put in a unique section. This is
1181 // turned on for field tracking.
1182 bool in_unique_section_ : 1;
1183 // True if this is a function wrapper created to put in a function
1184 // descriptor.
1185 bool is_descriptor_wrapper_ : 1;
1188 // A snapshot of the current binding state.
1190 class Bindings_snapshot
1192 public:
1193 Bindings_snapshot(const Block*, Location);
1195 // Report any errors appropriate for a goto from the current binding
1196 // state of B to this one.
1197 void
1198 check_goto_from(const Block* b, Location);
1200 // Report any errors appropriate for a goto from this binding state
1201 // to the current state of B.
1202 void
1203 check_goto_to(const Block* b);
1205 private:
1206 bool
1207 check_goto_block(Location, const Block*, const Block*, size_t*);
1209 void
1210 check_goto_defs(Location, const Block*, size_t, size_t);
1212 // The current block.
1213 const Block* block_;
1214 // The number of names currently defined in each open block.
1215 // Element 0 is this->block_, element 1 is
1216 // this->block_->enclosing(), etc.
1217 std::vector<size_t> counts_;
1218 // The location where this snapshot was taken.
1219 Location location_;
1222 // A function declaration.
1224 class Function_declaration
1226 public:
1227 Function_declaration(Function_type* fntype, Location location)
1228 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1229 fndecl_(NULL)
1232 Function_type*
1233 type() const
1234 { return this->fntype_; }
1236 Location
1237 location() const
1238 { return this->location_; }
1240 const std::string&
1241 asm_name() const
1242 { return this->asm_name_; }
1244 // Set the assembler name.
1245 void
1246 set_asm_name(const std::string& asm_name)
1247 { this->asm_name_ = asm_name; }
1249 // Return an expression for the function descriptor, given the named
1250 // object for this function. This may only be called for functions
1251 // without a closure. This will be an immutable struct with one
1252 // field that points to the function's code.
1253 Expression*
1254 descriptor(Gogo*, Named_object*);
1256 // Return true if we have created a descriptor for this declaration.
1257 bool
1258 has_descriptor() const
1259 { return this->descriptor_ != NULL; }
1261 // Return a decl for the function given an identifier.
1262 tree
1263 get_or_make_decl(Gogo*, Named_object*, tree id);
1265 // If there is a descriptor, build it into the backend
1266 // representation.
1267 void
1268 build_backend_descriptor(Gogo*);
1270 // Export a function declaration.
1271 void
1272 export_func(Export* exp, const std::string& name) const
1273 { Function::export_func_with_type(exp, name, this->fntype_); }
1275 private:
1276 // The type of the function.
1277 Function_type* fntype_;
1278 // The location of the declaration.
1279 Location location_;
1280 // The assembler name: this is the name to use in references to the
1281 // function. This is normally empty.
1282 std::string asm_name_;
1283 // The function descriptor, if any.
1284 Expression* descriptor_;
1285 // The function decl if needed.
1286 tree fndecl_;
1289 // A variable.
1291 class Variable
1293 public:
1294 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1295 bool is_receiver, Location);
1297 // Get the type of the variable.
1298 Type*
1299 type();
1301 Type*
1302 type() const;
1304 // Return whether the type is defined yet.
1305 bool
1306 has_type() const;
1308 // Get the initial value.
1309 Expression*
1310 init() const
1311 { return this->init_; }
1313 // Return whether there are any preinit statements.
1314 bool
1315 has_pre_init() const
1316 { return this->preinit_ != NULL; }
1318 // Return the preinit statements if any.
1319 Block*
1320 preinit() const
1321 { return this->preinit_; }
1323 // Return whether this is a global variable.
1324 bool
1325 is_global() const
1326 { return this->is_global_; }
1328 // Return whether this is a function parameter.
1329 bool
1330 is_parameter() const
1331 { return this->is_parameter_; }
1333 // Return whether this is the receiver parameter of a method.
1334 bool
1335 is_receiver() const
1336 { return this->is_receiver_; }
1338 // Change this parameter to be a receiver. This is used when
1339 // creating the thunks created for functions which call recover.
1340 void
1341 set_is_receiver()
1343 go_assert(this->is_parameter_);
1344 this->is_receiver_ = true;
1347 // Change this parameter to not be a receiver. This is used when
1348 // creating the thunks created for functions which call recover.
1349 void
1350 set_is_not_receiver()
1352 go_assert(this->is_parameter_);
1353 this->is_receiver_ = false;
1356 // Return whether this is the varargs parameter of a function.
1357 bool
1358 is_varargs_parameter() const
1359 { return this->is_varargs_parameter_; }
1361 // Whether this variable's address is taken.
1362 bool
1363 is_address_taken() const
1364 { return this->is_address_taken_; }
1366 // Whether this variable should live in the heap.
1367 bool
1368 is_in_heap() const
1369 { return this->is_address_taken_ && !this->is_global_; }
1371 // Note that something takes the address of this variable.
1372 void
1373 set_address_taken()
1374 { this->is_address_taken_ = true; }
1376 // Return whether the address is taken but does not escape.
1377 bool
1378 is_non_escaping_address_taken() const
1379 { return this->is_non_escaping_address_taken_; }
1381 // Note that something takes the address of this variable such that
1382 // the address does not escape the function.
1383 void
1384 set_non_escaping_address_taken()
1385 { this->is_non_escaping_address_taken_ = true; }
1387 // Get the source location of the variable's declaration.
1388 Location
1389 location() const
1390 { return this->location_; }
1392 // Record that this is the varargs parameter of a function.
1393 void
1394 set_is_varargs_parameter()
1396 go_assert(this->is_parameter_);
1397 this->is_varargs_parameter_ = true;
1400 // Return whether the variable has been used.
1401 bool
1402 is_used() const
1403 { return this->is_used_; }
1405 // Mark that the variable has been used.
1406 void
1407 set_is_used()
1408 { this->is_used_ = true; }
1410 // Clear the initial value; used for error handling.
1411 void
1412 clear_init()
1413 { this->init_ = NULL; }
1415 // Set the initial value; used for converting shortcuts.
1416 void
1417 set_init(Expression* init)
1418 { this->init_ = init; }
1420 // Get the preinit block, a block of statements to be run before the
1421 // initialization expression.
1422 Block*
1423 preinit_block(Gogo*);
1425 // Add a statement to be run before the initialization expression.
1426 // This is only used for global variables.
1427 void
1428 add_preinit_statement(Gogo*, Statement*);
1430 // Lower the initialization expression after parsing is complete.
1431 void
1432 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1434 // A special case: the init value is used only to determine the
1435 // type. This is used if the variable is defined using := with the
1436 // comma-ok form of a map index or a receive expression. The init
1437 // value is actually the map index expression or receive expression.
1438 // We use this because we may not know the right type at parse time.
1439 void
1440 set_type_from_init_tuple()
1441 { this->type_from_init_tuple_ = true; }
1443 // Another special case: the init value is used only to determine
1444 // the type. This is used if the variable is defined using := with
1445 // a range clause. The init value is the range expression. The
1446 // type of the variable is the index type of the range expression
1447 // (i.e., the first value returned by a range).
1448 void
1449 set_type_from_range_index()
1450 { this->type_from_range_index_ = true; }
1452 // Another special case: like set_type_from_range_index, but the
1453 // type is the value type of the range expression (i.e., the second
1454 // value returned by a range).
1455 void
1456 set_type_from_range_value()
1457 { this->type_from_range_value_ = true; }
1459 // Another special case: the init value is used only to determine
1460 // the type. This is used if the variable is defined using := with
1461 // a case in a select statement. The init value is the channel.
1462 // The type of the variable is the channel's element type.
1463 void
1464 set_type_from_chan_element()
1465 { this->type_from_chan_element_ = true; }
1467 // After we lower the select statement, we once again set the type
1468 // from the initialization expression.
1469 void
1470 clear_type_from_chan_element()
1472 go_assert(this->type_from_chan_element_);
1473 this->type_from_chan_element_ = false;
1476 // Note that this variable was created for a type switch clause.
1477 void
1478 set_is_type_switch_var()
1479 { this->is_type_switch_var_ = true; }
1481 // Mark the variable as going into a unique section.
1482 void
1483 set_in_unique_section()
1485 go_assert(this->is_global_);
1486 this->in_unique_section_ = true;
1489 // Traverse the initializer expression.
1491 traverse_expression(Traverse*, unsigned int traverse_mask);
1493 // Determine the type of the variable if necessary.
1494 void
1495 determine_type();
1497 // Get the backend representation of the variable.
1498 Bvariable*
1499 get_backend_variable(Gogo*, Named_object*, const Package*,
1500 const std::string&);
1502 // Get the initial value of the variable as a tree. This may only
1503 // be called if has_pre_init() returns false.
1504 tree
1505 get_init_tree(Gogo*, Named_object* function);
1507 // Return a series of statements which sets the value of the
1508 // variable in DECL. This should only be called is has_pre_init()
1509 // returns true. DECL may be NULL for a sink variable.
1510 tree
1511 get_init_block(Gogo*, Named_object* function, tree decl);
1513 // Export the variable.
1514 void
1515 export_var(Export*, const std::string& name) const;
1517 // Import a variable.
1518 static void
1519 import_var(Import*, std::string* pname, Type** ptype);
1521 private:
1522 // The type of a tuple.
1523 Type*
1524 type_from_tuple(Expression*, bool) const;
1526 // The type of a range.
1527 Type*
1528 type_from_range(Expression*, bool, bool) const;
1530 // The element type of a channel.
1531 Type*
1532 type_from_chan_element(Expression*, bool) const;
1534 // The variable's type. This may be NULL if the type is set from
1535 // the expression.
1536 Type* type_;
1537 // The initial value. This may be NULL if the variable should be
1538 // initialized to the default value for the type.
1539 Expression* init_;
1540 // Statements to run before the init statement.
1541 Block* preinit_;
1542 // Location of variable definition.
1543 Location location_;
1544 // Backend representation.
1545 Bvariable* backend_;
1546 // Whether this is a global variable.
1547 bool is_global_ : 1;
1548 // Whether this is a function parameter.
1549 bool is_parameter_ : 1;
1550 // Whether this is the receiver parameter of a method.
1551 bool is_receiver_ : 1;
1552 // Whether this is the varargs parameter of a function.
1553 bool is_varargs_parameter_ : 1;
1554 // Whether this variable is ever referenced.
1555 bool is_used_ : 1;
1556 // Whether something takes the address of this variable. For a
1557 // local variable this implies that the variable has to be on the
1558 // heap.
1559 bool is_address_taken_ : 1;
1560 // Whether something takes the address of this variable such that
1561 // the address does not escape the function.
1562 bool is_non_escaping_address_taken_ : 1;
1563 // True if we have seen this variable in a traversal.
1564 bool seen_ : 1;
1565 // True if we have lowered the initialization expression.
1566 bool init_is_lowered_ : 1;
1567 // True if init is a tuple used to set the type.
1568 bool type_from_init_tuple_ : 1;
1569 // True if init is a range clause and the type is the index type.
1570 bool type_from_range_index_ : 1;
1571 // True if init is a range clause and the type is the value type.
1572 bool type_from_range_value_ : 1;
1573 // True if init is a channel and the type is the channel's element type.
1574 bool type_from_chan_element_ : 1;
1575 // True if this is a variable created for a type switch case.
1576 bool is_type_switch_var_ : 1;
1577 // True if we have determined types.
1578 bool determined_type_ : 1;
1579 // True if this variable should be put in a unique section. This is
1580 // used for field tracking.
1581 bool in_unique_section_ : 1;
1584 // A variable which is really the name for a function return value, or
1585 // part of one.
1587 class Result_variable
1589 public:
1590 Result_variable(Type* type, Function* function, int index,
1591 Location location)
1592 : type_(type), function_(function), index_(index), location_(location),
1593 backend_(NULL), is_address_taken_(false),
1594 is_non_escaping_address_taken_(false)
1597 // Get the type of the result variable.
1598 Type*
1599 type() const
1600 { return this->type_; }
1602 // Get the function that this is associated with.
1603 Function*
1604 function() const
1605 { return this->function_; }
1607 // Index in the list of function results.
1609 index() const
1610 { return this->index_; }
1612 // The location of the variable definition.
1613 Location
1614 location() const
1615 { return this->location_; }
1617 // Whether this variable's address is taken.
1618 bool
1619 is_address_taken() const
1620 { return this->is_address_taken_; }
1622 // Note that something takes the address of this variable.
1623 void
1624 set_address_taken()
1625 { this->is_address_taken_ = true; }
1627 // Return whether the address is taken but does not escape.
1628 bool
1629 is_non_escaping_address_taken() const
1630 { return this->is_non_escaping_address_taken_; }
1632 // Note that something takes the address of this variable such that
1633 // the address does not escape the function.
1634 void
1635 set_non_escaping_address_taken()
1636 { this->is_non_escaping_address_taken_ = true; }
1638 // Whether this variable should live in the heap.
1639 bool
1640 is_in_heap() const
1641 { return this->is_address_taken_; }
1643 // Set the function. This is used when cloning functions which call
1644 // recover.
1645 void
1646 set_function(Function* function)
1647 { this->function_ = function; }
1649 // Get the backend representation of the variable.
1650 Bvariable*
1651 get_backend_variable(Gogo*, Named_object*, const std::string&);
1653 private:
1654 // Type of result variable.
1655 Type* type_;
1656 // Function with which this is associated.
1657 Function* function_;
1658 // Index in list of results.
1659 int index_;
1660 // Where the result variable is defined.
1661 Location location_;
1662 // Backend representation.
1663 Bvariable* backend_;
1664 // Whether something takes the address of this variable.
1665 bool is_address_taken_;
1666 // Whether something takes the address of this variable such that
1667 // the address does not escape the function.
1668 bool is_non_escaping_address_taken_;
1671 // The value we keep for a named constant. This lets us hold a type
1672 // and an expression.
1674 class Named_constant
1676 public:
1677 Named_constant(Type* type, Expression* expr, int iota_value,
1678 Location location)
1679 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1680 lowering_(false)
1683 Type*
1684 type() const
1685 { return this->type_; }
1687 Expression*
1688 expr() const
1689 { return this->expr_; }
1692 iota_value() const
1693 { return this->iota_value_; }
1695 Location
1696 location() const
1697 { return this->location_; }
1699 // Whether we are lowering.
1700 bool
1701 lowering() const
1702 { return this->lowering_; }
1704 // Set that we are lowering.
1705 void
1706 set_lowering()
1707 { this->lowering_ = true; }
1709 // We are no longer lowering.
1710 void
1711 clear_lowering()
1712 { this->lowering_ = false; }
1714 // Traverse the expression.
1716 traverse_expression(Traverse*);
1718 // Determine the type of the constant if necessary.
1719 void
1720 determine_type();
1722 // Indicate that we found and reported an error for this constant.
1723 void
1724 set_error();
1726 // Export the constant.
1727 void
1728 export_const(Export*, const std::string& name) const;
1730 // Import a constant.
1731 static void
1732 import_const(Import*, std::string*, Type**, Expression**);
1734 private:
1735 // The type of the constant.
1736 Type* type_;
1737 // The expression for the constant.
1738 Expression* expr_;
1739 // If the predeclared constant iota is used in EXPR_, this is the
1740 // value it will have. We do this because at parse time we don't
1741 // know whether the name "iota" will refer to the predeclared
1742 // constant or to something else. We put in the right value in when
1743 // we lower.
1744 int iota_value_;
1745 // The location of the definition.
1746 Location location_;
1747 // Whether we are currently lowering this constant.
1748 bool lowering_;
1751 // A type declaration.
1753 class Type_declaration
1755 public:
1756 Type_declaration(Location location)
1757 : location_(location), in_function_(NULL), in_function_index_(0),
1758 methods_(), issued_warning_(false)
1761 // Return the location.
1762 Location
1763 location() const
1764 { return this->location_; }
1766 // Return the function in which this type is declared. This will
1767 // return NULL for a type declared in global scope.
1768 Named_object*
1769 in_function(unsigned int* pindex)
1771 *pindex = this->in_function_index_;
1772 return this->in_function_;
1775 // Set the function in which this type is declared.
1776 void
1777 set_in_function(Named_object* f, unsigned int index)
1779 this->in_function_ = f;
1780 this->in_function_index_ = index;
1783 // Add a method to this type. This is used when methods are defined
1784 // before the type.
1785 Named_object*
1786 add_method(const std::string& name, Function* function);
1788 // Add a method declaration to this type.
1789 Named_object*
1790 add_method_declaration(const std::string& name, Package*,
1791 Function_type* type, Location location);
1793 // Return whether any methods were defined.
1794 bool
1795 has_methods() const;
1797 // Return the methods.
1798 const std::vector<Named_object*>*
1799 methods() const
1800 { return &this->methods_; }
1802 // Define methods when the real type is known.
1803 void
1804 define_methods(Named_type*);
1806 // This is called if we are trying to use this type. It returns
1807 // true if we should issue a warning.
1808 bool
1809 using_type();
1811 private:
1812 // The location of the type declaration.
1813 Location location_;
1814 // If this type is declared in a function, a pointer back to the
1815 // function in which it is defined.
1816 Named_object* in_function_;
1817 // The index of this type in IN_FUNCTION_.
1818 unsigned int in_function_index_;
1819 // Methods defined before the type is defined.
1820 std::vector<Named_object*> methods_;
1821 // True if we have issued a warning about a use of this type
1822 // declaration when it is undefined.
1823 bool issued_warning_;
1826 // An unknown object. These are created by the parser for forward
1827 // references to names which have not been seen before. In a correct
1828 // program, these will always point to a real definition by the end of
1829 // the parse. Because they point to another Named_object, these may
1830 // only be referenced by Unknown_expression objects.
1832 class Unknown_name
1834 public:
1835 Unknown_name(Location location)
1836 : location_(location), real_named_object_(NULL)
1839 // Return the location where this name was first seen.
1840 Location
1841 location() const
1842 { return this->location_; }
1844 // Return the real named object that this points to, or NULL if it
1845 // was never resolved.
1846 Named_object*
1847 real_named_object() const
1848 { return this->real_named_object_; }
1850 // Set the real named object that this points to.
1851 void
1852 set_real_named_object(Named_object* no);
1854 private:
1855 // The location where this name was first seen.
1856 Location location_;
1857 // The real named object when it is known.
1858 Named_object*
1859 real_named_object_;
1862 // A named object named. This is the result of a declaration. We
1863 // don't use a superclass because they all have to be handled
1864 // differently.
1866 class Named_object
1868 public:
1869 enum Classification
1871 // An uninitialized Named_object. We should never see this.
1872 NAMED_OBJECT_UNINITIALIZED,
1873 // An erroneous name. This indicates a parse error, to avoid
1874 // later errors about undefined references.
1875 NAMED_OBJECT_ERRONEOUS,
1876 // An unknown name. This is used for forward references. In a
1877 // correct program, these will all be resolved by the end of the
1878 // parse.
1879 NAMED_OBJECT_UNKNOWN,
1880 // A const.
1881 NAMED_OBJECT_CONST,
1882 // A type.
1883 NAMED_OBJECT_TYPE,
1884 // A forward type declaration.
1885 NAMED_OBJECT_TYPE_DECLARATION,
1886 // A var.
1887 NAMED_OBJECT_VAR,
1888 // A result variable in a function.
1889 NAMED_OBJECT_RESULT_VAR,
1890 // The blank identifier--the special variable named _.
1891 NAMED_OBJECT_SINK,
1892 // A func.
1893 NAMED_OBJECT_FUNC,
1894 // A forward func declaration.
1895 NAMED_OBJECT_FUNC_DECLARATION,
1896 // A package.
1897 NAMED_OBJECT_PACKAGE
1900 // Return the classification.
1901 Classification
1902 classification() const
1903 { return this->classification_; }
1905 // Classifiers.
1907 bool
1908 is_erroneous() const
1909 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1911 bool
1912 is_unknown() const
1913 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1915 bool
1916 is_const() const
1917 { return this->classification_ == NAMED_OBJECT_CONST; }
1919 bool
1920 is_type() const
1921 { return this->classification_ == NAMED_OBJECT_TYPE; }
1923 bool
1924 is_type_declaration() const
1925 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1927 bool
1928 is_variable() const
1929 { return this->classification_ == NAMED_OBJECT_VAR; }
1931 bool
1932 is_result_variable() const
1933 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1935 bool
1936 is_sink() const
1937 { return this->classification_ == NAMED_OBJECT_SINK; }
1939 bool
1940 is_function() const
1941 { return this->classification_ == NAMED_OBJECT_FUNC; }
1943 bool
1944 is_function_declaration() const
1945 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1947 bool
1948 is_package() const
1949 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1951 // Creators.
1953 static Named_object*
1954 make_erroneous_name(const std::string& name)
1955 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1957 static Named_object*
1958 make_unknown_name(const std::string& name, Location);
1960 static Named_object*
1961 make_constant(const Typed_identifier&, const Package*, Expression*,
1962 int iota_value);
1964 static Named_object*
1965 make_type(const std::string&, const Package*, Type*, Location);
1967 static Named_object*
1968 make_type_declaration(const std::string&, const Package*, Location);
1970 static Named_object*
1971 make_variable(const std::string&, const Package*, Variable*);
1973 static Named_object*
1974 make_result_variable(const std::string&, Result_variable*);
1976 static Named_object*
1977 make_sink();
1979 static Named_object*
1980 make_function(const std::string&, const Package*, Function*);
1982 static Named_object*
1983 make_function_declaration(const std::string&, const Package*, Function_type*,
1984 Location);
1986 static Named_object*
1987 make_package(const std::string& alias, Package* package);
1989 // Getters.
1991 Unknown_name*
1992 unknown_value()
1994 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1995 return this->u_.unknown_value;
1998 const Unknown_name*
1999 unknown_value() const
2001 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2002 return this->u_.unknown_value;
2005 Named_constant*
2006 const_value()
2008 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2009 return this->u_.const_value;
2012 const Named_constant*
2013 const_value() const
2015 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2016 return this->u_.const_value;
2019 Named_type*
2020 type_value()
2022 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2023 return this->u_.type_value;
2026 const Named_type*
2027 type_value() const
2029 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2030 return this->u_.type_value;
2033 Type_declaration*
2034 type_declaration_value()
2036 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2037 return this->u_.type_declaration;
2040 const Type_declaration*
2041 type_declaration_value() const
2043 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2044 return this->u_.type_declaration;
2047 Variable*
2048 var_value()
2050 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2051 return this->u_.var_value;
2054 const Variable*
2055 var_value() const
2057 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2058 return this->u_.var_value;
2061 Result_variable*
2062 result_var_value()
2064 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2065 return this->u_.result_var_value;
2068 const Result_variable*
2069 result_var_value() const
2071 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2072 return this->u_.result_var_value;
2075 Function*
2076 func_value()
2078 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2079 return this->u_.func_value;
2082 const Function*
2083 func_value() const
2085 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2086 return this->u_.func_value;
2089 Function_declaration*
2090 func_declaration_value()
2092 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2093 return this->u_.func_declaration_value;
2096 const Function_declaration*
2097 func_declaration_value() const
2099 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2100 return this->u_.func_declaration_value;
2103 Package*
2104 package_value()
2106 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2107 return this->u_.package_value;
2110 const Package*
2111 package_value() const
2113 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2114 return this->u_.package_value;
2117 const std::string&
2118 name() const
2119 { return this->name_; }
2121 // Return the name to use in an error message. The difference is
2122 // that if this Named_object is defined in a different package, this
2123 // will return PACKAGE.NAME.
2124 std::string
2125 message_name() const;
2127 const Package*
2128 package() const
2129 { return this->package_; }
2131 // Resolve an unknown value if possible. This returns the same
2132 // Named_object or a new one.
2133 Named_object*
2134 resolve()
2136 Named_object* ret = this;
2137 if (this->is_unknown())
2139 Named_object* r = this->unknown_value()->real_named_object();
2140 if (r != NULL)
2141 ret = r;
2143 return ret;
2146 const Named_object*
2147 resolve() const
2149 const Named_object* ret = this;
2150 if (this->is_unknown())
2152 const Named_object* r = this->unknown_value()->real_named_object();
2153 if (r != NULL)
2154 ret = r;
2156 return ret;
2159 // The location where this object was defined or referenced.
2160 Location
2161 location() const;
2163 // Convert a variable to the backend representation.
2164 Bvariable*
2165 get_backend_variable(Gogo*, Named_object* function);
2167 // Return a tree for the external identifier for this object.
2168 tree
2169 get_id(Gogo*);
2171 // Return a tree representing this object.
2172 tree
2173 get_tree(Gogo*, Named_object* function);
2175 // Define a type declaration.
2176 void
2177 set_type_value(Named_type*);
2179 // Define a function declaration.
2180 void
2181 set_function_value(Function*);
2183 // Declare an unknown name as a type declaration.
2184 void
2185 declare_as_type();
2187 // Export this object.
2188 void
2189 export_named_object(Export*) const;
2191 private:
2192 Named_object(const std::string&, const Package*, Classification);
2194 // The name of the object.
2195 std::string name_;
2196 // The package that this object is in. This is NULL if it is in the
2197 // file we are compiling.
2198 const Package* package_;
2199 // The type of object this is.
2200 Classification classification_;
2201 // The real data.
2202 union
2204 Unknown_name* unknown_value;
2205 Named_constant* const_value;
2206 Named_type* type_value;
2207 Type_declaration* type_declaration;
2208 Variable* var_value;
2209 Result_variable* result_var_value;
2210 Function* func_value;
2211 Function_declaration* func_declaration_value;
2212 Package* package_value;
2213 } u_;
2214 // The DECL tree for this object if we have already converted it.
2215 tree tree_;
2218 // A binding contour. This binds names to objects.
2220 class Bindings
2222 public:
2223 // Type for mapping from names to objects.
2224 typedef Unordered_map(std::string, Named_object*) Contour;
2226 Bindings(Bindings* enclosing);
2228 // Add an erroneous name.
2229 Named_object*
2230 add_erroneous_name(const std::string& name)
2231 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2233 // Add an unknown name.
2234 Named_object*
2235 add_unknown_name(const std::string& name, Location location)
2237 return this->add_named_object(Named_object::make_unknown_name(name,
2238 location));
2241 // Add a constant.
2242 Named_object*
2243 add_constant(const Typed_identifier& tid, const Package* package,
2244 Expression* expr, int iota_value)
2246 return this->add_named_object(Named_object::make_constant(tid, package,
2247 expr,
2248 iota_value));
2251 // Add a type.
2252 Named_object*
2253 add_type(const std::string& name, const Package* package, Type* type,
2254 Location location)
2256 return this->add_named_object(Named_object::make_type(name, package, type,
2257 location));
2260 // Add a named type. This is used for builtin types, and to add an
2261 // imported type to the global scope.
2262 Named_object*
2263 add_named_type(Named_type* named_type);
2265 // Add a type declaration.
2266 Named_object*
2267 add_type_declaration(const std::string& name, const Package* package,
2268 Location location)
2270 Named_object* no = Named_object::make_type_declaration(name, package,
2271 location);
2272 return this->add_named_object(no);
2275 // Add a variable.
2276 Named_object*
2277 add_variable(const std::string& name, const Package* package,
2278 Variable* variable)
2280 return this->add_named_object(Named_object::make_variable(name, package,
2281 variable));
2284 // Add a result variable.
2285 Named_object*
2286 add_result_variable(const std::string& name, Result_variable* result)
2288 return this->add_named_object(Named_object::make_result_variable(name,
2289 result));
2292 // Add a function.
2293 Named_object*
2294 add_function(const std::string& name, const Package*, Function* function);
2296 // Add a function declaration.
2297 Named_object*
2298 add_function_declaration(const std::string& name, const Package* package,
2299 Function_type* type, Location location);
2301 // Add a package. The location is the location of the import
2302 // statement.
2303 Named_object*
2304 add_package(const std::string& alias, Package* package)
2306 Named_object* no = Named_object::make_package(alias, package);
2307 return this->add_named_object(no);
2310 // Define a type which was already declared.
2311 void
2312 define_type(Named_object*, Named_type*);
2314 // Add a method to the list of objects. This is not added to the
2315 // lookup table.
2316 void
2317 add_method(Named_object*);
2319 // Add a named object to this binding.
2320 Named_object*
2321 add_named_object(Named_object* no)
2322 { return this->add_named_object_to_contour(&this->bindings_, no); }
2324 // Clear all names in file scope from the bindings.
2325 void
2326 clear_file_scope(Gogo*);
2328 // Look up a name in this binding contour and in any enclosing
2329 // binding contours. This returns NULL if the name is not found.
2330 Named_object*
2331 lookup(const std::string&) const;
2333 // Look up a name in this binding contour without looking in any
2334 // enclosing binding contours. Returns NULL if the name is not found.
2335 Named_object*
2336 lookup_local(const std::string&) const;
2338 // Remove a name.
2339 void
2340 remove_binding(Named_object*);
2342 // Mark all variables as used. This is used for some types of parse
2343 // error.
2344 void
2345 mark_locals_used();
2347 // Traverse the tree. See the Traverse class.
2349 traverse(Traverse*, bool is_global);
2351 // Iterate over definitions. This does not include things which
2352 // were only declared.
2354 typedef std::vector<Named_object*>::const_iterator
2355 const_definitions_iterator;
2357 const_definitions_iterator
2358 begin_definitions() const
2359 { return this->named_objects_.begin(); }
2361 const_definitions_iterator
2362 end_definitions() const
2363 { return this->named_objects_.end(); }
2365 // Return the number of definitions.
2366 size_t
2367 size_definitions() const
2368 { return this->named_objects_.size(); }
2370 // Return whether there are no definitions.
2371 bool
2372 empty_definitions() const
2373 { return this->named_objects_.empty(); }
2375 // Iterate over declarations. This is everything that has been
2376 // declared, which includes everything which has been defined.
2378 typedef Contour::const_iterator const_declarations_iterator;
2380 const_declarations_iterator
2381 begin_declarations() const
2382 { return this->bindings_.begin(); }
2384 const_declarations_iterator
2385 end_declarations() const
2386 { return this->bindings_.end(); }
2388 // Return the number of declarations.
2389 size_t
2390 size_declarations() const
2391 { return this->bindings_.size(); }
2393 // Return whether there are no declarations.
2394 bool
2395 empty_declarations() const
2396 { return this->bindings_.empty(); }
2398 // Return the first declaration.
2399 Named_object*
2400 first_declaration()
2401 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2403 private:
2404 Named_object*
2405 add_named_object_to_contour(Contour*, Named_object*);
2407 Named_object*
2408 new_definition(Named_object*, Named_object*);
2410 // Enclosing bindings.
2411 Bindings* enclosing_;
2412 // The list of objects.
2413 std::vector<Named_object*> named_objects_;
2414 // The mapping from names to objects.
2415 Contour bindings_;
2418 // A label.
2420 class Label
2422 public:
2423 Label(const std::string& name)
2424 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2425 refs_(), is_used_(false), blabel_(NULL)
2428 // Return the label's name.
2429 const std::string&
2430 name() const
2431 { return this->name_; }
2433 // Return whether the label has been defined.
2434 bool
2435 is_defined() const
2436 { return !Linemap::is_unknown_location(this->location_); }
2438 // Return whether the label has been used.
2439 bool
2440 is_used() const
2441 { return this->is_used_; }
2443 // Record that the label is used.
2444 void
2445 set_is_used()
2446 { this->is_used_ = true; }
2448 // Return the location of the definition.
2449 Location
2450 location() const
2451 { return this->location_; }
2453 // Return the bindings snapshot.
2454 Bindings_snapshot*
2455 snapshot() const
2456 { return this->snapshot_; }
2458 // Add a snapshot of a goto which refers to this label.
2459 void
2460 add_snapshot_ref(Bindings_snapshot* snapshot)
2462 go_assert(Linemap::is_unknown_location(this->location_));
2463 this->refs_.push_back(snapshot);
2466 // Return the list of snapshots of goto statements which refer to
2467 // this label.
2468 const std::vector<Bindings_snapshot*>&
2469 refs() const
2470 { return this->refs_; }
2472 // Clear the references.
2473 void
2474 clear_refs();
2476 // Define the label at LOCATION with the given bindings snapshot.
2477 void
2478 define(Location location, Bindings_snapshot* snapshot)
2480 go_assert(Linemap::is_unknown_location(this->location_)
2481 && this->snapshot_ == NULL);
2482 this->location_ = location;
2483 this->snapshot_ = snapshot;
2486 // Return the backend representation for this label.
2487 Blabel*
2488 get_backend_label(Translate_context*);
2490 // Return an expression for the address of this label. This is used
2491 // to get the return address of a deferred function to see whether
2492 // the function may call recover.
2493 Bexpression*
2494 get_addr(Translate_context*, Location location);
2496 private:
2497 // The name of the label.
2498 std::string name_;
2499 // The location of the definition. This is 0 if the label has not
2500 // yet been defined.
2501 Location location_;
2502 // A snapshot of the set of bindings defined at this label, used to
2503 // issue errors about invalid goto statements.
2504 Bindings_snapshot* snapshot_;
2505 // A list of snapshots of goto statements which refer to this label.
2506 std::vector<Bindings_snapshot*> refs_;
2507 // Whether the label has been used.
2508 bool is_used_;
2509 // The backend representation.
2510 Blabel* blabel_;
2513 // An unnamed label. These are used when lowering loops.
2515 class Unnamed_label
2517 public:
2518 Unnamed_label(Location location)
2519 : location_(location), blabel_(NULL)
2522 // Get the location where the label is defined.
2523 Location
2524 location() const
2525 { return this->location_; }
2527 // Set the location where the label is defined.
2528 void
2529 set_location(Location location)
2530 { this->location_ = location; }
2532 // Return a statement which defines this label.
2533 Bstatement*
2534 get_definition(Translate_context*);
2536 // Return a goto to this label from LOCATION.
2537 Bstatement*
2538 get_goto(Translate_context*, Location location);
2540 private:
2541 // Return the backend representation.
2542 Blabel*
2543 get_blabel(Translate_context*);
2545 // The location where the label is defined.
2546 Location location_;
2547 // The backend representation of this label.
2548 Blabel* blabel_;
2551 // An imported package.
2553 class Package
2555 public:
2556 Package(const std::string& pkgpath, Location location);
2558 // Get the package path used for all symbols exported from this
2559 // package.
2560 const std::string&
2561 pkgpath() const
2562 { return this->pkgpath_; }
2564 // Return the package path to use for a symbol name.
2565 const std::string&
2566 pkgpath_symbol() const
2567 { return this->pkgpath_symbol_; }
2569 // Return the location of the import statement.
2570 Location
2571 location() const
2572 { return this->location_; }
2574 // Return whether we know the name of this package yet.
2575 bool
2576 has_package_name() const
2577 { return !this->package_name_.empty(); }
2579 // The name that this package uses in its package clause. This may
2580 // be different from the name in the associated Named_object if the
2581 // import statement used an alias.
2582 const std::string&
2583 package_name() const
2585 go_assert(!this->package_name_.empty());
2586 return this->package_name_;
2589 // The priority of this package. The init function of packages with
2590 // lower priority must be run before the init function of packages
2591 // with higher priority.
2593 priority() const
2594 { return this->priority_; }
2596 // Set the priority.
2597 void
2598 set_priority(int priority);
2600 // Return the bindings.
2601 Bindings*
2602 bindings()
2603 { return this->bindings_; }
2605 // Whether some symbol from the package was used.
2606 bool
2607 used() const
2608 { return this->used_; }
2610 // Note that some symbol from this package was used.
2611 void
2612 set_used() const
2613 { this->used_ = true; }
2615 // Clear the used field for the next file.
2616 void
2617 clear_used()
2618 { this->used_ = false; }
2620 // Whether this package was imported in the current file.
2621 bool
2622 is_imported() const
2623 { return this->is_imported_; }
2625 // Note that this package was imported in the current file.
2626 void
2627 set_is_imported()
2628 { this->is_imported_ = true; }
2630 // Clear the imported field for the next file.
2631 void
2632 clear_is_imported()
2633 { this->is_imported_ = false; }
2635 // Whether this package was imported with a name of "_".
2636 bool
2637 uses_sink_alias() const
2638 { return this->uses_sink_alias_; }
2640 // Note that this package was imported with a name of "_".
2641 void
2642 set_uses_sink_alias()
2643 { this->uses_sink_alias_ = true; }
2645 // Clear the sink alias field for the next file.
2646 void
2647 clear_uses_sink_alias()
2648 { this->uses_sink_alias_ = false; }
2650 // Look up a name in the package. Returns NULL if the name is not
2651 // found.
2652 Named_object*
2653 lookup(const std::string& name) const
2654 { return this->bindings_->lookup(name); }
2656 // Set the name of the package.
2657 void
2658 set_package_name(const std::string& name, Location);
2660 // Set the location of the package. This is used to record the most
2661 // recent import location.
2662 void
2663 set_location(Location location)
2664 { this->location_ = location; }
2666 // Add a constant to the package.
2667 Named_object*
2668 add_constant(const Typed_identifier& tid, Expression* expr)
2669 { return this->bindings_->add_constant(tid, this, expr, 0); }
2671 // Add a type to the package.
2672 Named_object*
2673 add_type(const std::string& name, Type* type, Location location)
2674 { return this->bindings_->add_type(name, this, type, location); }
2676 // Add a type declaration to the package.
2677 Named_object*
2678 add_type_declaration(const std::string& name, Location location)
2679 { return this->bindings_->add_type_declaration(name, this, location); }
2681 // Add a variable to the package.
2682 Named_object*
2683 add_variable(const std::string& name, Variable* variable)
2684 { return this->bindings_->add_variable(name, this, variable); }
2686 // Add a function declaration to the package.
2687 Named_object*
2688 add_function_declaration(const std::string& name, Function_type* type,
2689 Location loc)
2690 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2692 // Determine types of constants.
2693 void
2694 determine_types();
2696 private:
2697 // The package path for type reflection data.
2698 std::string pkgpath_;
2699 // The package path for symbol names.
2700 std::string pkgpath_symbol_;
2701 // The name that this package uses in the package clause. This may
2702 // be the empty string if it is not yet known.
2703 std::string package_name_;
2704 // The names in this package.
2705 Bindings* bindings_;
2706 // The priority of this package. A package has a priority higher
2707 // than the priority of all of the packages that it imports. This
2708 // is used to run init functions in the right order.
2709 int priority_;
2710 // The location of the import statement.
2711 Location location_;
2712 // True if some name from this package was used. This is mutable
2713 // because we can use a package even if we have a const pointer to
2714 // it.
2715 mutable bool used_;
2716 // True if this package was imported in the current file.
2717 bool is_imported_;
2718 // True if this package was imported with a name of "_".
2719 bool uses_sink_alias_;
2722 // Return codes for the traversal functions. This is not an enum
2723 // because we want to be able to declare traversal functions in other
2724 // header files without including this one.
2726 // Continue traversal as usual.
2727 const int TRAVERSE_CONTINUE = -1;
2729 // Exit traversal.
2730 const int TRAVERSE_EXIT = 0;
2732 // Continue traversal, but skip components of the current object.
2733 // E.g., if this is returned by Traverse::statement, we do not
2734 // traverse the expressions in the statement even if
2735 // traverse_expressions is set in the traverse_mask.
2736 const int TRAVERSE_SKIP_COMPONENTS = 1;
2738 // This class is used when traversing the parse tree. The caller uses
2739 // a subclass which overrides functions as desired.
2741 class Traverse
2743 public:
2744 // These bitmasks say what to traverse.
2745 static const unsigned int traverse_variables = 0x1;
2746 static const unsigned int traverse_constants = 0x2;
2747 static const unsigned int traverse_functions = 0x4;
2748 static const unsigned int traverse_blocks = 0x8;
2749 static const unsigned int traverse_statements = 0x10;
2750 static const unsigned int traverse_expressions = 0x20;
2751 static const unsigned int traverse_types = 0x40;
2753 Traverse(unsigned int traverse_mask)
2754 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2757 virtual ~Traverse();
2759 // The bitmask of what to traverse.
2760 unsigned int
2761 traverse_mask() const
2762 { return this->traverse_mask_; }
2764 // Record that we are going to traverse a type. This returns true
2765 // if the type has already been seen in this traversal. This is
2766 // required because types, unlike expressions, can form a circular
2767 // graph.
2768 bool
2769 remember_type(const Type*);
2771 // Record that we are going to see an expression. This returns true
2772 // if the expression has already been seen in this traversal. This
2773 // is only needed for cases where multiple expressions can point to
2774 // a single one.
2775 bool
2776 remember_expression(const Expression*);
2778 // These functions return one of the TRAVERSE codes defined above.
2780 // If traverse_variables is set in the mask, this is called for
2781 // every variable in the tree.
2782 virtual int
2783 variable(Named_object*);
2785 // If traverse_constants is set in the mask, this is called for
2786 // every named constant in the tree. The bool parameter is true for
2787 // a global constant.
2788 virtual int
2789 constant(Named_object*, bool);
2791 // If traverse_functions is set in the mask, this is called for
2792 // every function in the tree.
2793 virtual int
2794 function(Named_object*);
2796 // If traverse_blocks is set in the mask, this is called for every
2797 // block in the tree.
2798 virtual int
2799 block(Block*);
2801 // If traverse_statements is set in the mask, this is called for
2802 // every statement in the tree.
2803 virtual int
2804 statement(Block*, size_t* index, Statement*);
2806 // If traverse_expressions is set in the mask, this is called for
2807 // every expression in the tree.
2808 virtual int
2809 expression(Expression**);
2811 // If traverse_types is set in the mask, this is called for every
2812 // type in the tree.
2813 virtual int
2814 type(Type*);
2816 private:
2817 // A hash table for types we have seen during this traversal. Note
2818 // that this uses the default hash functions for pointers rather
2819 // than Type_hash_identical and Type_identical. This is because for
2820 // traversal we care about seeing a specific type structure. If
2821 // there are two separate instances of identical types, we want to
2822 // traverse both.
2823 typedef Unordered_set(const Type*) Types_seen;
2825 typedef Unordered_set(const Expression*) Expressions_seen;
2827 // Bitmask of what sort of objects to traverse.
2828 unsigned int traverse_mask_;
2829 // Types which have been seen in this traversal.
2830 Types_seen* types_seen_;
2831 // Expressions which have been seen in this traversal.
2832 Expressions_seen* expressions_seen_;
2835 // A class which makes it easier to insert new statements before the
2836 // current statement during a traversal.
2838 class Statement_inserter
2840 public:
2841 // Empty constructor.
2842 Statement_inserter()
2843 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2846 // Constructor for a statement in a block.
2847 Statement_inserter(Block* block, size_t *pindex)
2848 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2851 // Constructor for a global variable.
2852 Statement_inserter(Gogo* gogo, Variable* var)
2853 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2854 { go_assert(var->is_global()); }
2856 // We use the default copy constructor and assignment operator.
2858 // Insert S before the statement we are traversing, or before the
2859 // initialization expression of a global variable.
2860 void
2861 insert(Statement* s);
2863 private:
2864 // The block that the statement is in.
2865 Block* block_;
2866 // The index of the statement that we are traversing.
2867 size_t* pindex_;
2868 // The IR, needed when looking at an initializer expression for a
2869 // global variable.
2870 Gogo* gogo_;
2871 // The global variable, when looking at an initializer expression.
2872 Variable* var_;
2875 // When translating the gogo IR into the backend data structure, this
2876 // is the context we pass down the blocks and statements.
2878 class Translate_context
2880 public:
2881 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2882 Bblock* bblock)
2883 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2884 block_(block), bblock_(bblock), is_const_(false)
2887 // Accessors.
2889 Gogo*
2890 gogo()
2891 { return this->gogo_; }
2893 Backend*
2894 backend()
2895 { return this->backend_; }
2897 Named_object*
2898 function()
2899 { return this->function_; }
2901 Block*
2902 block()
2903 { return this->block_; }
2905 Bblock*
2906 bblock()
2907 { return this->bblock_; }
2909 bool
2910 is_const()
2911 { return this->is_const_; }
2913 // Make a constant context.
2914 void
2915 set_is_const()
2916 { this->is_const_ = true; }
2918 private:
2919 // The IR for the entire compilation unit.
2920 Gogo* gogo_;
2921 // The generator for the backend data structures.
2922 Backend* backend_;
2923 // The function we are currently translating. NULL if not in a
2924 // function, e.g., the initializer of a global variable.
2925 Named_object* function_;
2926 // The block we are currently translating. NULL if not in a
2927 // function.
2928 Block *block_;
2929 // The backend representation of the current block. NULL if block_
2930 // is NULL.
2931 Bblock* bblock_;
2932 // Whether this is being evaluated in a constant context. This is
2933 // used for type descriptor initializers.
2934 bool is_const_;
2937 // Runtime error codes. These must match the values in
2938 // libgo/runtime/go-runtime-error.c.
2940 // Slice index out of bounds: negative or larger than the length of
2941 // the slice.
2942 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2944 // Array index out of bounds.
2945 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2947 // String index out of bounds.
2948 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2950 // Slice slice out of bounds: negative or larger than the length of
2951 // the slice or high bound less than low bound.
2952 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2954 // Array slice out of bounds.
2955 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2957 // String slice out of bounds.
2958 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2960 // Dereference of nil pointer. This is used when there is a
2961 // dereference of a pointer to a very large struct or array, to ensure
2962 // that a gigantic array is not used a proxy to access random memory
2963 // locations.
2964 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2966 // Slice length or capacity out of bounds in make: negative or
2967 // overflow or length greater than capacity.
2968 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2970 // Map capacity out of bounds in make: negative or overflow.
2971 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2973 // Channel capacity out of bounds in make: negative or overflow.
2974 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2976 // Division by zero.
2977 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2979 // This is used by some of the langhooks.
2980 extern Gogo* go_get_gogo();
2982 // Whether we have seen any errors. FIXME: Replace with a backend
2983 // interface.
2984 extern bool saw_errors();
2986 #endif // !defined(GO_GOGO_H)