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