]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/gofrontend/gogo.h
compiler: Give an error if a variable is defined but not used.
[thirdparty/gcc.git] / gcc / go / gofrontend / gogo.h
1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
2
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.
6
7 #ifndef GO_GOGO_H
8 #define GO_GOGO_H
9
10 #include "go-linemap.h"
11
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;
53
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
56
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
59 // function.
60
61 class Import_init
62 {
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 { }
68
69 // The name of the package being imported.
70 const std::string&
71 package_name() const
72 { return this->package_name_; }
73
74 // The name of the package's init function.
75 const std::string&
76 init_name() const
77 { return this->init_name_; }
78
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_; }
84
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_;
92 };
93
94 // For sorting purposes.
95
96 inline bool
97 operator<(const Import_init& i1, const Import_init& i2)
98 {
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();
106 }
107
108 // The holder for the internal representation of the entire
109 // compilation unit.
110
111 class Gogo
112 {
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);
117
118 // Get the backend generator.
119 Backend*
120 backend()
121 { return this->backend_; }
122
123 // Get the Location generator.
124 Linemap*
125 linemap()
126 { return this->linemap_; }
127
128 // Get the package name.
129 const std::string&
130 package_name() const;
131
132 // Set the package name.
133 void
134 set_package_name(const std::string&, Location);
135
136 // Return whether this is the "main" package.
137 bool
138 is_main_package() const;
139
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
145 {
146 return (is_exported
147 ? name
148 : ('.' + this->unique_prefix()
149 + '.' + this->package_name()
150 + '.' + name));
151 }
152
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); }
158
159 // Return whether a possibly packed name is hidden.
160 static bool
161 is_hidden_name(const std::string& name)
162 { return name[0] == '.'; }
163
164 // Return the package prefix of a hidden name.
165 static std::string
166 hidden_name_prefix(const std::string& name)
167 {
168 go_assert(Gogo::is_hidden_name(name));
169 return name.substr(1, name.rfind('.') - 1);
170 }
171
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);
176
177 // Return whether a name is the blank identifier _.
178 static bool
179 is_sink_name(const std::string& name)
180 {
181 return (name[0] == '.'
182 && name[name.length() - 1] == '_'
183 && name[name.length() - 2] == '.');
184 }
185
186 // Return the unique prefix to use for all exported symbols.
187 const std::string&
188 unique_prefix() const;
189
190 // Set the unique prefix.
191 void
192 set_unique_prefix(const std::string&);
193
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.
197 int
198 package_priority() const;
199
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);
206
207 // Whether we are the global binding level.
208 bool
209 in_global_scope() const;
210
211 // Look up a name in the current binding contours.
212 Named_object*
213 lookup(const std::string&, Named_object** pfunction) const;
214
215 // Look up a name in the current block.
216 Named_object*
217 lookup_in_block(const std::string&) const;
218
219 // Look up a name in the global namespace--the universal scope.
220 Named_object*
221 lookup_global(const char*) const;
222
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);
235
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);
242
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);
248
249 // Finish compiling a function.
250 void
251 finish_function(Location);
252
253 // Return the current function.
254 Named_object*
255 current_function() const;
256
257 // Return the current block.
258 Block*
259 current_block();
260
261 // Start a new block. This is not initially associated with a
262 // function.
263 void
264 start_block(Location);
265
266 // Finish the current block and return it.
267 Block*
268 finish_block(Location);
269
270 // Declare an unknown name. This is used while parsing. The name
271 // must be resolved by the end of the parse. Unknown names are
272 // always added at the package level.
273 Named_object*
274 add_unknown_name(const std::string& name, Location);
275
276 // Declare a function.
277 Named_object*
278 declare_function(const std::string&, Function_type*, Location);
279
280 // Declare a function at the package level. This is used for
281 // functions generated for a type.
282 Named_object*
283 declare_package_function(const std::string&, Function_type*, Location);
284
285 // Add a label.
286 Label*
287 add_label_definition(const std::string&, Location);
288
289 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
290 // report errors for a goto from the current location to the label
291 // location.
292 Label*
293 add_label_reference(const std::string&, Location,
294 bool issue_goto_errors);
295
296 // Return a snapshot of the current binding state.
297 Bindings_snapshot*
298 bindings_snapshot(Location);
299
300 // Add a statement to the current block.
301 void
302 add_statement(Statement*);
303
304 // Add a block to the current block.
305 void
306 add_block(Block*, Location);
307
308 // Add a constant.
309 Named_object*
310 add_constant(const Typed_identifier&, Expression*, int iota_value);
311
312 // Add a type.
313 void
314 add_type(const std::string&, Type*, Location);
315
316 // Add a named type. This is used for builtin types, and to add an
317 // imported type to the global scope.
318 void
319 add_named_type(Named_type*);
320
321 // Declare a type.
322 Named_object*
323 declare_type(const std::string&, Location);
324
325 // Declare a type at the package level. This is used when the
326 // parser sees an unknown name where a type name is required.
327 Named_object*
328 declare_package_type(const std::string&, Location);
329
330 // Define a type which was already declared.
331 void
332 define_type(Named_object*, Named_type*);
333
334 // Add a variable.
335 Named_object*
336 add_variable(const std::string&, Variable*);
337
338 // Add a sink--a reference to the blank identifier _.
339 Named_object*
340 add_sink();
341
342 // Add a named object to the current namespace. This is used for
343 // import . "package".
344 void
345 add_named_object(Named_object*);
346
347 // Mark all local variables in current bindings as used. This is
348 // used when there is a parse error to avoid useless errors.
349 void
350 mark_locals_used();
351
352 // Return a name to use for a thunk function. A thunk function is
353 // one we create during the compilation, for a go statement or a
354 // defer statement or a method expression.
355 static std::string
356 thunk_name();
357
358 // Return whether an object is a thunk.
359 static bool
360 is_thunk(const Named_object*);
361
362 // Note that we've seen an interface type. This is used to build
363 // all required interface method tables.
364 void
365 record_interface_type(Interface_type*);
366
367 // Note that we need an initialization function.
368 void
369 set_need_init_fn()
370 { this->need_init_fn_ = true; }
371
372 // Clear out all names in file scope. This is called when we start
373 // parsing a new file.
374 void
375 clear_file_scope();
376
377 // Queue up a type-specific function to be written out. This is
378 // used when a type-specific function is needed when not at the top
379 // level.
380 void
381 queue_specific_type_function(Type* type, Named_type* name,
382 const std::string& hash_name,
383 Function_type* hash_fntype,
384 const std::string& equal_name,
385 Function_type* equal_fntype);
386
387 // Write out queued specific type functions.
388 void
389 write_specific_type_functions();
390
391 // Traverse the tree. See the Traverse class.
392 void
393 traverse(Traverse*);
394
395 // Define the predeclared global names.
396 void
397 define_global_names();
398
399 // Verify and complete all types.
400 void
401 verify_types();
402
403 // Lower the parse tree.
404 void
405 lower_parse_tree();
406
407 // Lower all the statements in a block.
408 void
409 lower_block(Named_object* function, Block*);
410
411 // Lower an expression.
412 void
413 lower_expression(Named_object* function, Statement_inserter*, Expression**);
414
415 // Lower a constant.
416 void
417 lower_constant(Named_object*);
418
419 // Finalize the method lists and build stub methods for named types.
420 void
421 finalize_methods();
422
423 // Work out the types to use for unspecified variables and
424 // constants.
425 void
426 determine_types();
427
428 // Type check the program.
429 void
430 check_types();
431
432 // Check the types in a single block. This is used for complicated
433 // go statements.
434 void
435 check_types_in_block(Block*);
436
437 // Check for return statements.
438 void
439 check_return_statements();
440
441 // Do all exports.
442 void
443 do_exports();
444
445 // Add an import control function for an imported package to the
446 // list.
447 void
448 add_import_init_fn(const std::string& package_name,
449 const std::string& init_name, int prio);
450
451 // Turn short-cut operators (&&, ||) into explicit if statements.
452 void
453 remove_shortcuts();
454
455 // Use temporary variables to force order of evaluation.
456 void
457 order_evaluations();
458
459 // Build thunks for functions which call recover.
460 void
461 build_recover_thunks();
462
463 // Simplify statements which might use thunks: go and defer
464 // statements.
465 void
466 simplify_thunk_statements();
467
468 // Dump AST if -fgo-dump-ast is set
469 void
470 dump_ast(const char* basename);
471
472 // Convert named types to the backend representation.
473 void
474 convert_named_types();
475
476 // Convert named types in a list of bindings.
477 void
478 convert_named_types_in_bindings(Bindings*);
479
480 // True if named types have been converted to the backend
481 // representation.
482 bool
483 named_types_are_converted() const
484 { return this->named_types_are_converted_; }
485
486 // Write out the global values.
487 void
488 write_globals();
489
490 // Create trees for implicit builtin functions.
491 void
492 define_builtin_function_trees();
493
494 // Build a call to a builtin function. PDECL should point to a NULL
495 // initialized static pointer which will hold the fndecl. NAME is
496 // the name of the function. NARGS is the number of arguments.
497 // RETTYPE is the return type. It is followed by NARGS pairs of
498 // type and argument (both trees).
499 static tree
500 call_builtin(tree* pdecl, Location, const char* name, int nargs,
501 tree rettype, ...);
502
503 // Build a call to the runtime error function.
504 static tree
505 runtime_error(int code, Location);
506
507 // Build a builtin struct with a list of fields.
508 static tree
509 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
510 int nfields, ...);
511
512 // Mark a function declaration as a builtin library function.
513 static void
514 mark_fndecl_as_builtin_library(tree fndecl);
515
516 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
517 // the slice. VALUES points to the values. COUNT is the size,
518 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
519 // COUNT.
520 static tree
521 slice_constructor(tree slice_type_tree, tree values, tree count,
522 tree capacity);
523
524 // Build required interface method tables.
525 void
526 build_interface_method_tables();
527
528 // Build an interface method table for a type: a list of function
529 // pointers, one for each interface method. This returns a decl.
530 tree
531 interface_method_table_for_type(const Interface_type*, Named_type*,
532 bool is_pointer);
533
534 // Return a tree which allocate SIZE bytes to hold values of type
535 // TYPE.
536 tree
537 allocate_memory(Type *type, tree size, Location);
538
539 // Return a type to use for pointer to const char.
540 static tree
541 const_char_pointer_type_tree();
542
543 // Build a string constant with the right type.
544 static tree
545 string_constant_tree(const std::string&);
546
547 // Build a Go string constant. This returns a pointer to the
548 // constant.
549 tree
550 go_string_constant_tree(const std::string&);
551
552 // Receive a value from a channel.
553 static tree
554 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
555 Location);
556
557 // Make a trampoline which calls FNADDR passing CLOSURE.
558 tree
559 make_trampoline(tree fnaddr, tree closure, Location);
560
561 private:
562 // During parsing, we keep a stack of functions. Each function on
563 // the stack is one that we are currently parsing. For each
564 // function, we keep track of the current stack of blocks.
565 struct Open_function
566 {
567 // The function.
568 Named_object* function;
569 // The stack of active blocks in the function.
570 std::vector<Block*> blocks;
571 };
572
573 // The stack of functions.
574 typedef std::vector<Open_function> Open_functions;
575
576 // Set up the built-in unsafe package.
577 void
578 import_unsafe(const std::string&, bool is_exported, Location);
579
580 // Add a new imported package.
581 Named_object*
582 add_package(const std::string& real_name, const std::string& alias,
583 const std::string& unique_prefix, Location location);
584
585 // Return the current binding contour.
586 Bindings*
587 current_bindings();
588
589 const Bindings*
590 current_bindings() const;
591
592 // Get the name of the magic initialization function.
593 const std::string&
594 get_init_fn_name();
595
596 // Get the decl for the magic initialization function.
597 tree
598 initialization_function_decl();
599
600 // Write the magic initialization function.
601 void
602 write_initialization_function(tree fndecl, tree init_stmt_list);
603
604 // Initialize imported packages.
605 void
606 init_imports(tree*);
607
608 // Register variables with the garbage collector.
609 void
610 register_gc_vars(const std::vector<Named_object*>&, tree*);
611
612 // Build a pointer to a Go string constant. This returns a pointer
613 // to the pointer.
614 tree
615 ptr_go_string_constant_tree(const std::string&);
616
617 // Return the type of a trampoline.
618 static tree
619 trampoline_type_tree();
620
621 // Type used to map import names to packages.
622 typedef std::map<std::string, Package*> Imports;
623
624 // Type used to map package names to packages.
625 typedef std::map<std::string, Package*> Packages;
626
627 // Type used to map special names in the sys package.
628 typedef std::map<std::string, std::string> Sys_names;
629
630 // Type used to queue writing a type specific function.
631 struct Specific_type_function
632 {
633 Type* type;
634 Named_type* name;
635 std::string hash_name;
636 Function_type* hash_fntype;
637 std::string equal_name;
638 Function_type* equal_fntype;
639
640 Specific_type_function(Type* atype, Named_type* aname,
641 const std::string& ahash_name,
642 Function_type* ahash_fntype,
643 const std::string& aequal_name,
644 Function_type* aequal_fntype)
645 : type(atype), name(aname), hash_name(ahash_name),
646 hash_fntype(ahash_fntype), equal_name(aequal_name),
647 equal_fntype(aequal_fntype)
648 { }
649 };
650
651 // The backend generator.
652 Backend* backend_;
653 // The object used to keep track of file names and line numbers.
654 Linemap* linemap_;
655 // The package we are compiling.
656 Package* package_;
657 // The list of currently open functions during parsing.
658 Open_functions functions_;
659 // The global binding contour. This includes the builtin functions
660 // and the package we are compiling.
661 Bindings* globals_;
662 // Mapping from import file names to packages.
663 Imports imports_;
664 // Whether the magic unsafe package was imported.
665 bool imported_unsafe_;
666 // Mapping from package names we have seen to packages. This does
667 // not include the package we are compiling.
668 Packages packages_;
669 // The functions named "init", if there are any.
670 std::vector<Named_object*> init_functions_;
671 // Whether we need a magic initialization function.
672 bool need_init_fn_;
673 // The name of the magic initialization function.
674 std::string init_fn_name_;
675 // A list of import control variables for packages that we import.
676 std::set<Import_init> imported_init_fns_;
677 // The unique prefix used for all global symbols.
678 std::string unique_prefix_;
679 // Whether an explicit unique prefix was set by -fgo-prefix.
680 bool unique_prefix_specified_;
681 // A list of interface types defined while parsing.
682 std::vector<Interface_type*> interface_types_;
683 // Type specific functions to write out.
684 std::vector<Specific_type_function*> specific_type_functions_;
685 // Whether we are done writing out specific type functions.
686 bool specific_type_functions_are_written_;
687 // Whether named types have been converted.
688 bool named_types_are_converted_;
689 };
690
691 // A block of statements.
692
693 class Block
694 {
695 public:
696 Block(Block* enclosing, Location);
697
698 // Return the enclosing block.
699 const Block*
700 enclosing() const
701 { return this->enclosing_; }
702
703 // Return the bindings of the block.
704 Bindings*
705 bindings()
706 { return this->bindings_; }
707
708 const Bindings*
709 bindings() const
710 { return this->bindings_; }
711
712 // Look at the block's statements.
713 const std::vector<Statement*>*
714 statements() const
715 { return &this->statements_; }
716
717 // Return the start location. This is normally the location of the
718 // left curly brace which starts the block.
719 Location
720 start_location() const
721 { return this->start_location_; }
722
723 // Return the end location. This is normally the location of the
724 // right curly brace which ends the block.
725 Location
726 end_location() const
727 { return this->end_location_; }
728
729 // Add a statement to the block.
730 void
731 add_statement(Statement*);
732
733 // Add a statement to the front of the block.
734 void
735 add_statement_at_front(Statement*);
736
737 // Replace a statement in a block.
738 void
739 replace_statement(size_t index, Statement*);
740
741 // Add a Statement before statement number INDEX.
742 void
743 insert_statement_before(size_t index, Statement*);
744
745 // Add a Statement after statement number INDEX.
746 void
747 insert_statement_after(size_t index, Statement*);
748
749 // Set the end location of the block.
750 void
751 set_end_location(Location location)
752 { this->end_location_ = location; }
753
754 // Traverse the tree.
755 int
756 traverse(Traverse*);
757
758 // Set final types for unspecified variables and constants.
759 void
760 determine_types();
761
762 // Return true if execution of this block may fall through to the
763 // next block.
764 bool
765 may_fall_through() const;
766
767 // Convert the block to the backend representation.
768 Bblock*
769 get_backend(Translate_context*);
770
771 // Iterate over statements.
772
773 typedef std::vector<Statement*>::iterator iterator;
774
775 iterator
776 begin()
777 { return this->statements_.begin(); }
778
779 iterator
780 end()
781 { return this->statements_.end(); }
782
783 private:
784 // Enclosing block.
785 Block* enclosing_;
786 // Statements in the block.
787 std::vector<Statement*> statements_;
788 // Binding contour.
789 Bindings* bindings_;
790 // Location of start of block.
791 Location start_location_;
792 // Location of end of block.
793 Location end_location_;
794 };
795
796 // A function.
797
798 class Function
799 {
800 public:
801 Function(Function_type* type, Function*, Block*, Location);
802
803 // Return the function's type.
804 Function_type*
805 type() const
806 { return this->type_; }
807
808 // Return the enclosing function if there is one.
809 Function*
810 enclosing()
811 { return this->enclosing_; }
812
813 // Set the enclosing function. This is used when building thunks
814 // for functions which call recover.
815 void
816 set_enclosing(Function* enclosing)
817 {
818 go_assert(this->enclosing_ == NULL);
819 this->enclosing_ = enclosing;
820 }
821
822 // The result variables.
823 typedef std::vector<Named_object*> Results;
824
825 // Create the result variables in the outer block.
826 void
827 create_result_variables(Gogo*);
828
829 // Update the named result variables when cloning a function which
830 // calls recover.
831 void
832 update_result_variables();
833
834 // Return the result variables.
835 Results*
836 result_variables()
837 { return this->results_; }
838
839 // Whether the result variables have names.
840 bool
841 results_are_named() const
842 { return this->results_are_named_; }
843
844 // Add a new field to the closure variable.
845 void
846 add_closure_field(Named_object* var, Location loc)
847 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
848
849 // Whether this function needs a closure.
850 bool
851 needs_closure() const
852 { return !this->closure_fields_.empty(); }
853
854 // Return the closure variable, creating it if necessary. This is
855 // passed to the function as a static chain parameter.
856 Named_object*
857 closure_var();
858
859 // Set the closure variable. This is used when building thunks for
860 // functions which call recover.
861 void
862 set_closure_var(Named_object* v)
863 {
864 go_assert(this->closure_var_ == NULL);
865 this->closure_var_ = v;
866 }
867
868 // Return the variable for a reference to field INDEX in the closure
869 // variable.
870 Named_object*
871 enclosing_var(unsigned int index)
872 {
873 go_assert(index < this->closure_fields_.size());
874 return closure_fields_[index].first;
875 }
876
877 // Set the type of the closure variable if there is one.
878 void
879 set_closure_type();
880
881 // Get the block of statements associated with the function.
882 Block*
883 block() const
884 { return this->block_; }
885
886 // Get the location of the start of the function.
887 Location
888 location() const
889 { return this->location_; }
890
891 // Return whether this function is actually a method.
892 bool
893 is_method() const;
894
895 // Add a label definition to the function.
896 Label*
897 add_label_definition(Gogo*, const std::string& label_name, Location);
898
899 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
900 // if we should report errors for a goto from the current location
901 // to the label location.
902 Label*
903 add_label_reference(Gogo*, const std::string& label_name,
904 Location, bool issue_goto_errors);
905
906 // Warn about labels that are defined but not used.
907 void
908 check_labels() const;
909
910 // Whether this function calls the predeclared recover function.
911 bool
912 calls_recover() const
913 { return this->calls_recover_; }
914
915 // Record that this function calls the predeclared recover function.
916 // This is set during the lowering pass.
917 void
918 set_calls_recover()
919 { this->calls_recover_ = true; }
920
921 // Whether this is a recover thunk function.
922 bool
923 is_recover_thunk() const
924 { return this->is_recover_thunk_; }
925
926 // Record that this is a thunk built for a function which calls
927 // recover.
928 void
929 set_is_recover_thunk()
930 { this->is_recover_thunk_ = true; }
931
932 // Whether this function already has a recover thunk.
933 bool
934 has_recover_thunk() const
935 { return this->has_recover_thunk_; }
936
937 // Record that this function already has a recover thunk.
938 void
939 set_has_recover_thunk()
940 { this->has_recover_thunk_ = true; }
941
942 // Swap with another function. Used only for the thunk which calls
943 // recover.
944 void
945 swap_for_recover(Function *);
946
947 // Traverse the tree.
948 int
949 traverse(Traverse*);
950
951 // Determine types in the function.
952 void
953 determine_types();
954
955 // Return the function's decl given an identifier.
956 tree
957 get_or_make_decl(Gogo*, Named_object*, tree id);
958
959 // Return the function's decl after it has been built.
960 tree
961 get_decl() const
962 {
963 go_assert(this->fndecl_ != NULL);
964 return this->fndecl_;
965 }
966
967 // Set the function decl to hold a tree of the function code.
968 void
969 build_tree(Gogo*, Named_object*);
970
971 // Get the value to return when not explicitly specified. May also
972 // add statements to execute first to STMT_LIST.
973 tree
974 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
975
976 // Get a tree for the variable holding the defer stack.
977 Expression*
978 defer_stack(Location);
979
980 // Export the function.
981 void
982 export_func(Export*, const std::string& name) const;
983
984 // Export a function with a type.
985 static void
986 export_func_with_type(Export*, const std::string& name,
987 const Function_type*);
988
989 // Import a function.
990 static void
991 import_func(Import*, std::string* pname, Typed_identifier** receiver,
992 Typed_identifier_list** pparameters,
993 Typed_identifier_list** presults, bool* is_varargs);
994
995 private:
996 // Type for mapping from label names to Label objects.
997 typedef Unordered_map(std::string, Label*) Labels;
998
999 tree
1000 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1001
1002 tree
1003 copy_parm_to_heap(Gogo*, Named_object*, tree);
1004
1005 void
1006 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1007
1008 typedef std::vector<std::pair<Named_object*,
1009 Location> > Closure_fields;
1010
1011 // The function's type.
1012 Function_type* type_;
1013 // The enclosing function. This is NULL when there isn't one, which
1014 // is the normal case.
1015 Function* enclosing_;
1016 // The result variables, if any.
1017 Results* results_;
1018 // If there is a closure, this is the list of variables which appear
1019 // in the closure. This is created by the parser, and then resolved
1020 // to a real type when we lower parse trees.
1021 Closure_fields closure_fields_;
1022 // The closure variable, passed as a parameter using the static
1023 // chain parameter. Normally NULL.
1024 Named_object* closure_var_;
1025 // The outer block of statements in the function.
1026 Block* block_;
1027 // The source location of the start of the function.
1028 Location location_;
1029 // Labels defined or referenced in the function.
1030 Labels labels_;
1031 // The function decl.
1032 tree fndecl_;
1033 // The defer stack variable. A pointer to this variable is used to
1034 // distinguish the defer stack for one function from another. This
1035 // is NULL unless we actually need a defer stack.
1036 Temporary_statement* defer_stack_;
1037 // True if the result variables are named.
1038 bool results_are_named_;
1039 // True if this function calls the predeclared recover function.
1040 bool calls_recover_;
1041 // True if this a thunk built for a function which calls recover.
1042 bool is_recover_thunk_;
1043 // True if this function already has a recover thunk.
1044 bool has_recover_thunk_;
1045 };
1046
1047 // A snapshot of the current binding state.
1048
1049 class Bindings_snapshot
1050 {
1051 public:
1052 Bindings_snapshot(const Block*, Location);
1053
1054 // Report any errors appropriate for a goto from the current binding
1055 // state of B to this one.
1056 void
1057 check_goto_from(const Block* b, Location);
1058
1059 // Report any errors appropriate for a goto from this binding state
1060 // to the current state of B.
1061 void
1062 check_goto_to(const Block* b);
1063
1064 private:
1065 bool
1066 check_goto_block(Location, const Block*, const Block*, size_t*);
1067
1068 void
1069 check_goto_defs(Location, const Block*, size_t, size_t);
1070
1071 // The current block.
1072 const Block* block_;
1073 // The number of names currently defined in each open block.
1074 // Element 0 is this->block_, element 1 is
1075 // this->block_->enclosing(), etc.
1076 std::vector<size_t> counts_;
1077 // The location where this snapshot was taken.
1078 Location location_;
1079 };
1080
1081 // A function declaration.
1082
1083 class Function_declaration
1084 {
1085 public:
1086 Function_declaration(Function_type* fntype, Location location)
1087 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1088 { }
1089
1090 Function_type*
1091 type() const
1092 { return this->fntype_; }
1093
1094 Location
1095 location() const
1096 { return this->location_; }
1097
1098 const std::string&
1099 asm_name() const
1100 { return this->asm_name_; }
1101
1102 // Set the assembler name.
1103 void
1104 set_asm_name(const std::string& asm_name)
1105 { this->asm_name_ = asm_name; }
1106
1107 // Return a decl for the function given an identifier.
1108 tree
1109 get_or_make_decl(Gogo*, Named_object*, tree id);
1110
1111 // Export a function declaration.
1112 void
1113 export_func(Export* exp, const std::string& name) const
1114 { Function::export_func_with_type(exp, name, this->fntype_); }
1115
1116 private:
1117 // The type of the function.
1118 Function_type* fntype_;
1119 // The location of the declaration.
1120 Location location_;
1121 // The assembler name: this is the name to use in references to the
1122 // function. This is normally empty.
1123 std::string asm_name_;
1124 // The function decl if needed.
1125 tree fndecl_;
1126 };
1127
1128 // A variable.
1129
1130 class Variable
1131 {
1132 public:
1133 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1134 bool is_receiver, Location);
1135
1136 // Get the type of the variable.
1137 Type*
1138 type();
1139
1140 Type*
1141 type() const;
1142
1143 // Return whether the type is defined yet.
1144 bool
1145 has_type() const
1146 { return this->type_ != NULL; }
1147
1148 // Get the initial value.
1149 Expression*
1150 init() const
1151 { return this->init_; }
1152
1153 // Return whether there are any preinit statements.
1154 bool
1155 has_pre_init() const
1156 { return this->preinit_ != NULL; }
1157
1158 // Return the preinit statements if any.
1159 Block*
1160 preinit() const
1161 { return this->preinit_; }
1162
1163 // Return whether this is a global variable.
1164 bool
1165 is_global() const
1166 { return this->is_global_; }
1167
1168 // Return whether this is a function parameter.
1169 bool
1170 is_parameter() const
1171 { return this->is_parameter_; }
1172
1173 // Return whether this is the receiver parameter of a method.
1174 bool
1175 is_receiver() const
1176 { return this->is_receiver_; }
1177
1178 // Change this parameter to be a receiver. This is used when
1179 // creating the thunks created for functions which call recover.
1180 void
1181 set_is_receiver()
1182 {
1183 go_assert(this->is_parameter_);
1184 this->is_receiver_ = true;
1185 }
1186
1187 // Change this parameter to not be a receiver. This is used when
1188 // creating the thunks created for functions which call recover.
1189 void
1190 set_is_not_receiver()
1191 {
1192 go_assert(this->is_parameter_);
1193 this->is_receiver_ = false;
1194 }
1195
1196 // Return whether this is the varargs parameter of a function.
1197 bool
1198 is_varargs_parameter() const
1199 { return this->is_varargs_parameter_; }
1200
1201 // Whether this variable's address is taken.
1202 bool
1203 is_address_taken() const
1204 { return this->is_address_taken_; }
1205
1206 // Whether this variable should live in the heap.
1207 bool
1208 is_in_heap() const
1209 { return this->is_address_taken_ && !this->is_global_; }
1210
1211 // Note that something takes the address of this variable.
1212 void
1213 set_address_taken()
1214 { this->is_address_taken_ = true; }
1215
1216 // Return whether the address is taken but does not escape.
1217 bool
1218 is_non_escaping_address_taken() const
1219 { return this->is_non_escaping_address_taken_; }
1220
1221 // Note that something takes the address of this variable such that
1222 // the address does not escape the function.
1223 void
1224 set_non_escaping_address_taken()
1225 { this->is_non_escaping_address_taken_ = true; }
1226
1227 // Get the source location of the variable's declaration.
1228 Location
1229 location() const
1230 { return this->location_; }
1231
1232 // Record that this is the varargs parameter of a function.
1233 void
1234 set_is_varargs_parameter()
1235 {
1236 go_assert(this->is_parameter_);
1237 this->is_varargs_parameter_ = true;
1238 }
1239
1240 // Return whether the variable has been used.
1241 bool
1242 is_used() const
1243 { return this->is_used_; }
1244
1245 // Mark that the variable has been used.
1246 void
1247 set_is_used()
1248 { this->is_used_ = true; }
1249
1250 // Clear the initial value; used for error handling.
1251 void
1252 clear_init()
1253 { this->init_ = NULL; }
1254
1255 // Set the initial value; used for converting shortcuts.
1256 void
1257 set_init(Expression* init)
1258 { this->init_ = init; }
1259
1260 // Get the preinit block, a block of statements to be run before the
1261 // initialization expression.
1262 Block*
1263 preinit_block(Gogo*);
1264
1265 // Add a statement to be run before the initialization expression.
1266 // This is only used for global variables.
1267 void
1268 add_preinit_statement(Gogo*, Statement*);
1269
1270 // Lower the initialization expression after parsing is complete.
1271 void
1272 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1273
1274 // A special case: the init value is used only to determine the
1275 // type. This is used if the variable is defined using := with the
1276 // comma-ok form of a map index or a receive expression. The init
1277 // value is actually the map index expression or receive expression.
1278 // We use this because we may not know the right type at parse time.
1279 void
1280 set_type_from_init_tuple()
1281 { this->type_from_init_tuple_ = true; }
1282
1283 // Another special case: the init value is used only to determine
1284 // the type. This is used if the variable is defined using := with
1285 // a range clause. The init value is the range expression. The
1286 // type of the variable is the index type of the range expression
1287 // (i.e., the first value returned by a range).
1288 void
1289 set_type_from_range_index()
1290 { this->type_from_range_index_ = true; }
1291
1292 // Another special case: like set_type_from_range_index, but the
1293 // type is the value type of the range expression (i.e., the second
1294 // value returned by a range).
1295 void
1296 set_type_from_range_value()
1297 { this->type_from_range_value_ = true; }
1298
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 case in a select statement. The init value is the channel.
1302 // The type of the variable is the channel's element type.
1303 void
1304 set_type_from_chan_element()
1305 { this->type_from_chan_element_ = true; }
1306
1307 // After we lower the select statement, we once again set the type
1308 // from the initialization expression.
1309 void
1310 clear_type_from_chan_element()
1311 {
1312 go_assert(this->type_from_chan_element_);
1313 this->type_from_chan_element_ = false;
1314 }
1315
1316 // Note that this variable was created for a type switch clause.
1317 void
1318 set_is_type_switch_var()
1319 { this->is_type_switch_var_ = true; }
1320
1321 // Traverse the initializer expression.
1322 int
1323 traverse_expression(Traverse*, unsigned int traverse_mask);
1324
1325 // Determine the type of the variable if necessary.
1326 void
1327 determine_type();
1328
1329 // Get the backend representation of the variable.
1330 Bvariable*
1331 get_backend_variable(Gogo*, Named_object*, const Package*,
1332 const std::string&);
1333
1334 // Get the initial value of the variable as a tree. This may only
1335 // be called if has_pre_init() returns false.
1336 tree
1337 get_init_tree(Gogo*, Named_object* function);
1338
1339 // Return a series of statements which sets the value of the
1340 // variable in DECL. This should only be called is has_pre_init()
1341 // returns true. DECL may be NULL for a sink variable.
1342 tree
1343 get_init_block(Gogo*, Named_object* function, tree decl);
1344
1345 // Export the variable.
1346 void
1347 export_var(Export*, const std::string& name) const;
1348
1349 // Import a variable.
1350 static void
1351 import_var(Import*, std::string* pname, Type** ptype);
1352
1353 private:
1354 // The type of a tuple.
1355 Type*
1356 type_from_tuple(Expression*, bool) const;
1357
1358 // The type of a range.
1359 Type*
1360 type_from_range(Expression*, bool, bool) const;
1361
1362 // The element type of a channel.
1363 Type*
1364 type_from_chan_element(Expression*, bool) const;
1365
1366 // The variable's type. This may be NULL if the type is set from
1367 // the expression.
1368 Type* type_;
1369 // The initial value. This may be NULL if the variable should be
1370 // initialized to the default value for the type.
1371 Expression* init_;
1372 // Statements to run before the init statement.
1373 Block* preinit_;
1374 // Location of variable definition.
1375 Location location_;
1376 // Backend representation.
1377 Bvariable* backend_;
1378 // Whether this is a global variable.
1379 bool is_global_ : 1;
1380 // Whether this is a function parameter.
1381 bool is_parameter_ : 1;
1382 // Whether this is the receiver parameter of a method.
1383 bool is_receiver_ : 1;
1384 // Whether this is the varargs parameter of a function.
1385 bool is_varargs_parameter_ : 1;
1386 // Whether this variable is ever referenced.
1387 bool is_used_ : 1;
1388 // Whether something takes the address of this variable. For a
1389 // local variable this implies that the variable has to be on the
1390 // heap.
1391 bool is_address_taken_ : 1;
1392 // Whether something takes the address of this variable such that
1393 // the address does not escape the function.
1394 bool is_non_escaping_address_taken_ : 1;
1395 // True if we have seen this variable in a traversal.
1396 bool seen_ : 1;
1397 // True if we have lowered the initialization expression.
1398 bool init_is_lowered_ : 1;
1399 // True if init is a tuple used to set the type.
1400 bool type_from_init_tuple_ : 1;
1401 // True if init is a range clause and the type is the index type.
1402 bool type_from_range_index_ : 1;
1403 // True if init is a range clause and the type is the value type.
1404 bool type_from_range_value_ : 1;
1405 // True if init is a channel and the type is the channel's element type.
1406 bool type_from_chan_element_ : 1;
1407 // True if this is a variable created for a type switch case.
1408 bool is_type_switch_var_ : 1;
1409 // True if we have determined types.
1410 bool determined_type_ : 1;
1411 };
1412
1413 // A variable which is really the name for a function return value, or
1414 // part of one.
1415
1416 class Result_variable
1417 {
1418 public:
1419 Result_variable(Type* type, Function* function, int index,
1420 Location location)
1421 : type_(type), function_(function), index_(index), location_(location),
1422 backend_(NULL), is_address_taken_(false),
1423 is_non_escaping_address_taken_(false)
1424 { }
1425
1426 // Get the type of the result variable.
1427 Type*
1428 type() const
1429 { return this->type_; }
1430
1431 // Get the function that this is associated with.
1432 Function*
1433 function() const
1434 { return this->function_; }
1435
1436 // Index in the list of function results.
1437 int
1438 index() const
1439 { return this->index_; }
1440
1441 // The location of the variable definition.
1442 Location
1443 location() const
1444 { return this->location_; }
1445
1446 // Whether this variable's address is taken.
1447 bool
1448 is_address_taken() const
1449 { return this->is_address_taken_; }
1450
1451 // Note that something takes the address of this variable.
1452 void
1453 set_address_taken()
1454 { this->is_address_taken_ = true; }
1455
1456 // Return whether the address is taken but does not escape.
1457 bool
1458 is_non_escaping_address_taken() const
1459 { return this->is_non_escaping_address_taken_; }
1460
1461 // Note that something takes the address of this variable such that
1462 // the address does not escape the function.
1463 void
1464 set_non_escaping_address_taken()
1465 { this->is_non_escaping_address_taken_ = true; }
1466
1467 // Whether this variable should live in the heap.
1468 bool
1469 is_in_heap() const
1470 { return this->is_address_taken_; }
1471
1472 // Set the function. This is used when cloning functions which call
1473 // recover.
1474 void
1475 set_function(Function* function)
1476 { this->function_ = function; }
1477
1478 // Get the backend representation of the variable.
1479 Bvariable*
1480 get_backend_variable(Gogo*, Named_object*, const std::string&);
1481
1482 private:
1483 // Type of result variable.
1484 Type* type_;
1485 // Function with which this is associated.
1486 Function* function_;
1487 // Index in list of results.
1488 int index_;
1489 // Where the result variable is defined.
1490 Location location_;
1491 // Backend representation.
1492 Bvariable* backend_;
1493 // Whether something takes the address of this variable.
1494 bool is_address_taken_;
1495 // Whether something takes the address of this variable such that
1496 // the address does not escape the function.
1497 bool is_non_escaping_address_taken_;
1498 };
1499
1500 // The value we keep for a named constant. This lets us hold a type
1501 // and an expression.
1502
1503 class Named_constant
1504 {
1505 public:
1506 Named_constant(Type* type, Expression* expr, int iota_value,
1507 Location location)
1508 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1509 lowering_(false)
1510 { }
1511
1512 Type*
1513 type() const
1514 { return this->type_; }
1515
1516 Expression*
1517 expr() const
1518 { return this->expr_; }
1519
1520 int
1521 iota_value() const
1522 { return this->iota_value_; }
1523
1524 Location
1525 location() const
1526 { return this->location_; }
1527
1528 // Whether we are lowering.
1529 bool
1530 lowering() const
1531 { return this->lowering_; }
1532
1533 // Set that we are lowering.
1534 void
1535 set_lowering()
1536 { this->lowering_ = true; }
1537
1538 // We are no longer lowering.
1539 void
1540 clear_lowering()
1541 { this->lowering_ = false; }
1542
1543 // Traverse the expression.
1544 int
1545 traverse_expression(Traverse*);
1546
1547 // Determine the type of the constant if necessary.
1548 void
1549 determine_type();
1550
1551 // Indicate that we found and reported an error for this constant.
1552 void
1553 set_error();
1554
1555 // Export the constant.
1556 void
1557 export_const(Export*, const std::string& name) const;
1558
1559 // Import a constant.
1560 static void
1561 import_const(Import*, std::string*, Type**, Expression**);
1562
1563 private:
1564 // The type of the constant.
1565 Type* type_;
1566 // The expression for the constant.
1567 Expression* expr_;
1568 // If the predeclared constant iota is used in EXPR_, this is the
1569 // value it will have. We do this because at parse time we don't
1570 // know whether the name "iota" will refer to the predeclared
1571 // constant or to something else. We put in the right value in when
1572 // we lower.
1573 int iota_value_;
1574 // The location of the definition.
1575 Location location_;
1576 // Whether we are currently lowering this constant.
1577 bool lowering_;
1578 };
1579
1580 // A type declaration.
1581
1582 class Type_declaration
1583 {
1584 public:
1585 Type_declaration(Location location)
1586 : location_(location), in_function_(NULL), methods_(),
1587 issued_warning_(false)
1588 { }
1589
1590 // Return the location.
1591 Location
1592 location() const
1593 { return this->location_; }
1594
1595 // Return the function in which this type is declared. This will
1596 // return NULL for a type declared in global scope.
1597 Named_object*
1598 in_function()
1599 { return this->in_function_; }
1600
1601 // Set the function in which this type is declared.
1602 void
1603 set_in_function(Named_object* f)
1604 { this->in_function_ = f; }
1605
1606 // Add a method to this type. This is used when methods are defined
1607 // before the type.
1608 Named_object*
1609 add_method(const std::string& name, Function* function);
1610
1611 // Add a method declaration to this type.
1612 Named_object*
1613 add_method_declaration(const std::string& name, Function_type* type,
1614 Location location);
1615
1616 // Return whether any methods were defined.
1617 bool
1618 has_methods() const;
1619
1620 // Define methods when the real type is known.
1621 void
1622 define_methods(Named_type*);
1623
1624 // This is called if we are trying to use this type. It returns
1625 // true if we should issue a warning.
1626 bool
1627 using_type();
1628
1629 private:
1630 typedef std::vector<Named_object*> Methods;
1631
1632 // The location of the type declaration.
1633 Location location_;
1634 // If this type is declared in a function, a pointer back to the
1635 // function in which it is defined.
1636 Named_object* in_function_;
1637 // Methods defined before the type is defined.
1638 Methods methods_;
1639 // True if we have issued a warning about a use of this type
1640 // declaration when it is undefined.
1641 bool issued_warning_;
1642 };
1643
1644 // An unknown object. These are created by the parser for forward
1645 // references to names which have not been seen before. In a correct
1646 // program, these will always point to a real definition by the end of
1647 // the parse. Because they point to another Named_object, these may
1648 // only be referenced by Unknown_expression objects.
1649
1650 class Unknown_name
1651 {
1652 public:
1653 Unknown_name(Location location)
1654 : location_(location), real_named_object_(NULL)
1655 { }
1656
1657 // Return the location where this name was first seen.
1658 Location
1659 location() const
1660 { return this->location_; }
1661
1662 // Return the real named object that this points to, or NULL if it
1663 // was never resolved.
1664 Named_object*
1665 real_named_object() const
1666 { return this->real_named_object_; }
1667
1668 // Set the real named object that this points to.
1669 void
1670 set_real_named_object(Named_object* no);
1671
1672 private:
1673 // The location where this name was first seen.
1674 Location location_;
1675 // The real named object when it is known.
1676 Named_object*
1677 real_named_object_;
1678 };
1679
1680 // A named object named. This is the result of a declaration. We
1681 // don't use a superclass because they all have to be handled
1682 // differently.
1683
1684 class Named_object
1685 {
1686 public:
1687 enum Classification
1688 {
1689 // An uninitialized Named_object. We should never see this.
1690 NAMED_OBJECT_UNINITIALIZED,
1691 // An unknown name. This is used for forward references. In a
1692 // correct program, these will all be resolved by the end of the
1693 // parse.
1694 NAMED_OBJECT_UNKNOWN,
1695 // A const.
1696 NAMED_OBJECT_CONST,
1697 // A type.
1698 NAMED_OBJECT_TYPE,
1699 // A forward type declaration.
1700 NAMED_OBJECT_TYPE_DECLARATION,
1701 // A var.
1702 NAMED_OBJECT_VAR,
1703 // A result variable in a function.
1704 NAMED_OBJECT_RESULT_VAR,
1705 // The blank identifier--the special variable named _.
1706 NAMED_OBJECT_SINK,
1707 // A func.
1708 NAMED_OBJECT_FUNC,
1709 // A forward func declaration.
1710 NAMED_OBJECT_FUNC_DECLARATION,
1711 // A package.
1712 NAMED_OBJECT_PACKAGE
1713 };
1714
1715 // Return the classification.
1716 Classification
1717 classification() const
1718 { return this->classification_; }
1719
1720 // Classifiers.
1721
1722 bool
1723 is_unknown() const
1724 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1725
1726 bool
1727 is_const() const
1728 { return this->classification_ == NAMED_OBJECT_CONST; }
1729
1730 bool
1731 is_type() const
1732 { return this->classification_ == NAMED_OBJECT_TYPE; }
1733
1734 bool
1735 is_type_declaration() const
1736 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1737
1738 bool
1739 is_variable() const
1740 { return this->classification_ == NAMED_OBJECT_VAR; }
1741
1742 bool
1743 is_result_variable() const
1744 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1745
1746 bool
1747 is_sink() const
1748 { return this->classification_ == NAMED_OBJECT_SINK; }
1749
1750 bool
1751 is_function() const
1752 { return this->classification_ == NAMED_OBJECT_FUNC; }
1753
1754 bool
1755 is_function_declaration() const
1756 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1757
1758 bool
1759 is_package() const
1760 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1761
1762 // Creators.
1763
1764 static Named_object*
1765 make_unknown_name(const std::string& name, Location);
1766
1767 static Named_object*
1768 make_constant(const Typed_identifier&, const Package*, Expression*,
1769 int iota_value);
1770
1771 static Named_object*
1772 make_type(const std::string&, const Package*, Type*, Location);
1773
1774 static Named_object*
1775 make_type_declaration(const std::string&, const Package*, Location);
1776
1777 static Named_object*
1778 make_variable(const std::string&, const Package*, Variable*);
1779
1780 static Named_object*
1781 make_result_variable(const std::string&, Result_variable*);
1782
1783 static Named_object*
1784 make_sink();
1785
1786 static Named_object*
1787 make_function(const std::string&, const Package*, Function*);
1788
1789 static Named_object*
1790 make_function_declaration(const std::string&, const Package*, Function_type*,
1791 Location);
1792
1793 static Named_object*
1794 make_package(const std::string& alias, Package* package);
1795
1796 // Getters.
1797
1798 Unknown_name*
1799 unknown_value()
1800 {
1801 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1802 return this->u_.unknown_value;
1803 }
1804
1805 const Unknown_name*
1806 unknown_value() const
1807 {
1808 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1809 return this->u_.unknown_value;
1810 }
1811
1812 Named_constant*
1813 const_value()
1814 {
1815 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1816 return this->u_.const_value;
1817 }
1818
1819 const Named_constant*
1820 const_value() const
1821 {
1822 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1823 return this->u_.const_value;
1824 }
1825
1826 Named_type*
1827 type_value()
1828 {
1829 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1830 return this->u_.type_value;
1831 }
1832
1833 const Named_type*
1834 type_value() const
1835 {
1836 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1837 return this->u_.type_value;
1838 }
1839
1840 Type_declaration*
1841 type_declaration_value()
1842 {
1843 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1844 return this->u_.type_declaration;
1845 }
1846
1847 const Type_declaration*
1848 type_declaration_value() const
1849 {
1850 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1851 return this->u_.type_declaration;
1852 }
1853
1854 Variable*
1855 var_value()
1856 {
1857 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1858 return this->u_.var_value;
1859 }
1860
1861 const Variable*
1862 var_value() const
1863 {
1864 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1865 return this->u_.var_value;
1866 }
1867
1868 Result_variable*
1869 result_var_value()
1870 {
1871 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1872 return this->u_.result_var_value;
1873 }
1874
1875 const Result_variable*
1876 result_var_value() const
1877 {
1878 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1879 return this->u_.result_var_value;
1880 }
1881
1882 Function*
1883 func_value()
1884 {
1885 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1886 return this->u_.func_value;
1887 }
1888
1889 const Function*
1890 func_value() const
1891 {
1892 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1893 return this->u_.func_value;
1894 }
1895
1896 Function_declaration*
1897 func_declaration_value()
1898 {
1899 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1900 return this->u_.func_declaration_value;
1901 }
1902
1903 const Function_declaration*
1904 func_declaration_value() const
1905 {
1906 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1907 return this->u_.func_declaration_value;
1908 }
1909
1910 Package*
1911 package_value()
1912 {
1913 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1914 return this->u_.package_value;
1915 }
1916
1917 const Package*
1918 package_value() const
1919 {
1920 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1921 return this->u_.package_value;
1922 }
1923
1924 const std::string&
1925 name() const
1926 { return this->name_; }
1927
1928 // Return the name to use in an error message. The difference is
1929 // that if this Named_object is defined in a different package, this
1930 // will return PACKAGE.NAME.
1931 std::string
1932 message_name() const;
1933
1934 const Package*
1935 package() const
1936 { return this->package_; }
1937
1938 // Resolve an unknown value if possible. This returns the same
1939 // Named_object or a new one.
1940 Named_object*
1941 resolve()
1942 {
1943 Named_object* ret = this;
1944 if (this->is_unknown())
1945 {
1946 Named_object* r = this->unknown_value()->real_named_object();
1947 if (r != NULL)
1948 ret = r;
1949 }
1950 return ret;
1951 }
1952
1953 const Named_object*
1954 resolve() const
1955 {
1956 const Named_object* ret = this;
1957 if (this->is_unknown())
1958 {
1959 const Named_object* r = this->unknown_value()->real_named_object();
1960 if (r != NULL)
1961 ret = r;
1962 }
1963 return ret;
1964 }
1965
1966 // The location where this object was defined or referenced.
1967 Location
1968 location() const;
1969
1970 // Convert a variable to the backend representation.
1971 Bvariable*
1972 get_backend_variable(Gogo*, Named_object* function);
1973
1974 // Return a tree for the external identifier for this object.
1975 tree
1976 get_id(Gogo*);
1977
1978 // Return a tree representing this object.
1979 tree
1980 get_tree(Gogo*, Named_object* function);
1981
1982 // Define a type declaration.
1983 void
1984 set_type_value(Named_type*);
1985
1986 // Define a function declaration.
1987 void
1988 set_function_value(Function*);
1989
1990 // Declare an unknown name as a type declaration.
1991 void
1992 declare_as_type();
1993
1994 // Export this object.
1995 void
1996 export_named_object(Export*) const;
1997
1998 private:
1999 Named_object(const std::string&, const Package*, Classification);
2000
2001 // The name of the object.
2002 std::string name_;
2003 // The package that this object is in. This is NULL if it is in the
2004 // file we are compiling.
2005 const Package* package_;
2006 // The type of object this is.
2007 Classification classification_;
2008 // The real data.
2009 union
2010 {
2011 Unknown_name* unknown_value;
2012 Named_constant* const_value;
2013 Named_type* type_value;
2014 Type_declaration* type_declaration;
2015 Variable* var_value;
2016 Result_variable* result_var_value;
2017 Function* func_value;
2018 Function_declaration* func_declaration_value;
2019 Package* package_value;
2020 } u_;
2021 // The DECL tree for this object if we have already converted it.
2022 tree tree_;
2023 };
2024
2025 // A binding contour. This binds names to objects.
2026
2027 class Bindings
2028 {
2029 public:
2030 // Type for mapping from names to objects.
2031 typedef Unordered_map(std::string, Named_object*) Contour;
2032
2033 Bindings(Bindings* enclosing);
2034
2035 // Add an unknown name.
2036 Named_object*
2037 add_unknown_name(const std::string& name, Location location)
2038 {
2039 return this->add_named_object(Named_object::make_unknown_name(name,
2040 location));
2041 }
2042
2043 // Add a constant.
2044 Named_object*
2045 add_constant(const Typed_identifier& tid, const Package* package,
2046 Expression* expr, int iota_value)
2047 {
2048 return this->add_named_object(Named_object::make_constant(tid, package,
2049 expr,
2050 iota_value));
2051 }
2052
2053 // Add a type.
2054 Named_object*
2055 add_type(const std::string& name, const Package* package, Type* type,
2056 Location location)
2057 {
2058 return this->add_named_object(Named_object::make_type(name, package, type,
2059 location));
2060 }
2061
2062 // Add a named type. This is used for builtin types, and to add an
2063 // imported type to the global scope.
2064 Named_object*
2065 add_named_type(Named_type* named_type);
2066
2067 // Add a type declaration.
2068 Named_object*
2069 add_type_declaration(const std::string& name, const Package* package,
2070 Location location)
2071 {
2072 Named_object* no = Named_object::make_type_declaration(name, package,
2073 location);
2074 return this->add_named_object(no);
2075 }
2076
2077 // Add a variable.
2078 Named_object*
2079 add_variable(const std::string& name, const Package* package,
2080 Variable* variable)
2081 {
2082 return this->add_named_object(Named_object::make_variable(name, package,
2083 variable));
2084 }
2085
2086 // Add a result variable.
2087 Named_object*
2088 add_result_variable(const std::string& name, Result_variable* result)
2089 {
2090 return this->add_named_object(Named_object::make_result_variable(name,
2091 result));
2092 }
2093
2094 // Add a function.
2095 Named_object*
2096 add_function(const std::string& name, const Package*, Function* function);
2097
2098 // Add a function declaration.
2099 Named_object*
2100 add_function_declaration(const std::string& name, const Package* package,
2101 Function_type* type, Location location);
2102
2103 // Add a package. The location is the location of the import
2104 // statement.
2105 Named_object*
2106 add_package(const std::string& alias, Package* package)
2107 {
2108 Named_object* no = Named_object::make_package(alias, package);
2109 return this->add_named_object(no);
2110 }
2111
2112 // Define a type which was already declared.
2113 void
2114 define_type(Named_object*, Named_type*);
2115
2116 // Add a method to the list of objects. This is not added to the
2117 // lookup table.
2118 void
2119 add_method(Named_object*);
2120
2121 // Add a named object to this binding.
2122 Named_object*
2123 add_named_object(Named_object* no)
2124 { return this->add_named_object_to_contour(&this->bindings_, no); }
2125
2126 // Clear all names in file scope from the bindings.
2127 void
2128 clear_file_scope();
2129
2130 // Look up a name in this binding contour and in any enclosing
2131 // binding contours. This returns NULL if the name is not found.
2132 Named_object*
2133 lookup(const std::string&) const;
2134
2135 // Look up a name in this binding contour without looking in any
2136 // enclosing binding contours. Returns NULL if the name is not found.
2137 Named_object*
2138 lookup_local(const std::string&) const;
2139
2140 // Remove a name.
2141 void
2142 remove_binding(Named_object*);
2143
2144 // Mark all variables as used. This is used for some types of parse
2145 // error.
2146 void
2147 mark_locals_used();
2148
2149 // Traverse the tree. See the Traverse class.
2150 int
2151 traverse(Traverse*, bool is_global);
2152
2153 // Iterate over definitions. This does not include things which
2154 // were only declared.
2155
2156 typedef std::vector<Named_object*>::const_iterator
2157 const_definitions_iterator;
2158
2159 const_definitions_iterator
2160 begin_definitions() const
2161 { return this->named_objects_.begin(); }
2162
2163 const_definitions_iterator
2164 end_definitions() const
2165 { return this->named_objects_.end(); }
2166
2167 // Return the number of definitions.
2168 size_t
2169 size_definitions() const
2170 { return this->named_objects_.size(); }
2171
2172 // Return whether there are no definitions.
2173 bool
2174 empty_definitions() const
2175 { return this->named_objects_.empty(); }
2176
2177 // Iterate over declarations. This is everything that has been
2178 // declared, which includes everything which has been defined.
2179
2180 typedef Contour::const_iterator const_declarations_iterator;
2181
2182 const_declarations_iterator
2183 begin_declarations() const
2184 { return this->bindings_.begin(); }
2185
2186 const_declarations_iterator
2187 end_declarations() const
2188 { return this->bindings_.end(); }
2189
2190 // Return the number of declarations.
2191 size_t
2192 size_declarations() const
2193 { return this->bindings_.size(); }
2194
2195 // Return whether there are no declarations.
2196 bool
2197 empty_declarations() const
2198 { return this->bindings_.empty(); }
2199
2200 // Return the first declaration.
2201 Named_object*
2202 first_declaration()
2203 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2204
2205 private:
2206 Named_object*
2207 add_named_object_to_contour(Contour*, Named_object*);
2208
2209 Named_object*
2210 new_definition(Named_object*, Named_object*);
2211
2212 // Enclosing bindings.
2213 Bindings* enclosing_;
2214 // The list of objects.
2215 std::vector<Named_object*> named_objects_;
2216 // The mapping from names to objects.
2217 Contour bindings_;
2218 };
2219
2220 // A label.
2221
2222 class Label
2223 {
2224 public:
2225 Label(const std::string& name)
2226 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2227 refs_(), is_used_(false), blabel_(NULL)
2228 { }
2229
2230 // Return the label's name.
2231 const std::string&
2232 name() const
2233 { return this->name_; }
2234
2235 // Return whether the label has been defined.
2236 bool
2237 is_defined() const
2238 { return !Linemap::is_unknown_location(this->location_); }
2239
2240 // Return whether the label has been used.
2241 bool
2242 is_used() const
2243 { return this->is_used_; }
2244
2245 // Record that the label is used.
2246 void
2247 set_is_used()
2248 { this->is_used_ = true; }
2249
2250 // Return the location of the definition.
2251 Location
2252 location() const
2253 { return this->location_; }
2254
2255 // Return the bindings snapshot.
2256 Bindings_snapshot*
2257 snapshot() const
2258 { return this->snapshot_; }
2259
2260 // Add a snapshot of a goto which refers to this label.
2261 void
2262 add_snapshot_ref(Bindings_snapshot* snapshot)
2263 {
2264 go_assert(Linemap::is_unknown_location(this->location_));
2265 this->refs_.push_back(snapshot);
2266 }
2267
2268 // Return the list of snapshots of goto statements which refer to
2269 // this label.
2270 const std::vector<Bindings_snapshot*>&
2271 refs() const
2272 { return this->refs_; }
2273
2274 // Clear the references.
2275 void
2276 clear_refs();
2277
2278 // Define the label at LOCATION with the given bindings snapshot.
2279 void
2280 define(Location location, Bindings_snapshot* snapshot)
2281 {
2282 go_assert(Linemap::is_unknown_location(this->location_)
2283 && this->snapshot_ == NULL);
2284 this->location_ = location;
2285 this->snapshot_ = snapshot;
2286 }
2287
2288 // Return the backend representation for this label.
2289 Blabel*
2290 get_backend_label(Translate_context*);
2291
2292 // Return an expression for the address of this label. This is used
2293 // to get the return address of a deferred function to see whether
2294 // the function may call recover.
2295 Bexpression*
2296 get_addr(Translate_context*, Location location);
2297
2298 private:
2299 // The name of the label.
2300 std::string name_;
2301 // The location of the definition. This is 0 if the label has not
2302 // yet been defined.
2303 Location location_;
2304 // A snapshot of the set of bindings defined at this label, used to
2305 // issue errors about invalid goto statements.
2306 Bindings_snapshot* snapshot_;
2307 // A list of snapshots of goto statements which refer to this label.
2308 std::vector<Bindings_snapshot*> refs_;
2309 // Whether the label has been used.
2310 bool is_used_;
2311 // The backend representation.
2312 Blabel* blabel_;
2313 };
2314
2315 // An unnamed label. These are used when lowering loops.
2316
2317 class Unnamed_label
2318 {
2319 public:
2320 Unnamed_label(Location location)
2321 : location_(location), blabel_(NULL)
2322 { }
2323
2324 // Get the location where the label is defined.
2325 Location
2326 location() const
2327 { return this->location_; }
2328
2329 // Set the location where the label is defined.
2330 void
2331 set_location(Location location)
2332 { this->location_ = location; }
2333
2334 // Return a statement which defines this label.
2335 Bstatement*
2336 get_definition(Translate_context*);
2337
2338 // Return a goto to this label from LOCATION.
2339 Bstatement*
2340 get_goto(Translate_context*, Location location);
2341
2342 private:
2343 // Return the backend representation.
2344 Blabel*
2345 get_blabel(Translate_context*);
2346
2347 // The location where the label is defined.
2348 Location location_;
2349 // The backend representation of this label.
2350 Blabel* blabel_;
2351 };
2352
2353 // An imported package.
2354
2355 class Package
2356 {
2357 public:
2358 Package(const std::string& name, const std::string& unique_prefix,
2359 Location location);
2360
2361 // The real name of this package. This may be different from the
2362 // name in the associated Named_object if the import statement used
2363 // an alias.
2364 const std::string&
2365 name() const
2366 { return this->name_; }
2367
2368 // Return the location of the import statement.
2369 Location
2370 location() const
2371 { return this->location_; }
2372
2373 // Get the unique prefix used for all symbols exported from this
2374 // package.
2375 const std::string&
2376 unique_prefix() const
2377 {
2378 go_assert(!this->unique_prefix_.empty());
2379 return this->unique_prefix_;
2380 }
2381
2382 // The priority of this package. The init function of packages with
2383 // lower priority must be run before the init function of packages
2384 // with higher priority.
2385 int
2386 priority() const
2387 { return this->priority_; }
2388
2389 // Set the priority.
2390 void
2391 set_priority(int priority);
2392
2393 // Return the bindings.
2394 Bindings*
2395 bindings()
2396 { return this->bindings_; }
2397
2398 // Whether some symbol from the package was used.
2399 bool
2400 used() const
2401 { return this->used_; }
2402
2403 // Note that some symbol from this package was used.
2404 void
2405 set_used() const
2406 { this->used_ = true; }
2407
2408 // Clear the used field for the next file.
2409 void
2410 clear_used()
2411 { this->used_ = false; }
2412
2413 // Whether this package was imported in the current file.
2414 bool
2415 is_imported() const
2416 { return this->is_imported_; }
2417
2418 // Note that this package was imported in the current file.
2419 void
2420 set_is_imported()
2421 { this->is_imported_ = true; }
2422
2423 // Clear the imported field for the next file.
2424 void
2425 clear_is_imported()
2426 { this->is_imported_ = false; }
2427
2428 // Whether this package was imported with a name of "_".
2429 bool
2430 uses_sink_alias() const
2431 { return this->uses_sink_alias_; }
2432
2433 // Note that this package was imported with a name of "_".
2434 void
2435 set_uses_sink_alias()
2436 { this->uses_sink_alias_ = true; }
2437
2438 // Clear the sink alias field for the next file.
2439 void
2440 clear_uses_sink_alias()
2441 { this->uses_sink_alias_ = false; }
2442
2443 // Look up a name in the package. Returns NULL if the name is not
2444 // found.
2445 Named_object*
2446 lookup(const std::string& name) const
2447 { return this->bindings_->lookup(name); }
2448
2449 // Set the location of the package. This is used if it is seen in a
2450 // different import before it is really imported.
2451 void
2452 set_location(Location location)
2453 { this->location_ = location; }
2454
2455 // Add a constant to the package.
2456 Named_object*
2457 add_constant(const Typed_identifier& tid, Expression* expr)
2458 { return this->bindings_->add_constant(tid, this, expr, 0); }
2459
2460 // Add a type to the package.
2461 Named_object*
2462 add_type(const std::string& name, Type* type, Location location)
2463 { return this->bindings_->add_type(name, this, type, location); }
2464
2465 // Add a type declaration to the package.
2466 Named_object*
2467 add_type_declaration(const std::string& name, Location location)
2468 { return this->bindings_->add_type_declaration(name, this, location); }
2469
2470 // Add a variable to the package.
2471 Named_object*
2472 add_variable(const std::string& name, Variable* variable)
2473 { return this->bindings_->add_variable(name, this, variable); }
2474
2475 // Add a function declaration to the package.
2476 Named_object*
2477 add_function_declaration(const std::string& name, Function_type* type,
2478 Location loc)
2479 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2480
2481 // Determine types of constants.
2482 void
2483 determine_types();
2484
2485 private:
2486 // The real name of this package.
2487 std::string name_;
2488 // The unique prefix for all exported global symbols.
2489 std::string unique_prefix_;
2490 // The names in this package.
2491 Bindings* bindings_;
2492 // The priority of this package. A package has a priority higher
2493 // than the priority of all of the packages that it imports. This
2494 // is used to run init functions in the right order.
2495 int priority_;
2496 // The location of the import statement.
2497 Location location_;
2498 // True if some name from this package was used. This is mutable
2499 // because we can use a package even if we have a const pointer to
2500 // it.
2501 mutable bool used_;
2502 // True if this package was imported in the current file.
2503 bool is_imported_;
2504 // True if this package was imported with a name of "_".
2505 bool uses_sink_alias_;
2506 };
2507
2508 // Return codes for the traversal functions. This is not an enum
2509 // because we want to be able to declare traversal functions in other
2510 // header files without including this one.
2511
2512 // Continue traversal as usual.
2513 const int TRAVERSE_CONTINUE = -1;
2514
2515 // Exit traversal.
2516 const int TRAVERSE_EXIT = 0;
2517
2518 // Continue traversal, but skip components of the current object.
2519 // E.g., if this is returned by Traverse::statement, we do not
2520 // traverse the expressions in the statement even if
2521 // traverse_expressions is set in the traverse_mask.
2522 const int TRAVERSE_SKIP_COMPONENTS = 1;
2523
2524 // This class is used when traversing the parse tree. The caller uses
2525 // a subclass which overrides functions as desired.
2526
2527 class Traverse
2528 {
2529 public:
2530 // These bitmasks say what to traverse.
2531 static const unsigned int traverse_variables = 0x1;
2532 static const unsigned int traverse_constants = 0x2;
2533 static const unsigned int traverse_functions = 0x4;
2534 static const unsigned int traverse_blocks = 0x8;
2535 static const unsigned int traverse_statements = 0x10;
2536 static const unsigned int traverse_expressions = 0x20;
2537 static const unsigned int traverse_types = 0x40;
2538
2539 Traverse(unsigned int traverse_mask)
2540 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2541 { }
2542
2543 virtual ~Traverse();
2544
2545 // The bitmask of what to traverse.
2546 unsigned int
2547 traverse_mask() const
2548 { return this->traverse_mask_; }
2549
2550 // Record that we are going to traverse a type. This returns true
2551 // if the type has already been seen in this traversal. This is
2552 // required because types, unlike expressions, can form a circular
2553 // graph.
2554 bool
2555 remember_type(const Type*);
2556
2557 // Record that we are going to see an expression. This returns true
2558 // if the expression has already been seen in this traversal. This
2559 // is only needed for cases where multiple expressions can point to
2560 // a single one.
2561 bool
2562 remember_expression(const Expression*);
2563
2564 // These functions return one of the TRAVERSE codes defined above.
2565
2566 // If traverse_variables is set in the mask, this is called for
2567 // every variable in the tree.
2568 virtual int
2569 variable(Named_object*);
2570
2571 // If traverse_constants is set in the mask, this is called for
2572 // every named constant in the tree. The bool parameter is true for
2573 // a global constant.
2574 virtual int
2575 constant(Named_object*, bool);
2576
2577 // If traverse_functions is set in the mask, this is called for
2578 // every function in the tree.
2579 virtual int
2580 function(Named_object*);
2581
2582 // If traverse_blocks is set in the mask, this is called for every
2583 // block in the tree.
2584 virtual int
2585 block(Block*);
2586
2587 // If traverse_statements is set in the mask, this is called for
2588 // every statement in the tree.
2589 virtual int
2590 statement(Block*, size_t* index, Statement*);
2591
2592 // If traverse_expressions is set in the mask, this is called for
2593 // every expression in the tree.
2594 virtual int
2595 expression(Expression**);
2596
2597 // If traverse_types is set in the mask, this is called for every
2598 // type in the tree.
2599 virtual int
2600 type(Type*);
2601
2602 private:
2603 // A hash table for types we have seen during this traversal. Note
2604 // that this uses the default hash functions for pointers rather
2605 // than Type_hash_identical and Type_identical. This is because for
2606 // traversal we care about seeing a specific type structure. If
2607 // there are two separate instances of identical types, we want to
2608 // traverse both.
2609 typedef Unordered_set(const Type*) Types_seen;
2610
2611 typedef Unordered_set(const Expression*) Expressions_seen;
2612
2613 // Bitmask of what sort of objects to traverse.
2614 unsigned int traverse_mask_;
2615 // Types which have been seen in this traversal.
2616 Types_seen* types_seen_;
2617 // Expressions which have been seen in this traversal.
2618 Expressions_seen* expressions_seen_;
2619 };
2620
2621 // A class which makes it easier to insert new statements before the
2622 // current statement during a traversal.
2623
2624 class Statement_inserter
2625 {
2626 public:
2627 // Empty constructor.
2628 Statement_inserter()
2629 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2630 { }
2631
2632 // Constructor for a statement in a block.
2633 Statement_inserter(Block* block, size_t *pindex)
2634 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2635 { }
2636
2637 // Constructor for a global variable.
2638 Statement_inserter(Gogo* gogo, Variable* var)
2639 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2640 { go_assert(var->is_global()); }
2641
2642 // We use the default copy constructor and assignment operator.
2643
2644 // Insert S before the statement we are traversing, or before the
2645 // initialization expression of a global variable.
2646 void
2647 insert(Statement* s);
2648
2649 private:
2650 // The block that the statement is in.
2651 Block* block_;
2652 // The index of the statement that we are traversing.
2653 size_t* pindex_;
2654 // The IR, needed when looking at an initializer expression for a
2655 // global variable.
2656 Gogo* gogo_;
2657 // The global variable, when looking at an initializer expression.
2658 Variable* var_;
2659 };
2660
2661 // When translating the gogo IR into the backend data structure, this
2662 // is the context we pass down the blocks and statements.
2663
2664 class Translate_context
2665 {
2666 public:
2667 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2668 Bblock* bblock)
2669 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2670 block_(block), bblock_(bblock), is_const_(false)
2671 { }
2672
2673 // Accessors.
2674
2675 Gogo*
2676 gogo()
2677 { return this->gogo_; }
2678
2679 Backend*
2680 backend()
2681 { return this->backend_; }
2682
2683 Named_object*
2684 function()
2685 { return this->function_; }
2686
2687 Block*
2688 block()
2689 { return this->block_; }
2690
2691 Bblock*
2692 bblock()
2693 { return this->bblock_; }
2694
2695 bool
2696 is_const()
2697 { return this->is_const_; }
2698
2699 // Make a constant context.
2700 void
2701 set_is_const()
2702 { this->is_const_ = true; }
2703
2704 private:
2705 // The IR for the entire compilation unit.
2706 Gogo* gogo_;
2707 // The generator for the backend data structures.
2708 Backend* backend_;
2709 // The function we are currently translating. NULL if not in a
2710 // function, e.g., the initializer of a global variable.
2711 Named_object* function_;
2712 // The block we are currently translating. NULL if not in a
2713 // function.
2714 Block *block_;
2715 // The backend representation of the current block. NULL if block_
2716 // is NULL.
2717 Bblock* bblock_;
2718 // Whether this is being evaluated in a constant context. This is
2719 // used for type descriptor initializers.
2720 bool is_const_;
2721 };
2722
2723 // Runtime error codes. These must match the values in
2724 // libgo/runtime/go-runtime-error.c.
2725
2726 // Slice index out of bounds: negative or larger than the length of
2727 // the slice.
2728 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2729
2730 // Array index out of bounds.
2731 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2732
2733 // String index out of bounds.
2734 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2735
2736 // Slice slice out of bounds: negative or larger than the length of
2737 // the slice or high bound less than low bound.
2738 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2739
2740 // Array slice out of bounds.
2741 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2742
2743 // String slice out of bounds.
2744 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2745
2746 // Dereference of nil pointer. This is used when there is a
2747 // dereference of a pointer to a very large struct or array, to ensure
2748 // that a gigantic array is not used a proxy to access random memory
2749 // locations.
2750 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2751
2752 // Slice length or capacity out of bounds in make: negative or
2753 // overflow or length greater than capacity.
2754 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2755
2756 // Map capacity out of bounds in make: negative or overflow.
2757 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2758
2759 // Channel capacity out of bounds in make: negative or overflow.
2760 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2761
2762 // This is used by some of the langhooks.
2763 extern Gogo* go_get_gogo();
2764
2765 // Whether we have seen any errors. FIXME: Replace with a backend
2766 // interface.
2767 extern bool saw_errors();
2768
2769 #endif // !defined(GO_GOGO_H)