2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / go / gofrontend / gogo.h
blob9c5f8cb4a6bebe5c92b068e5b0140efeee73342c
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 Method;
41 class Methods;
42 class Named_object;
43 class Label;
44 class Translate_context;
45 class Backend;
46 class Export;
47 class Import;
48 class Bexpression;
49 class Bstatement;
50 class Bblock;
51 class Bvariable;
52 class Blabel;
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 // a prefix of the package name, so that hidden symbols in different
142 // packages do 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->unique_prefix()
149 + '.' + this->package_name()
150 + '.' + name));
153 // Unpack a name which may have been hidden. Returns the
154 // user-visible name of the object.
155 static std::string
156 unpack_hidden_name(const std::string& name)
157 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
159 // Return whether a possibly packed name is hidden.
160 static bool
161 is_hidden_name(const std::string& name)
162 { return name[0] == '.'; }
164 // Return the package prefix of a hidden name.
165 static std::string
166 hidden_name_prefix(const std::string& name)
168 go_assert(Gogo::is_hidden_name(name));
169 return name.substr(1, name.rfind('.') - 1);
172 // Given a name which may or may not have been hidden, return the
173 // name to use in an error message.
174 static std::string
175 message_name(const std::string& name);
177 // Return whether a name is the blank identifier _.
178 static bool
179 is_sink_name(const std::string& name)
181 return (name[0] == '.'
182 && name[name.length() - 1] == '_'
183 && name[name.length() - 2] == '.');
186 // Return the unique prefix to use for all exported symbols.
187 const std::string&
188 unique_prefix() const;
190 // Set the unique prefix.
191 void
192 set_unique_prefix(const std::string&);
194 // Return the priority to use for the package we are compiling.
195 // This is two more than the largest priority of any package we
196 // import.
198 package_priority() const;
200 // Import a package. FILENAME is the file name argument, LOCAL_NAME
201 // is the local name to give to the package. If LOCAL_NAME is empty
202 // the declarations are added to the global scope.
203 void
204 import_package(const std::string& filename, const std::string& local_name,
205 bool is_local_name_exported, Location);
207 // Whether we are the global binding level.
208 bool
209 in_global_scope() const;
211 // Look up a name in the current binding contours.
212 Named_object*
213 lookup(const std::string&, Named_object** pfunction) const;
215 // Look up a name in the current block.
216 Named_object*
217 lookup_in_block(const std::string&) const;
219 // Look up a name in the global namespace--the universal scope.
220 Named_object*
221 lookup_global(const char*) const;
223 // Add a new imported package. REAL_NAME is the real name of the
224 // package. ALIAS is the alias of the package; this may be the same
225 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
226 // this package should be added to the global namespace; this is
227 // true if the alias is ".". LOCATION is the location of the import
228 // statement. This returns the new package, or NULL on error.
229 Package*
230 add_imported_package(const std::string& real_name, const std::string& alias,
231 bool is_alias_exported,
232 const std::string& unique_prefix,
233 Location location,
234 bool* padd_to_globals);
236 // Register a package. This package may or may not be imported.
237 // This returns the Package structure for the package, creating if
238 // it necessary.
239 Package*
240 register_package(const std::string& name, const std::string& unique_prefix,
241 Location);
243 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
244 // method function should be added to the type of its receiver.
245 Named_object*
246 start_function(const std::string& name, Function_type* type,
247 bool add_method_to_type, Location);
249 // Finish compiling a function.
250 void
251 finish_function(Location);
253 // Return the current function.
254 Named_object*
255 current_function() const;
257 // Return the current block.
258 Block*
259 current_block();
261 // Start a new block. This is not initially associated with a
262 // function.
263 void
264 start_block(Location);
266 // Finish the current block and return it.
267 Block*
268 finish_block(Location);
270 // Declare an erroneous name. This is used to avoid knock-on errors
271 // after a parsing error.
272 Named_object*
273 add_erroneous_name(const std::string& name);
275 // Declare an unknown name. This is used while parsing. The name
276 // must be resolved by the end of the parse. Unknown names are
277 // always added at the package level.
278 Named_object*
279 add_unknown_name(const std::string& name, Location);
281 // Declare a function.
282 Named_object*
283 declare_function(const std::string&, Function_type*, Location);
285 // Declare a function at the package level. This is used for
286 // functions generated for a type.
287 Named_object*
288 declare_package_function(const std::string&, Function_type*, Location);
290 // Add a label.
291 Label*
292 add_label_definition(const std::string&, Location);
294 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
295 // report errors for a goto from the current location to the label
296 // location.
297 Label*
298 add_label_reference(const std::string&, Location,
299 bool issue_goto_errors);
301 // Return a snapshot of the current binding state.
302 Bindings_snapshot*
303 bindings_snapshot(Location);
305 // Add a statement to the current block.
306 void
307 add_statement(Statement*);
309 // Add a block to the current block.
310 void
311 add_block(Block*, Location);
313 // Add a constant.
314 Named_object*
315 add_constant(const Typed_identifier&, Expression*, int iota_value);
317 // Add a type.
318 void
319 add_type(const std::string&, Type*, Location);
321 // Add a named type. This is used for builtin types, and to add an
322 // imported type to the global scope.
323 void
324 add_named_type(Named_type*);
326 // Declare a type.
327 Named_object*
328 declare_type(const std::string&, Location);
330 // Declare a type at the package level. This is used when the
331 // parser sees an unknown name where a type name is required.
332 Named_object*
333 declare_package_type(const std::string&, Location);
335 // Define a type which was already declared.
336 void
337 define_type(Named_object*, Named_type*);
339 // Add a variable.
340 Named_object*
341 add_variable(const std::string&, Variable*);
343 // Add a sink--a reference to the blank identifier _.
344 Named_object*
345 add_sink();
347 // Add a type which needs to be verified. This is used for sink
348 // types, just to give appropriate error messages.
349 void
350 add_type_to_verify(Type* type);
352 // Add a named object to the current namespace. This is used for
353 // import . "package".
354 void
355 add_named_object(Named_object*);
357 // Mark all local variables in current bindings as used. This is
358 // used when there is a parse error to avoid useless errors.
359 void
360 mark_locals_used();
362 // Return a name to use for a thunk function. A thunk function is
363 // one we create during the compilation, for a go statement or a
364 // defer statement or a method expression.
365 static std::string
366 thunk_name();
368 // Return whether an object is a thunk.
369 static bool
370 is_thunk(const Named_object*);
372 // Note that we've seen an interface type. This is used to build
373 // all required interface method tables.
374 void
375 record_interface_type(Interface_type*);
377 // Note that we need an initialization function.
378 void
379 set_need_init_fn()
380 { this->need_init_fn_ = true; }
382 // Clear out all names in file scope. This is called when we start
383 // parsing a new file.
384 void
385 clear_file_scope();
387 // Queue up a type-specific function to be written out. This is
388 // used when a type-specific function is needed when not at the top
389 // level.
390 void
391 queue_specific_type_function(Type* type, Named_type* name,
392 const std::string& hash_name,
393 Function_type* hash_fntype,
394 const std::string& equal_name,
395 Function_type* equal_fntype);
397 // Write out queued specific type functions.
398 void
399 write_specific_type_functions();
401 // Whether we are done writing out specific type functions.
402 bool
403 specific_type_functions_are_written() const
404 { return this->specific_type_functions_are_written_; }
406 // Traverse the tree. See the Traverse class.
407 void
408 traverse(Traverse*);
410 // Define the predeclared global names.
411 void
412 define_global_names();
414 // Verify and complete all types.
415 void
416 verify_types();
418 // Lower the parse tree.
419 void
420 lower_parse_tree();
422 // Lower all the statements in a block.
423 void
424 lower_block(Named_object* function, Block*);
426 // Lower an expression.
427 void
428 lower_expression(Named_object* function, Statement_inserter*, Expression**);
430 // Lower a constant.
431 void
432 lower_constant(Named_object*);
434 // Finalize the method lists and build stub methods for named types.
435 void
436 finalize_methods();
438 // Work out the types to use for unspecified variables and
439 // constants.
440 void
441 determine_types();
443 // Type check the program.
444 void
445 check_types();
447 // Check the types in a single block. This is used for complicated
448 // go statements.
449 void
450 check_types_in_block(Block*);
452 // Check for return statements.
453 void
454 check_return_statements();
456 // Do all exports.
457 void
458 do_exports();
460 // Add an import control function for an imported package to the
461 // list.
462 void
463 add_import_init_fn(const std::string& package_name,
464 const std::string& init_name, int prio);
466 // Turn short-cut operators (&&, ||) into explicit if statements.
467 void
468 remove_shortcuts();
470 // Use temporary variables to force order of evaluation.
471 void
472 order_evaluations();
474 // Build thunks for functions which call recover.
475 void
476 build_recover_thunks();
478 // Simplify statements which might use thunks: go and defer
479 // statements.
480 void
481 simplify_thunk_statements();
483 // Dump AST if -fgo-dump-ast is set
484 void
485 dump_ast(const char* basename);
487 // Convert named types to the backend representation.
488 void
489 convert_named_types();
491 // Convert named types in a list of bindings.
492 void
493 convert_named_types_in_bindings(Bindings*);
495 // True if named types have been converted to the backend
496 // representation.
497 bool
498 named_types_are_converted() const
499 { return this->named_types_are_converted_; }
501 // Write out the global values.
502 void
503 write_globals();
505 // Create trees for implicit builtin functions.
506 void
507 define_builtin_function_trees();
509 // Build a call to a builtin function. PDECL should point to a NULL
510 // initialized static pointer which will hold the fndecl. NAME is
511 // the name of the function. NARGS is the number of arguments.
512 // RETTYPE is the return type. It is followed by NARGS pairs of
513 // type and argument (both trees).
514 static tree
515 call_builtin(tree* pdecl, Location, const char* name, int nargs,
516 tree rettype, ...);
518 // Build a call to the runtime error function.
519 static tree
520 runtime_error(int code, Location);
522 // Build a builtin struct with a list of fields.
523 static tree
524 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
525 int nfields, ...);
527 // Mark a function declaration as a builtin library function.
528 static void
529 mark_fndecl_as_builtin_library(tree fndecl);
531 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
532 // the slice. VALUES points to the values. COUNT is the size,
533 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
534 // COUNT.
535 static tree
536 slice_constructor(tree slice_type_tree, tree values, tree count,
537 tree capacity);
539 // Build required interface method tables.
540 void
541 build_interface_method_tables();
543 // Build an interface method table for a type: a list of function
544 // pointers, one for each interface method. This returns a decl.
545 tree
546 interface_method_table_for_type(const Interface_type*, Named_type*,
547 bool is_pointer);
549 // Return a tree which allocate SIZE bytes to hold values of type
550 // TYPE.
551 tree
552 allocate_memory(Type *type, tree size, Location);
554 // Return a type to use for pointer to const char.
555 static tree
556 const_char_pointer_type_tree();
558 // Build a string constant with the right type.
559 static tree
560 string_constant_tree(const std::string&);
562 // Build a Go string constant. This returns a pointer to the
563 // constant.
564 tree
565 go_string_constant_tree(const std::string&);
567 // Receive a value from a channel.
568 static tree
569 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
570 Location);
572 // Make a trampoline which calls FNADDR passing CLOSURE.
573 tree
574 make_trampoline(tree fnaddr, tree closure, Location);
576 private:
577 // During parsing, we keep a stack of functions. Each function on
578 // the stack is one that we are currently parsing. For each
579 // function, we keep track of the current stack of blocks.
580 struct Open_function
582 // The function.
583 Named_object* function;
584 // The stack of active blocks in the function.
585 std::vector<Block*> blocks;
588 // The stack of functions.
589 typedef std::vector<Open_function> Open_functions;
591 // Set up the built-in unsafe package.
592 void
593 import_unsafe(const std::string&, bool is_exported, Location);
595 // Add a new imported package.
596 Named_object*
597 add_package(const std::string& real_name, const std::string& alias,
598 const std::string& unique_prefix, Location location);
600 // Return the current binding contour.
601 Bindings*
602 current_bindings();
604 const Bindings*
605 current_bindings() const;
607 // Get the name of the magic initialization function.
608 const std::string&
609 get_init_fn_name();
611 // Get the decl for the magic initialization function.
612 tree
613 initialization_function_decl();
615 // Write the magic initialization function.
616 void
617 write_initialization_function(tree fndecl, tree init_stmt_list);
619 // Initialize imported packages.
620 void
621 init_imports(tree*);
623 // Register variables with the garbage collector.
624 void
625 register_gc_vars(const std::vector<Named_object*>&, tree*);
627 // Build a pointer to a Go string constant. This returns a pointer
628 // to the pointer.
629 tree
630 ptr_go_string_constant_tree(const std::string&);
632 // Return the type of a trampoline.
633 static tree
634 trampoline_type_tree();
636 // Type used to map import names to packages.
637 typedef std::map<std::string, Package*> Imports;
639 // Type used to map package names to packages.
640 typedef std::map<std::string, Package*> Packages;
642 // Type used to map special names in the sys package.
643 typedef std::map<std::string, std::string> Sys_names;
645 // Type used to queue writing a type specific function.
646 struct Specific_type_function
648 Type* type;
649 Named_type* name;
650 std::string hash_name;
651 Function_type* hash_fntype;
652 std::string equal_name;
653 Function_type* equal_fntype;
655 Specific_type_function(Type* atype, Named_type* aname,
656 const std::string& ahash_name,
657 Function_type* ahash_fntype,
658 const std::string& aequal_name,
659 Function_type* aequal_fntype)
660 : type(atype), name(aname), hash_name(ahash_name),
661 hash_fntype(ahash_fntype), equal_name(aequal_name),
662 equal_fntype(aequal_fntype)
666 // The backend generator.
667 Backend* backend_;
668 // The object used to keep track of file names and line numbers.
669 Linemap* linemap_;
670 // The package we are compiling.
671 Package* package_;
672 // The list of currently open functions during parsing.
673 Open_functions functions_;
674 // The global binding contour. This includes the builtin functions
675 // and the package we are compiling.
676 Bindings* globals_;
677 // Mapping from import file names to packages.
678 Imports imports_;
679 // Whether the magic unsafe package was imported.
680 bool imported_unsafe_;
681 // Mapping from package names we have seen to packages. This does
682 // not include the package we are compiling.
683 Packages packages_;
684 // The functions named "init", if there are any.
685 std::vector<Named_object*> init_functions_;
686 // Whether we need a magic initialization function.
687 bool need_init_fn_;
688 // The name of the magic initialization function.
689 std::string init_fn_name_;
690 // A list of import control variables for packages that we import.
691 std::set<Import_init> imported_init_fns_;
692 // The unique prefix used for all global symbols.
693 std::string unique_prefix_;
694 // Whether an explicit unique prefix was set by -fgo-prefix.
695 bool unique_prefix_specified_;
696 // A list of types to verify.
697 std::vector<Type*> verify_types_;
698 // A list of interface types defined while parsing.
699 std::vector<Interface_type*> interface_types_;
700 // Type specific functions to write out.
701 std::vector<Specific_type_function*> specific_type_functions_;
702 // Whether we are done writing out specific type functions.
703 bool specific_type_functions_are_written_;
704 // Whether named types have been converted.
705 bool named_types_are_converted_;
708 // A block of statements.
710 class Block
712 public:
713 Block(Block* enclosing, Location);
715 // Return the enclosing block.
716 const Block*
717 enclosing() const
718 { return this->enclosing_; }
720 // Return the bindings of the block.
721 Bindings*
722 bindings()
723 { return this->bindings_; }
725 const Bindings*
726 bindings() const
727 { return this->bindings_; }
729 // Look at the block's statements.
730 const std::vector<Statement*>*
731 statements() const
732 { return &this->statements_; }
734 // Return the start location. This is normally the location of the
735 // left curly brace which starts the block.
736 Location
737 start_location() const
738 { return this->start_location_; }
740 // Return the end location. This is normally the location of the
741 // right curly brace which ends the block.
742 Location
743 end_location() const
744 { return this->end_location_; }
746 // Add a statement to the block.
747 void
748 add_statement(Statement*);
750 // Add a statement to the front of the block.
751 void
752 add_statement_at_front(Statement*);
754 // Replace a statement in a block.
755 void
756 replace_statement(size_t index, Statement*);
758 // Add a Statement before statement number INDEX.
759 void
760 insert_statement_before(size_t index, Statement*);
762 // Add a Statement after statement number INDEX.
763 void
764 insert_statement_after(size_t index, Statement*);
766 // Set the end location of the block.
767 void
768 set_end_location(Location location)
769 { this->end_location_ = location; }
771 // Traverse the tree.
773 traverse(Traverse*);
775 // Set final types for unspecified variables and constants.
776 void
777 determine_types();
779 // Return true if execution of this block may fall through to the
780 // next block.
781 bool
782 may_fall_through() const;
784 // Convert the block to the backend representation.
785 Bblock*
786 get_backend(Translate_context*);
788 // Iterate over statements.
790 typedef std::vector<Statement*>::iterator iterator;
792 iterator
793 begin()
794 { return this->statements_.begin(); }
796 iterator
797 end()
798 { return this->statements_.end(); }
800 private:
801 // Enclosing block.
802 Block* enclosing_;
803 // Statements in the block.
804 std::vector<Statement*> statements_;
805 // Binding contour.
806 Bindings* bindings_;
807 // Location of start of block.
808 Location start_location_;
809 // Location of end of block.
810 Location end_location_;
813 // A function.
815 class Function
817 public:
818 Function(Function_type* type, Function*, Block*, Location);
820 // Return the function's type.
821 Function_type*
822 type() const
823 { return this->type_; }
825 // Return the enclosing function if there is one.
826 Function*
827 enclosing()
828 { return this->enclosing_; }
830 // Set the enclosing function. This is used when building thunks
831 // for functions which call recover.
832 void
833 set_enclosing(Function* enclosing)
835 go_assert(this->enclosing_ == NULL);
836 this->enclosing_ = enclosing;
839 // The result variables.
840 typedef std::vector<Named_object*> Results;
842 // Create the result variables in the outer block.
843 void
844 create_result_variables(Gogo*);
846 // Update the named result variables when cloning a function which
847 // calls recover.
848 void
849 update_result_variables();
851 // Return the result variables.
852 Results*
853 result_variables()
854 { return this->results_; }
856 // Whether the result variables have names.
857 bool
858 results_are_named() const
859 { return this->results_are_named_; }
861 // Add a new field to the closure variable.
862 void
863 add_closure_field(Named_object* var, Location loc)
864 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
866 // Whether this function needs a closure.
867 bool
868 needs_closure() const
869 { return !this->closure_fields_.empty(); }
871 // Return the closure variable, creating it if necessary. This is
872 // passed to the function as a static chain parameter.
873 Named_object*
874 closure_var();
876 // Set the closure variable. This is used when building thunks for
877 // functions which call recover.
878 void
879 set_closure_var(Named_object* v)
881 go_assert(this->closure_var_ == NULL);
882 this->closure_var_ = v;
885 // Return the variable for a reference to field INDEX in the closure
886 // variable.
887 Named_object*
888 enclosing_var(unsigned int index)
890 go_assert(index < this->closure_fields_.size());
891 return closure_fields_[index].first;
894 // Set the type of the closure variable if there is one.
895 void
896 set_closure_type();
898 // Get the block of statements associated with the function.
899 Block*
900 block() const
901 { return this->block_; }
903 // Get the location of the start of the function.
904 Location
905 location() const
906 { return this->location_; }
908 // Return whether this function is actually a method.
909 bool
910 is_method() const;
912 // Add a label definition to the function.
913 Label*
914 add_label_definition(Gogo*, const std::string& label_name, Location);
916 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
917 // if we should report errors for a goto from the current location
918 // to the label location.
919 Label*
920 add_label_reference(Gogo*, const std::string& label_name,
921 Location, bool issue_goto_errors);
923 // Warn about labels that are defined but not used.
924 void
925 check_labels() const;
927 // Whether this function calls the predeclared recover function.
928 bool
929 calls_recover() const
930 { return this->calls_recover_; }
932 // Record that this function calls the predeclared recover function.
933 // This is set during the lowering pass.
934 void
935 set_calls_recover()
936 { this->calls_recover_ = true; }
938 // Whether this is a recover thunk function.
939 bool
940 is_recover_thunk() const
941 { return this->is_recover_thunk_; }
943 // Record that this is a thunk built for a function which calls
944 // recover.
945 void
946 set_is_recover_thunk()
947 { this->is_recover_thunk_ = true; }
949 // Whether this function already has a recover thunk.
950 bool
951 has_recover_thunk() const
952 { return this->has_recover_thunk_; }
954 // Record that this function already has a recover thunk.
955 void
956 set_has_recover_thunk()
957 { this->has_recover_thunk_ = true; }
959 // Swap with another function. Used only for the thunk which calls
960 // recover.
961 void
962 swap_for_recover(Function *);
964 // Traverse the tree.
966 traverse(Traverse*);
968 // Determine types in the function.
969 void
970 determine_types();
972 // Return the function's decl given an identifier.
973 tree
974 get_or_make_decl(Gogo*, Named_object*, tree id);
976 // Return the function's decl after it has been built.
977 tree
978 get_decl() const
980 go_assert(this->fndecl_ != NULL);
981 return this->fndecl_;
984 // Set the function decl to hold a tree of the function code.
985 void
986 build_tree(Gogo*, Named_object*);
988 // Get the value to return when not explicitly specified. May also
989 // add statements to execute first to STMT_LIST.
990 tree
991 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
993 // Get a tree for the variable holding the defer stack.
994 Expression*
995 defer_stack(Location);
997 // Export the function.
998 void
999 export_func(Export*, const std::string& name) const;
1001 // Export a function with a type.
1002 static void
1003 export_func_with_type(Export*, const std::string& name,
1004 const Function_type*);
1006 // Import a function.
1007 static void
1008 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1009 Typed_identifier_list** pparameters,
1010 Typed_identifier_list** presults, bool* is_varargs);
1012 private:
1013 // Type for mapping from label names to Label objects.
1014 typedef Unordered_map(std::string, Label*) Labels;
1016 tree
1017 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1019 tree
1020 copy_parm_to_heap(Gogo*, Named_object*, tree);
1022 void
1023 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1025 typedef std::vector<std::pair<Named_object*,
1026 Location> > Closure_fields;
1028 // The function's type.
1029 Function_type* type_;
1030 // The enclosing function. This is NULL when there isn't one, which
1031 // is the normal case.
1032 Function* enclosing_;
1033 // The result variables, if any.
1034 Results* results_;
1035 // If there is a closure, this is the list of variables which appear
1036 // in the closure. This is created by the parser, and then resolved
1037 // to a real type when we lower parse trees.
1038 Closure_fields closure_fields_;
1039 // The closure variable, passed as a parameter using the static
1040 // chain parameter. Normally NULL.
1041 Named_object* closure_var_;
1042 // The outer block of statements in the function.
1043 Block* block_;
1044 // The source location of the start of the function.
1045 Location location_;
1046 // Labels defined or referenced in the function.
1047 Labels labels_;
1048 // The function decl.
1049 tree fndecl_;
1050 // The defer stack variable. A pointer to this variable is used to
1051 // distinguish the defer stack for one function from another. This
1052 // is NULL unless we actually need a defer stack.
1053 Temporary_statement* defer_stack_;
1054 // True if the result variables are named.
1055 bool results_are_named_;
1056 // True if this function calls the predeclared recover function.
1057 bool calls_recover_;
1058 // True if this a thunk built for a function which calls recover.
1059 bool is_recover_thunk_;
1060 // True if this function already has a recover thunk.
1061 bool has_recover_thunk_;
1064 // A snapshot of the current binding state.
1066 class Bindings_snapshot
1068 public:
1069 Bindings_snapshot(const Block*, Location);
1071 // Report any errors appropriate for a goto from the current binding
1072 // state of B to this one.
1073 void
1074 check_goto_from(const Block* b, Location);
1076 // Report any errors appropriate for a goto from this binding state
1077 // to the current state of B.
1078 void
1079 check_goto_to(const Block* b);
1081 private:
1082 bool
1083 check_goto_block(Location, const Block*, const Block*, size_t*);
1085 void
1086 check_goto_defs(Location, const Block*, size_t, size_t);
1088 // The current block.
1089 const Block* block_;
1090 // The number of names currently defined in each open block.
1091 // Element 0 is this->block_, element 1 is
1092 // this->block_->enclosing(), etc.
1093 std::vector<size_t> counts_;
1094 // The location where this snapshot was taken.
1095 Location location_;
1098 // A function declaration.
1100 class Function_declaration
1102 public:
1103 Function_declaration(Function_type* fntype, Location location)
1104 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1107 Function_type*
1108 type() const
1109 { return this->fntype_; }
1111 Location
1112 location() const
1113 { return this->location_; }
1115 const std::string&
1116 asm_name() const
1117 { return this->asm_name_; }
1119 // Set the assembler name.
1120 void
1121 set_asm_name(const std::string& asm_name)
1122 { this->asm_name_ = asm_name; }
1124 // Return a decl for the function given an identifier.
1125 tree
1126 get_or_make_decl(Gogo*, Named_object*, tree id);
1128 // Export a function declaration.
1129 void
1130 export_func(Export* exp, const std::string& name) const
1131 { Function::export_func_with_type(exp, name, this->fntype_); }
1133 private:
1134 // The type of the function.
1135 Function_type* fntype_;
1136 // The location of the declaration.
1137 Location location_;
1138 // The assembler name: this is the name to use in references to the
1139 // function. This is normally empty.
1140 std::string asm_name_;
1141 // The function decl if needed.
1142 tree fndecl_;
1145 // A variable.
1147 class Variable
1149 public:
1150 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1151 bool is_receiver, Location);
1153 // Get the type of the variable.
1154 Type*
1155 type();
1157 Type*
1158 type() const;
1160 // Return whether the type is defined yet.
1161 bool
1162 has_type() const;
1164 // Get the initial value.
1165 Expression*
1166 init() const
1167 { return this->init_; }
1169 // Return whether there are any preinit statements.
1170 bool
1171 has_pre_init() const
1172 { return this->preinit_ != NULL; }
1174 // Return the preinit statements if any.
1175 Block*
1176 preinit() const
1177 { return this->preinit_; }
1179 // Return whether this is a global variable.
1180 bool
1181 is_global() const
1182 { return this->is_global_; }
1184 // Return whether this is a function parameter.
1185 bool
1186 is_parameter() const
1187 { return this->is_parameter_; }
1189 // Return whether this is the receiver parameter of a method.
1190 bool
1191 is_receiver() const
1192 { return this->is_receiver_; }
1194 // Change this parameter to be a receiver. This is used when
1195 // creating the thunks created for functions which call recover.
1196 void
1197 set_is_receiver()
1199 go_assert(this->is_parameter_);
1200 this->is_receiver_ = true;
1203 // Change this parameter to not be a receiver. This is used when
1204 // creating the thunks created for functions which call recover.
1205 void
1206 set_is_not_receiver()
1208 go_assert(this->is_parameter_);
1209 this->is_receiver_ = false;
1212 // Return whether this is the varargs parameter of a function.
1213 bool
1214 is_varargs_parameter() const
1215 { return this->is_varargs_parameter_; }
1217 // Whether this variable's address is taken.
1218 bool
1219 is_address_taken() const
1220 { return this->is_address_taken_; }
1222 // Whether this variable should live in the heap.
1223 bool
1224 is_in_heap() const
1225 { return this->is_address_taken_ && !this->is_global_; }
1227 // Note that something takes the address of this variable.
1228 void
1229 set_address_taken()
1230 { this->is_address_taken_ = true; }
1232 // Return whether the address is taken but does not escape.
1233 bool
1234 is_non_escaping_address_taken() const
1235 { return this->is_non_escaping_address_taken_; }
1237 // Note that something takes the address of this variable such that
1238 // the address does not escape the function.
1239 void
1240 set_non_escaping_address_taken()
1241 { this->is_non_escaping_address_taken_ = true; }
1243 // Get the source location of the variable's declaration.
1244 Location
1245 location() const
1246 { return this->location_; }
1248 // Record that this is the varargs parameter of a function.
1249 void
1250 set_is_varargs_parameter()
1252 go_assert(this->is_parameter_);
1253 this->is_varargs_parameter_ = true;
1256 // Return whether the variable has been used.
1257 bool
1258 is_used() const
1259 { return this->is_used_; }
1261 // Mark that the variable has been used.
1262 void
1263 set_is_used()
1264 { this->is_used_ = true; }
1266 // Clear the initial value; used for error handling.
1267 void
1268 clear_init()
1269 { this->init_ = NULL; }
1271 // Set the initial value; used for converting shortcuts.
1272 void
1273 set_init(Expression* init)
1274 { this->init_ = init; }
1276 // Get the preinit block, a block of statements to be run before the
1277 // initialization expression.
1278 Block*
1279 preinit_block(Gogo*);
1281 // Add a statement to be run before the initialization expression.
1282 // This is only used for global variables.
1283 void
1284 add_preinit_statement(Gogo*, Statement*);
1286 // Lower the initialization expression after parsing is complete.
1287 void
1288 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1290 // A special case: the init value is used only to determine the
1291 // type. This is used if the variable is defined using := with the
1292 // comma-ok form of a map index or a receive expression. The init
1293 // value is actually the map index expression or receive expression.
1294 // We use this because we may not know the right type at parse time.
1295 void
1296 set_type_from_init_tuple()
1297 { this->type_from_init_tuple_ = true; }
1299 // Another special case: the init value is used only to determine
1300 // the type. This is used if the variable is defined using := with
1301 // a range clause. The init value is the range expression. The
1302 // type of the variable is the index type of the range expression
1303 // (i.e., the first value returned by a range).
1304 void
1305 set_type_from_range_index()
1306 { this->type_from_range_index_ = true; }
1308 // Another special case: like set_type_from_range_index, but the
1309 // type is the value type of the range expression (i.e., the second
1310 // value returned by a range).
1311 void
1312 set_type_from_range_value()
1313 { this->type_from_range_value_ = true; }
1315 // Another special case: the init value is used only to determine
1316 // the type. This is used if the variable is defined using := with
1317 // a case in a select statement. The init value is the channel.
1318 // The type of the variable is the channel's element type.
1319 void
1320 set_type_from_chan_element()
1321 { this->type_from_chan_element_ = true; }
1323 // After we lower the select statement, we once again set the type
1324 // from the initialization expression.
1325 void
1326 clear_type_from_chan_element()
1328 go_assert(this->type_from_chan_element_);
1329 this->type_from_chan_element_ = false;
1332 // Note that this variable was created for a type switch clause.
1333 void
1334 set_is_type_switch_var()
1335 { this->is_type_switch_var_ = true; }
1337 // Traverse the initializer expression.
1339 traverse_expression(Traverse*, unsigned int traverse_mask);
1341 // Determine the type of the variable if necessary.
1342 void
1343 determine_type();
1345 // Get the backend representation of the variable.
1346 Bvariable*
1347 get_backend_variable(Gogo*, Named_object*, const Package*,
1348 const std::string&);
1350 // Get the initial value of the variable as a tree. This may only
1351 // be called if has_pre_init() returns false.
1352 tree
1353 get_init_tree(Gogo*, Named_object* function);
1355 // Return a series of statements which sets the value of the
1356 // variable in DECL. This should only be called is has_pre_init()
1357 // returns true. DECL may be NULL for a sink variable.
1358 tree
1359 get_init_block(Gogo*, Named_object* function, tree decl);
1361 // Export the variable.
1362 void
1363 export_var(Export*, const std::string& name) const;
1365 // Import a variable.
1366 static void
1367 import_var(Import*, std::string* pname, Type** ptype);
1369 private:
1370 // The type of a tuple.
1371 Type*
1372 type_from_tuple(Expression*, bool) const;
1374 // The type of a range.
1375 Type*
1376 type_from_range(Expression*, bool, bool) const;
1378 // The element type of a channel.
1379 Type*
1380 type_from_chan_element(Expression*, bool) const;
1382 // The variable's type. This may be NULL if the type is set from
1383 // the expression.
1384 Type* type_;
1385 // The initial value. This may be NULL if the variable should be
1386 // initialized to the default value for the type.
1387 Expression* init_;
1388 // Statements to run before the init statement.
1389 Block* preinit_;
1390 // Location of variable definition.
1391 Location location_;
1392 // Backend representation.
1393 Bvariable* backend_;
1394 // Whether this is a global variable.
1395 bool is_global_ : 1;
1396 // Whether this is a function parameter.
1397 bool is_parameter_ : 1;
1398 // Whether this is the receiver parameter of a method.
1399 bool is_receiver_ : 1;
1400 // Whether this is the varargs parameter of a function.
1401 bool is_varargs_parameter_ : 1;
1402 // Whether this variable is ever referenced.
1403 bool is_used_ : 1;
1404 // Whether something takes the address of this variable. For a
1405 // local variable this implies that the variable has to be on the
1406 // heap.
1407 bool is_address_taken_ : 1;
1408 // Whether something takes the address of this variable such that
1409 // the address does not escape the function.
1410 bool is_non_escaping_address_taken_ : 1;
1411 // True if we have seen this variable in a traversal.
1412 bool seen_ : 1;
1413 // True if we have lowered the initialization expression.
1414 bool init_is_lowered_ : 1;
1415 // True if init is a tuple used to set the type.
1416 bool type_from_init_tuple_ : 1;
1417 // True if init is a range clause and the type is the index type.
1418 bool type_from_range_index_ : 1;
1419 // True if init is a range clause and the type is the value type.
1420 bool type_from_range_value_ : 1;
1421 // True if init is a channel and the type is the channel's element type.
1422 bool type_from_chan_element_ : 1;
1423 // True if this is a variable created for a type switch case.
1424 bool is_type_switch_var_ : 1;
1425 // True if we have determined types.
1426 bool determined_type_ : 1;
1429 // A variable which is really the name for a function return value, or
1430 // part of one.
1432 class Result_variable
1434 public:
1435 Result_variable(Type* type, Function* function, int index,
1436 Location location)
1437 : type_(type), function_(function), index_(index), location_(location),
1438 backend_(NULL), is_address_taken_(false),
1439 is_non_escaping_address_taken_(false)
1442 // Get the type of the result variable.
1443 Type*
1444 type() const
1445 { return this->type_; }
1447 // Get the function that this is associated with.
1448 Function*
1449 function() const
1450 { return this->function_; }
1452 // Index in the list of function results.
1454 index() const
1455 { return this->index_; }
1457 // The location of the variable definition.
1458 Location
1459 location() const
1460 { return this->location_; }
1462 // Whether this variable's address is taken.
1463 bool
1464 is_address_taken() const
1465 { return this->is_address_taken_; }
1467 // Note that something takes the address of this variable.
1468 void
1469 set_address_taken()
1470 { this->is_address_taken_ = true; }
1472 // Return whether the address is taken but does not escape.
1473 bool
1474 is_non_escaping_address_taken() const
1475 { return this->is_non_escaping_address_taken_; }
1477 // Note that something takes the address of this variable such that
1478 // the address does not escape the function.
1479 void
1480 set_non_escaping_address_taken()
1481 { this->is_non_escaping_address_taken_ = true; }
1483 // Whether this variable should live in the heap.
1484 bool
1485 is_in_heap() const
1486 { return this->is_address_taken_; }
1488 // Set the function. This is used when cloning functions which call
1489 // recover.
1490 void
1491 set_function(Function* function)
1492 { this->function_ = function; }
1494 // Get the backend representation of the variable.
1495 Bvariable*
1496 get_backend_variable(Gogo*, Named_object*, const std::string&);
1498 private:
1499 // Type of result variable.
1500 Type* type_;
1501 // Function with which this is associated.
1502 Function* function_;
1503 // Index in list of results.
1504 int index_;
1505 // Where the result variable is defined.
1506 Location location_;
1507 // Backend representation.
1508 Bvariable* backend_;
1509 // Whether something takes the address of this variable.
1510 bool is_address_taken_;
1511 // Whether something takes the address of this variable such that
1512 // the address does not escape the function.
1513 bool is_non_escaping_address_taken_;
1516 // The value we keep for a named constant. This lets us hold a type
1517 // and an expression.
1519 class Named_constant
1521 public:
1522 Named_constant(Type* type, Expression* expr, int iota_value,
1523 Location location)
1524 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1525 lowering_(false)
1528 Type*
1529 type() const
1530 { return this->type_; }
1532 Expression*
1533 expr() const
1534 { return this->expr_; }
1537 iota_value() const
1538 { return this->iota_value_; }
1540 Location
1541 location() const
1542 { return this->location_; }
1544 // Whether we are lowering.
1545 bool
1546 lowering() const
1547 { return this->lowering_; }
1549 // Set that we are lowering.
1550 void
1551 set_lowering()
1552 { this->lowering_ = true; }
1554 // We are no longer lowering.
1555 void
1556 clear_lowering()
1557 { this->lowering_ = false; }
1559 // Traverse the expression.
1561 traverse_expression(Traverse*);
1563 // Determine the type of the constant if necessary.
1564 void
1565 determine_type();
1567 // Indicate that we found and reported an error for this constant.
1568 void
1569 set_error();
1571 // Export the constant.
1572 void
1573 export_const(Export*, const std::string& name) const;
1575 // Import a constant.
1576 static void
1577 import_const(Import*, std::string*, Type**, Expression**);
1579 private:
1580 // The type of the constant.
1581 Type* type_;
1582 // The expression for the constant.
1583 Expression* expr_;
1584 // If the predeclared constant iota is used in EXPR_, this is the
1585 // value it will have. We do this because at parse time we don't
1586 // know whether the name "iota" will refer to the predeclared
1587 // constant or to something else. We put in the right value in when
1588 // we lower.
1589 int iota_value_;
1590 // The location of the definition.
1591 Location location_;
1592 // Whether we are currently lowering this constant.
1593 bool lowering_;
1596 // A type declaration.
1598 class Type_declaration
1600 public:
1601 Type_declaration(Location location)
1602 : location_(location), in_function_(NULL), methods_(),
1603 issued_warning_(false)
1606 // Return the location.
1607 Location
1608 location() const
1609 { return this->location_; }
1611 // Return the function in which this type is declared. This will
1612 // return NULL for a type declared in global scope.
1613 Named_object*
1614 in_function()
1615 { return this->in_function_; }
1617 // Set the function in which this type is declared.
1618 void
1619 set_in_function(Named_object* f)
1620 { this->in_function_ = f; }
1622 // Add a method to this type. This is used when methods are defined
1623 // before the type.
1624 Named_object*
1625 add_method(const std::string& name, Function* function);
1627 // Add a method declaration to this type.
1628 Named_object*
1629 add_method_declaration(const std::string& name, Package*,
1630 Function_type* type, Location location);
1632 // Return whether any methods were defined.
1633 bool
1634 has_methods() const;
1636 // Define methods when the real type is known.
1637 void
1638 define_methods(Named_type*);
1640 // This is called if we are trying to use this type. It returns
1641 // true if we should issue a warning.
1642 bool
1643 using_type();
1645 private:
1646 typedef std::vector<Named_object*> Methods;
1648 // The location of the type declaration.
1649 Location location_;
1650 // If this type is declared in a function, a pointer back to the
1651 // function in which it is defined.
1652 Named_object* in_function_;
1653 // Methods defined before the type is defined.
1654 Methods methods_;
1655 // True if we have issued a warning about a use of this type
1656 // declaration when it is undefined.
1657 bool issued_warning_;
1660 // An unknown object. These are created by the parser for forward
1661 // references to names which have not been seen before. In a correct
1662 // program, these will always point to a real definition by the end of
1663 // the parse. Because they point to another Named_object, these may
1664 // only be referenced by Unknown_expression objects.
1666 class Unknown_name
1668 public:
1669 Unknown_name(Location location)
1670 : location_(location), real_named_object_(NULL)
1673 // Return the location where this name was first seen.
1674 Location
1675 location() const
1676 { return this->location_; }
1678 // Return the real named object that this points to, or NULL if it
1679 // was never resolved.
1680 Named_object*
1681 real_named_object() const
1682 { return this->real_named_object_; }
1684 // Set the real named object that this points to.
1685 void
1686 set_real_named_object(Named_object* no);
1688 private:
1689 // The location where this name was first seen.
1690 Location location_;
1691 // The real named object when it is known.
1692 Named_object*
1693 real_named_object_;
1696 // A named object named. This is the result of a declaration. We
1697 // don't use a superclass because they all have to be handled
1698 // differently.
1700 class Named_object
1702 public:
1703 enum Classification
1705 // An uninitialized Named_object. We should never see this.
1706 NAMED_OBJECT_UNINITIALIZED,
1707 // An erroneous name. This indicates a parse error, to avoid
1708 // later errors about undefined references.
1709 NAMED_OBJECT_ERRONEOUS,
1710 // An unknown name. This is used for forward references. In a
1711 // correct program, these will all be resolved by the end of the
1712 // parse.
1713 NAMED_OBJECT_UNKNOWN,
1714 // A const.
1715 NAMED_OBJECT_CONST,
1716 // A type.
1717 NAMED_OBJECT_TYPE,
1718 // A forward type declaration.
1719 NAMED_OBJECT_TYPE_DECLARATION,
1720 // A var.
1721 NAMED_OBJECT_VAR,
1722 // A result variable in a function.
1723 NAMED_OBJECT_RESULT_VAR,
1724 // The blank identifier--the special variable named _.
1725 NAMED_OBJECT_SINK,
1726 // A func.
1727 NAMED_OBJECT_FUNC,
1728 // A forward func declaration.
1729 NAMED_OBJECT_FUNC_DECLARATION,
1730 // A package.
1731 NAMED_OBJECT_PACKAGE
1734 // Return the classification.
1735 Classification
1736 classification() const
1737 { return this->classification_; }
1739 // Classifiers.
1741 bool
1742 is_erroneous() const
1743 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1745 bool
1746 is_unknown() const
1747 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1749 bool
1750 is_const() const
1751 { return this->classification_ == NAMED_OBJECT_CONST; }
1753 bool
1754 is_type() const
1755 { return this->classification_ == NAMED_OBJECT_TYPE; }
1757 bool
1758 is_type_declaration() const
1759 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1761 bool
1762 is_variable() const
1763 { return this->classification_ == NAMED_OBJECT_VAR; }
1765 bool
1766 is_result_variable() const
1767 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1769 bool
1770 is_sink() const
1771 { return this->classification_ == NAMED_OBJECT_SINK; }
1773 bool
1774 is_function() const
1775 { return this->classification_ == NAMED_OBJECT_FUNC; }
1777 bool
1778 is_function_declaration() const
1779 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1781 bool
1782 is_package() const
1783 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1785 // Creators.
1787 static Named_object*
1788 make_erroneous_name(const std::string& name)
1789 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1791 static Named_object*
1792 make_unknown_name(const std::string& name, Location);
1794 static Named_object*
1795 make_constant(const Typed_identifier&, const Package*, Expression*,
1796 int iota_value);
1798 static Named_object*
1799 make_type(const std::string&, const Package*, Type*, Location);
1801 static Named_object*
1802 make_type_declaration(const std::string&, const Package*, Location);
1804 static Named_object*
1805 make_variable(const std::string&, const Package*, Variable*);
1807 static Named_object*
1808 make_result_variable(const std::string&, Result_variable*);
1810 static Named_object*
1811 make_sink();
1813 static Named_object*
1814 make_function(const std::string&, const Package*, Function*);
1816 static Named_object*
1817 make_function_declaration(const std::string&, const Package*, Function_type*,
1818 Location);
1820 static Named_object*
1821 make_package(const std::string& alias, Package* package);
1823 // Getters.
1825 Unknown_name*
1826 unknown_value()
1828 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1829 return this->u_.unknown_value;
1832 const Unknown_name*
1833 unknown_value() const
1835 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1836 return this->u_.unknown_value;
1839 Named_constant*
1840 const_value()
1842 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1843 return this->u_.const_value;
1846 const Named_constant*
1847 const_value() const
1849 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1850 return this->u_.const_value;
1853 Named_type*
1854 type_value()
1856 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1857 return this->u_.type_value;
1860 const Named_type*
1861 type_value() const
1863 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1864 return this->u_.type_value;
1867 Type_declaration*
1868 type_declaration_value()
1870 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1871 return this->u_.type_declaration;
1874 const Type_declaration*
1875 type_declaration_value() const
1877 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1878 return this->u_.type_declaration;
1881 Variable*
1882 var_value()
1884 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1885 return this->u_.var_value;
1888 const Variable*
1889 var_value() const
1891 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1892 return this->u_.var_value;
1895 Result_variable*
1896 result_var_value()
1898 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1899 return this->u_.result_var_value;
1902 const Result_variable*
1903 result_var_value() const
1905 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1906 return this->u_.result_var_value;
1909 Function*
1910 func_value()
1912 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1913 return this->u_.func_value;
1916 const Function*
1917 func_value() const
1919 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1920 return this->u_.func_value;
1923 Function_declaration*
1924 func_declaration_value()
1926 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1927 return this->u_.func_declaration_value;
1930 const Function_declaration*
1931 func_declaration_value() const
1933 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1934 return this->u_.func_declaration_value;
1937 Package*
1938 package_value()
1940 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1941 return this->u_.package_value;
1944 const Package*
1945 package_value() const
1947 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1948 return this->u_.package_value;
1951 const std::string&
1952 name() const
1953 { return this->name_; }
1955 // Return the name to use in an error message. The difference is
1956 // that if this Named_object is defined in a different package, this
1957 // will return PACKAGE.NAME.
1958 std::string
1959 message_name() const;
1961 const Package*
1962 package() const
1963 { return this->package_; }
1965 // Resolve an unknown value if possible. This returns the same
1966 // Named_object or a new one.
1967 Named_object*
1968 resolve()
1970 Named_object* ret = this;
1971 if (this->is_unknown())
1973 Named_object* r = this->unknown_value()->real_named_object();
1974 if (r != NULL)
1975 ret = r;
1977 return ret;
1980 const Named_object*
1981 resolve() const
1983 const Named_object* ret = this;
1984 if (this->is_unknown())
1986 const Named_object* r = this->unknown_value()->real_named_object();
1987 if (r != NULL)
1988 ret = r;
1990 return ret;
1993 // The location where this object was defined or referenced.
1994 Location
1995 location() const;
1997 // Convert a variable to the backend representation.
1998 Bvariable*
1999 get_backend_variable(Gogo*, Named_object* function);
2001 // Return a tree for the external identifier for this object.
2002 tree
2003 get_id(Gogo*);
2005 // Return a tree representing this object.
2006 tree
2007 get_tree(Gogo*, Named_object* function);
2009 // Define a type declaration.
2010 void
2011 set_type_value(Named_type*);
2013 // Define a function declaration.
2014 void
2015 set_function_value(Function*);
2017 // Declare an unknown name as a type declaration.
2018 void
2019 declare_as_type();
2021 // Export this object.
2022 void
2023 export_named_object(Export*) const;
2025 private:
2026 Named_object(const std::string&, const Package*, Classification);
2028 // The name of the object.
2029 std::string name_;
2030 // The package that this object is in. This is NULL if it is in the
2031 // file we are compiling.
2032 const Package* package_;
2033 // The type of object this is.
2034 Classification classification_;
2035 // The real data.
2036 union
2038 Unknown_name* unknown_value;
2039 Named_constant* const_value;
2040 Named_type* type_value;
2041 Type_declaration* type_declaration;
2042 Variable* var_value;
2043 Result_variable* result_var_value;
2044 Function* func_value;
2045 Function_declaration* func_declaration_value;
2046 Package* package_value;
2047 } u_;
2048 // The DECL tree for this object if we have already converted it.
2049 tree tree_;
2052 // A binding contour. This binds names to objects.
2054 class Bindings
2056 public:
2057 // Type for mapping from names to objects.
2058 typedef Unordered_map(std::string, Named_object*) Contour;
2060 Bindings(Bindings* enclosing);
2062 // Add an erroneous name.
2063 Named_object*
2064 add_erroneous_name(const std::string& name)
2065 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2067 // Add an unknown name.
2068 Named_object*
2069 add_unknown_name(const std::string& name, Location location)
2071 return this->add_named_object(Named_object::make_unknown_name(name,
2072 location));
2075 // Add a constant.
2076 Named_object*
2077 add_constant(const Typed_identifier& tid, const Package* package,
2078 Expression* expr, int iota_value)
2080 return this->add_named_object(Named_object::make_constant(tid, package,
2081 expr,
2082 iota_value));
2085 // Add a type.
2086 Named_object*
2087 add_type(const std::string& name, const Package* package, Type* type,
2088 Location location)
2090 return this->add_named_object(Named_object::make_type(name, package, type,
2091 location));
2094 // Add a named type. This is used for builtin types, and to add an
2095 // imported type to the global scope.
2096 Named_object*
2097 add_named_type(Named_type* named_type);
2099 // Add a type declaration.
2100 Named_object*
2101 add_type_declaration(const std::string& name, const Package* package,
2102 Location location)
2104 Named_object* no = Named_object::make_type_declaration(name, package,
2105 location);
2106 return this->add_named_object(no);
2109 // Add a variable.
2110 Named_object*
2111 add_variable(const std::string& name, const Package* package,
2112 Variable* variable)
2114 return this->add_named_object(Named_object::make_variable(name, package,
2115 variable));
2118 // Add a result variable.
2119 Named_object*
2120 add_result_variable(const std::string& name, Result_variable* result)
2122 return this->add_named_object(Named_object::make_result_variable(name,
2123 result));
2126 // Add a function.
2127 Named_object*
2128 add_function(const std::string& name, const Package*, Function* function);
2130 // Add a function declaration.
2131 Named_object*
2132 add_function_declaration(const std::string& name, const Package* package,
2133 Function_type* type, Location location);
2135 // Add a package. The location is the location of the import
2136 // statement.
2137 Named_object*
2138 add_package(const std::string& alias, Package* package)
2140 Named_object* no = Named_object::make_package(alias, package);
2141 return this->add_named_object(no);
2144 // Define a type which was already declared.
2145 void
2146 define_type(Named_object*, Named_type*);
2148 // Add a method to the list of objects. This is not added to the
2149 // lookup table.
2150 void
2151 add_method(Named_object*);
2153 // Add a named object to this binding.
2154 Named_object*
2155 add_named_object(Named_object* no)
2156 { return this->add_named_object_to_contour(&this->bindings_, no); }
2158 // Clear all names in file scope from the bindings.
2159 void
2160 clear_file_scope();
2162 // Look up a name in this binding contour and in any enclosing
2163 // binding contours. This returns NULL if the name is not found.
2164 Named_object*
2165 lookup(const std::string&) const;
2167 // Look up a name in this binding contour without looking in any
2168 // enclosing binding contours. Returns NULL if the name is not found.
2169 Named_object*
2170 lookup_local(const std::string&) const;
2172 // Remove a name.
2173 void
2174 remove_binding(Named_object*);
2176 // Mark all variables as used. This is used for some types of parse
2177 // error.
2178 void
2179 mark_locals_used();
2181 // Traverse the tree. See the Traverse class.
2183 traverse(Traverse*, bool is_global);
2185 // Iterate over definitions. This does not include things which
2186 // were only declared.
2188 typedef std::vector<Named_object*>::const_iterator
2189 const_definitions_iterator;
2191 const_definitions_iterator
2192 begin_definitions() const
2193 { return this->named_objects_.begin(); }
2195 const_definitions_iterator
2196 end_definitions() const
2197 { return this->named_objects_.end(); }
2199 // Return the number of definitions.
2200 size_t
2201 size_definitions() const
2202 { return this->named_objects_.size(); }
2204 // Return whether there are no definitions.
2205 bool
2206 empty_definitions() const
2207 { return this->named_objects_.empty(); }
2209 // Iterate over declarations. This is everything that has been
2210 // declared, which includes everything which has been defined.
2212 typedef Contour::const_iterator const_declarations_iterator;
2214 const_declarations_iterator
2215 begin_declarations() const
2216 { return this->bindings_.begin(); }
2218 const_declarations_iterator
2219 end_declarations() const
2220 { return this->bindings_.end(); }
2222 // Return the number of declarations.
2223 size_t
2224 size_declarations() const
2225 { return this->bindings_.size(); }
2227 // Return whether there are no declarations.
2228 bool
2229 empty_declarations() const
2230 { return this->bindings_.empty(); }
2232 // Return the first declaration.
2233 Named_object*
2234 first_declaration()
2235 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2237 private:
2238 Named_object*
2239 add_named_object_to_contour(Contour*, Named_object*);
2241 Named_object*
2242 new_definition(Named_object*, Named_object*);
2244 // Enclosing bindings.
2245 Bindings* enclosing_;
2246 // The list of objects.
2247 std::vector<Named_object*> named_objects_;
2248 // The mapping from names to objects.
2249 Contour bindings_;
2252 // A label.
2254 class Label
2256 public:
2257 Label(const std::string& name)
2258 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2259 refs_(), is_used_(false), blabel_(NULL)
2262 // Return the label's name.
2263 const std::string&
2264 name() const
2265 { return this->name_; }
2267 // Return whether the label has been defined.
2268 bool
2269 is_defined() const
2270 { return !Linemap::is_unknown_location(this->location_); }
2272 // Return whether the label has been used.
2273 bool
2274 is_used() const
2275 { return this->is_used_; }
2277 // Record that the label is used.
2278 void
2279 set_is_used()
2280 { this->is_used_ = true; }
2282 // Return the location of the definition.
2283 Location
2284 location() const
2285 { return this->location_; }
2287 // Return the bindings snapshot.
2288 Bindings_snapshot*
2289 snapshot() const
2290 { return this->snapshot_; }
2292 // Add a snapshot of a goto which refers to this label.
2293 void
2294 add_snapshot_ref(Bindings_snapshot* snapshot)
2296 go_assert(Linemap::is_unknown_location(this->location_));
2297 this->refs_.push_back(snapshot);
2300 // Return the list of snapshots of goto statements which refer to
2301 // this label.
2302 const std::vector<Bindings_snapshot*>&
2303 refs() const
2304 { return this->refs_; }
2306 // Clear the references.
2307 void
2308 clear_refs();
2310 // Define the label at LOCATION with the given bindings snapshot.
2311 void
2312 define(Location location, Bindings_snapshot* snapshot)
2314 go_assert(Linemap::is_unknown_location(this->location_)
2315 && this->snapshot_ == NULL);
2316 this->location_ = location;
2317 this->snapshot_ = snapshot;
2320 // Return the backend representation for this label.
2321 Blabel*
2322 get_backend_label(Translate_context*);
2324 // Return an expression for the address of this label. This is used
2325 // to get the return address of a deferred function to see whether
2326 // the function may call recover.
2327 Bexpression*
2328 get_addr(Translate_context*, Location location);
2330 private:
2331 // The name of the label.
2332 std::string name_;
2333 // The location of the definition. This is 0 if the label has not
2334 // yet been defined.
2335 Location location_;
2336 // A snapshot of the set of bindings defined at this label, used to
2337 // issue errors about invalid goto statements.
2338 Bindings_snapshot* snapshot_;
2339 // A list of snapshots of goto statements which refer to this label.
2340 std::vector<Bindings_snapshot*> refs_;
2341 // Whether the label has been used.
2342 bool is_used_;
2343 // The backend representation.
2344 Blabel* blabel_;
2347 // An unnamed label. These are used when lowering loops.
2349 class Unnamed_label
2351 public:
2352 Unnamed_label(Location location)
2353 : location_(location), blabel_(NULL)
2356 // Get the location where the label is defined.
2357 Location
2358 location() const
2359 { return this->location_; }
2361 // Set the location where the label is defined.
2362 void
2363 set_location(Location location)
2364 { this->location_ = location; }
2366 // Return a statement which defines this label.
2367 Bstatement*
2368 get_definition(Translate_context*);
2370 // Return a goto to this label from LOCATION.
2371 Bstatement*
2372 get_goto(Translate_context*, Location location);
2374 private:
2375 // Return the backend representation.
2376 Blabel*
2377 get_blabel(Translate_context*);
2379 // The location where the label is defined.
2380 Location location_;
2381 // The backend representation of this label.
2382 Blabel* blabel_;
2385 // An imported package.
2387 class Package
2389 public:
2390 Package(const std::string& name, const std::string& unique_prefix,
2391 Location location);
2393 // The real name of this package. This may be different from the
2394 // name in the associated Named_object if the import statement used
2395 // an alias.
2396 const std::string&
2397 name() const
2398 { return this->name_; }
2400 // Return the location of the import statement.
2401 Location
2402 location() const
2403 { return this->location_; }
2405 // Get the unique prefix used for all symbols exported from this
2406 // package.
2407 const std::string&
2408 unique_prefix() const
2410 go_assert(!this->unique_prefix_.empty());
2411 return this->unique_prefix_;
2414 // The priority of this package. The init function of packages with
2415 // lower priority must be run before the init function of packages
2416 // with higher priority.
2418 priority() const
2419 { return this->priority_; }
2421 // Set the priority.
2422 void
2423 set_priority(int priority);
2425 // Return the bindings.
2426 Bindings*
2427 bindings()
2428 { return this->bindings_; }
2430 // Whether some symbol from the package was used.
2431 bool
2432 used() const
2433 { return this->used_; }
2435 // Note that some symbol from this package was used.
2436 void
2437 set_used() const
2438 { this->used_ = true; }
2440 // Clear the used field for the next file.
2441 void
2442 clear_used()
2443 { this->used_ = false; }
2445 // Whether this package was imported in the current file.
2446 bool
2447 is_imported() const
2448 { return this->is_imported_; }
2450 // Note that this package was imported in the current file.
2451 void
2452 set_is_imported()
2453 { this->is_imported_ = true; }
2455 // Clear the imported field for the next file.
2456 void
2457 clear_is_imported()
2458 { this->is_imported_ = false; }
2460 // Whether this package was imported with a name of "_".
2461 bool
2462 uses_sink_alias() const
2463 { return this->uses_sink_alias_; }
2465 // Note that this package was imported with a name of "_".
2466 void
2467 set_uses_sink_alias()
2468 { this->uses_sink_alias_ = true; }
2470 // Clear the sink alias field for the next file.
2471 void
2472 clear_uses_sink_alias()
2473 { this->uses_sink_alias_ = false; }
2475 // Look up a name in the package. Returns NULL if the name is not
2476 // found.
2477 Named_object*
2478 lookup(const std::string& name) const
2479 { return this->bindings_->lookup(name); }
2481 // Set the location of the package. This is used if it is seen in a
2482 // different import before it is really imported.
2483 void
2484 set_location(Location location)
2485 { this->location_ = location; }
2487 // Add a constant to the package.
2488 Named_object*
2489 add_constant(const Typed_identifier& tid, Expression* expr)
2490 { return this->bindings_->add_constant(tid, this, expr, 0); }
2492 // Add a type to the package.
2493 Named_object*
2494 add_type(const std::string& name, Type* type, Location location)
2495 { return this->bindings_->add_type(name, this, type, location); }
2497 // Add a type declaration to the package.
2498 Named_object*
2499 add_type_declaration(const std::string& name, Location location)
2500 { return this->bindings_->add_type_declaration(name, this, location); }
2502 // Add a variable to the package.
2503 Named_object*
2504 add_variable(const std::string& name, Variable* variable)
2505 { return this->bindings_->add_variable(name, this, variable); }
2507 // Add a function declaration to the package.
2508 Named_object*
2509 add_function_declaration(const std::string& name, Function_type* type,
2510 Location loc)
2511 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2513 // Determine types of constants.
2514 void
2515 determine_types();
2517 private:
2518 // The real name of this package.
2519 std::string name_;
2520 // The unique prefix for all exported global symbols.
2521 std::string unique_prefix_;
2522 // The names in this package.
2523 Bindings* bindings_;
2524 // The priority of this package. A package has a priority higher
2525 // than the priority of all of the packages that it imports. This
2526 // is used to run init functions in the right order.
2527 int priority_;
2528 // The location of the import statement.
2529 Location location_;
2530 // True if some name from this package was used. This is mutable
2531 // because we can use a package even if we have a const pointer to
2532 // it.
2533 mutable bool used_;
2534 // True if this package was imported in the current file.
2535 bool is_imported_;
2536 // True if this package was imported with a name of "_".
2537 bool uses_sink_alias_;
2540 // Return codes for the traversal functions. This is not an enum
2541 // because we want to be able to declare traversal functions in other
2542 // header files without including this one.
2544 // Continue traversal as usual.
2545 const int TRAVERSE_CONTINUE = -1;
2547 // Exit traversal.
2548 const int TRAVERSE_EXIT = 0;
2550 // Continue traversal, but skip components of the current object.
2551 // E.g., if this is returned by Traverse::statement, we do not
2552 // traverse the expressions in the statement even if
2553 // traverse_expressions is set in the traverse_mask.
2554 const int TRAVERSE_SKIP_COMPONENTS = 1;
2556 // This class is used when traversing the parse tree. The caller uses
2557 // a subclass which overrides functions as desired.
2559 class Traverse
2561 public:
2562 // These bitmasks say what to traverse.
2563 static const unsigned int traverse_variables = 0x1;
2564 static const unsigned int traverse_constants = 0x2;
2565 static const unsigned int traverse_functions = 0x4;
2566 static const unsigned int traverse_blocks = 0x8;
2567 static const unsigned int traverse_statements = 0x10;
2568 static const unsigned int traverse_expressions = 0x20;
2569 static const unsigned int traverse_types = 0x40;
2571 Traverse(unsigned int traverse_mask)
2572 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2575 virtual ~Traverse();
2577 // The bitmask of what to traverse.
2578 unsigned int
2579 traverse_mask() const
2580 { return this->traverse_mask_; }
2582 // Record that we are going to traverse a type. This returns true
2583 // if the type has already been seen in this traversal. This is
2584 // required because types, unlike expressions, can form a circular
2585 // graph.
2586 bool
2587 remember_type(const Type*);
2589 // Record that we are going to see an expression. This returns true
2590 // if the expression has already been seen in this traversal. This
2591 // is only needed for cases where multiple expressions can point to
2592 // a single one.
2593 bool
2594 remember_expression(const Expression*);
2596 // These functions return one of the TRAVERSE codes defined above.
2598 // If traverse_variables is set in the mask, this is called for
2599 // every variable in the tree.
2600 virtual int
2601 variable(Named_object*);
2603 // If traverse_constants is set in the mask, this is called for
2604 // every named constant in the tree. The bool parameter is true for
2605 // a global constant.
2606 virtual int
2607 constant(Named_object*, bool);
2609 // If traverse_functions is set in the mask, this is called for
2610 // every function in the tree.
2611 virtual int
2612 function(Named_object*);
2614 // If traverse_blocks is set in the mask, this is called for every
2615 // block in the tree.
2616 virtual int
2617 block(Block*);
2619 // If traverse_statements is set in the mask, this is called for
2620 // every statement in the tree.
2621 virtual int
2622 statement(Block*, size_t* index, Statement*);
2624 // If traverse_expressions is set in the mask, this is called for
2625 // every expression in the tree.
2626 virtual int
2627 expression(Expression**);
2629 // If traverse_types is set in the mask, this is called for every
2630 // type in the tree.
2631 virtual int
2632 type(Type*);
2634 private:
2635 // A hash table for types we have seen during this traversal. Note
2636 // that this uses the default hash functions for pointers rather
2637 // than Type_hash_identical and Type_identical. This is because for
2638 // traversal we care about seeing a specific type structure. If
2639 // there are two separate instances of identical types, we want to
2640 // traverse both.
2641 typedef Unordered_set(const Type*) Types_seen;
2643 typedef Unordered_set(const Expression*) Expressions_seen;
2645 // Bitmask of what sort of objects to traverse.
2646 unsigned int traverse_mask_;
2647 // Types which have been seen in this traversal.
2648 Types_seen* types_seen_;
2649 // Expressions which have been seen in this traversal.
2650 Expressions_seen* expressions_seen_;
2653 // A class which makes it easier to insert new statements before the
2654 // current statement during a traversal.
2656 class Statement_inserter
2658 public:
2659 // Empty constructor.
2660 Statement_inserter()
2661 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2664 // Constructor for a statement in a block.
2665 Statement_inserter(Block* block, size_t *pindex)
2666 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2669 // Constructor for a global variable.
2670 Statement_inserter(Gogo* gogo, Variable* var)
2671 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2672 { go_assert(var->is_global()); }
2674 // We use the default copy constructor and assignment operator.
2676 // Insert S before the statement we are traversing, or before the
2677 // initialization expression of a global variable.
2678 void
2679 insert(Statement* s);
2681 private:
2682 // The block that the statement is in.
2683 Block* block_;
2684 // The index of the statement that we are traversing.
2685 size_t* pindex_;
2686 // The IR, needed when looking at an initializer expression for a
2687 // global variable.
2688 Gogo* gogo_;
2689 // The global variable, when looking at an initializer expression.
2690 Variable* var_;
2693 // When translating the gogo IR into the backend data structure, this
2694 // is the context we pass down the blocks and statements.
2696 class Translate_context
2698 public:
2699 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2700 Bblock* bblock)
2701 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2702 block_(block), bblock_(bblock), is_const_(false)
2705 // Accessors.
2707 Gogo*
2708 gogo()
2709 { return this->gogo_; }
2711 Backend*
2712 backend()
2713 { return this->backend_; }
2715 Named_object*
2716 function()
2717 { return this->function_; }
2719 Block*
2720 block()
2721 { return this->block_; }
2723 Bblock*
2724 bblock()
2725 { return this->bblock_; }
2727 bool
2728 is_const()
2729 { return this->is_const_; }
2731 // Make a constant context.
2732 void
2733 set_is_const()
2734 { this->is_const_ = true; }
2736 private:
2737 // The IR for the entire compilation unit.
2738 Gogo* gogo_;
2739 // The generator for the backend data structures.
2740 Backend* backend_;
2741 // The function we are currently translating. NULL if not in a
2742 // function, e.g., the initializer of a global variable.
2743 Named_object* function_;
2744 // The block we are currently translating. NULL if not in a
2745 // function.
2746 Block *block_;
2747 // The backend representation of the current block. NULL if block_
2748 // is NULL.
2749 Bblock* bblock_;
2750 // Whether this is being evaluated in a constant context. This is
2751 // used for type descriptor initializers.
2752 bool is_const_;
2755 // Runtime error codes. These must match the values in
2756 // libgo/runtime/go-runtime-error.c.
2758 // Slice index out of bounds: negative or larger than the length of
2759 // the slice.
2760 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2762 // Array index out of bounds.
2763 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2765 // String index out of bounds.
2766 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2768 // Slice slice out of bounds: negative or larger than the length of
2769 // the slice or high bound less than low bound.
2770 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2772 // Array slice out of bounds.
2773 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2775 // String slice out of bounds.
2776 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2778 // Dereference of nil pointer. This is used when there is a
2779 // dereference of a pointer to a very large struct or array, to ensure
2780 // that a gigantic array is not used a proxy to access random memory
2781 // locations.
2782 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2784 // Slice length or capacity out of bounds in make: negative or
2785 // overflow or length greater than capacity.
2786 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2788 // Map capacity out of bounds in make: negative or overflow.
2789 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2791 // Channel capacity out of bounds in make: negative or overflow.
2792 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2794 // Division by zero.
2795 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2797 // This is used by some of the langhooks.
2798 extern Gogo* go_get_gogo();
2800 // Whether we have seen any errors. FIXME: Replace with a backend
2801 // interface.
2802 extern bool saw_errors();
2804 #endif // !defined(GO_GOGO_H)