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