Branch data Line data Source code
1 : : // gogo.h -- Go frontend parsed representation. -*- C++ -*-
2 : :
3 : : // Copyright 2009 The Go Authors. All rights reserved.
4 : : // Use of this source code is governed by a BSD-style
5 : : // license that can be found in the LICENSE file.
6 : :
7 : : #ifndef GO_GOGO_H
8 : : #define GO_GOGO_H
9 : :
10 : : #include "go-linemap.h"
11 : :
12 : : class Traverse;
13 : : class Statement_inserter;
14 : : class Type;
15 : : class Type_equal;
16 : : class Typed_identifier;
17 : : class Typed_identifier_list;
18 : : class Function_type;
19 : : class Expression;
20 : : class Expression_list;
21 : : class Statement;
22 : : class Temporary_statement;
23 : : class Block;
24 : : class Function;
25 : : class Bindings;
26 : : class Bindings_snapshot;
27 : : class Package;
28 : : class Variable;
29 : : class Pointer_type;
30 : : class Struct_type;
31 : : class Struct_field;
32 : : class Struct_field_list;
33 : : class Array_type;
34 : : class Map_type;
35 : : class Channel_type;
36 : : class Interface_type;
37 : : class Named_type;
38 : : class Forward_declaration_type;
39 : : class Named_object;
40 : : class Label;
41 : : class Translate_context;
42 : : class Backend;
43 : : class Export;
44 : : class Export_function_body;
45 : : class Import;
46 : : class Import_function_body;
47 : : class Bexpression;
48 : : class Btype;
49 : : class Bstatement;
50 : : class Bblock;
51 : : class Bvariable;
52 : : class Blabel;
53 : : class Bfunction;
54 : : class Escape_context;
55 : : class Node;
56 : :
57 : : // This file declares the basic classes used to hold the internal
58 : : // representation of Go which is built by the parser.
59 : :
60 : : // The name of some backend object. Backend objects have a
61 : : // user-visible name and an assembler name. The user visible name
62 : : // might include arbitrary Unicode characters. The assembler name
63 : : // will not.
64 : :
65 : : class Backend_name
66 : : {
67 : : public:
68 : 3301877 : Backend_name()
69 : 16509385 : : prefix_(NULL), components_(), count_(0), suffix_(),
70 : 3301877 : is_asm_name_(false), is_non_identifier_(false)
71 : 3301877 : {}
72 : :
73 : : // Set the prefix. Prefixes are always constant strings.
74 : : void
75 : 1372973 : set_prefix(const char* p)
76 : : {
77 : 1372973 : go_assert(this->prefix_ == NULL && !this->is_asm_name_);
78 : 1372973 : this->prefix_ = p;
79 : 1372973 : }
80 : :
81 : : // Set the suffix.
82 : : void
83 : 973406 : set_suffix(const std::string& s)
84 : : {
85 : 973406 : go_assert(this->suffix_.empty() && !this->is_asm_name_);
86 : 973406 : this->suffix_ = s;
87 : 973406 : }
88 : :
89 : : // Append to the suffix.
90 : : void
91 : 295509 : append_suffix(const std::string& s)
92 : : {
93 : 295509 : if (this->is_asm_name_)
94 : 182773 : this->components_[0].append(s);
95 : : else
96 : 112736 : this->suffix_.append(s);
97 : 295509 : }
98 : :
99 : : // Add a component.
100 : : void
101 : 4468736 : add(const std::string& c)
102 : : {
103 : 4468736 : go_assert(this->count_ < Backend_name::max_components
104 : : && !this->is_asm_name_);
105 : 4468736 : this->components_[this->count_] = c;
106 : 4468736 : ++this->count_;
107 : 4468736 : }
108 : :
109 : : // Set an assembler name specified by the user. This overrides both
110 : : // the user-visible name and the assembler name. No further
111 : : // encoding is applied.
112 : : void
113 : 409350 : set_asm_name(const std::string& n)
114 : : {
115 : 409350 : go_assert(this->prefix_ == NULL
116 : : && this->count_ == 0
117 : : && this->suffix_.empty()
118 : : && !this->is_asm_name_);
119 : 409350 : this->components_[0] = n;
120 : 409350 : this->is_asm_name_ = true;
121 : 409350 : }
122 : :
123 : : // Whether some component includes some characters that can't appear
124 : : // in an identifier.
125 : : bool
126 : 603 : is_non_identifier() const
127 : 603 : { return this->is_non_identifier_; }
128 : :
129 : : // Record that some component includes some character that can't
130 : : // appear in an identifier.
131 : : void
132 : 1532317 : set_is_non_identifier()
133 : 1532317 : { this->is_non_identifier_ = true; }
134 : :
135 : : // Get the user visible name.
136 : : std::string
137 : : name() const;
138 : :
139 : : // Get the assembler name. This may be the same as the user visible
140 : : // name.
141 : : std::string
142 : : asm_name() const;
143 : :
144 : : // Get an optional assembler name: if it would be the same as the
145 : : // user visible name, this returns the empty string.
146 : : std::string
147 : : optional_asm_name() const;
148 : :
149 : : private:
150 : : // The maximum number of components.
151 : : static const int max_components = 4;
152 : :
153 : : // An optional prefix that does not require encoding.
154 : : const char *prefix_;
155 : : // Up to four components. The name will include these components
156 : : // separated by dots. Each component will be underscore-encoded
157 : : // (see the long comment near the top of names.cc).
158 : : std::string components_[Backend_name::max_components];
159 : : // Number of components.
160 : : int count_;
161 : : // An optional suffix that does not require encoding.
162 : : std::string suffix_;
163 : : // True if components_[0] is an assembler name specified by the user.
164 : : bool is_asm_name_;
165 : : // True if some component includes some character that can't
166 : : // normally appear in an identifier.
167 : : bool is_non_identifier_;
168 : : };
169 : :
170 : : // An initialization function for an imported package. This is a
171 : : // magic function which initializes variables and runs the "init"
172 : : // function.
173 : :
174 : : class Import_init
175 : : {
176 : : public:
177 : 1711998 : Import_init(const std::string& package_name, const std::string& init_name,
178 : : int priority)
179 : 3423996 : : package_name_(package_name), init_name_(init_name), priority_(priority)
180 : 1711998 : { }
181 : :
182 : : // The name of the package being imported.
183 : : const std::string&
184 : 749508 : package_name() const
185 : 691914 : { return this->package_name_; }
186 : :
187 : : // The name of the package's init function.
188 : : const std::string&
189 : 43829421 : init_name() const
190 : 34450671 : { return this->init_name_; }
191 : :
192 : : // Older V1 export data uses a priority scheme to order
193 : : // initialization functions; functions with a lower priority number
194 : : // must be run first. This value will be set to -1 for current
195 : : // generation objects, and will take on a non-negative value only
196 : : // when importing a V1-vintage object.
197 : : int
198 : 931958 : priority() const
199 : 701905 : { return this->priority_; }
200 : :
201 : : // Reset priority.
202 : : void
203 : 560443 : set_priority(int new_priority)
204 : 560443 : { this->priority_ = new_priority; }
205 : :
206 : : // Record the fact that some other init fcn must be run before this init fcn.
207 : : void
208 : 3302679 : record_precursor_fcn(std::string init_fcn_name)
209 : 3302679 : { this->precursor_functions_.insert(init_fcn_name); }
210 : :
211 : : // Return the list of precursor fcns for this fcn (must be run before it).
212 : : const std::set<std::string>&
213 : : precursors() const
214 : 2329786 : { return this->precursor_functions_; }
215 : :
216 : : // Whether this is a dummy init, which is used only to record transitive import.
217 : : bool
218 : 301817 : is_dummy() const
219 : 301817 : { return this->init_name_[0] == '~'; }
220 : :
221 : : private:
222 : : // The name of the package being imported.
223 : : std::string package_name_;
224 : : // The name of the package's init function.
225 : : std::string init_name_;
226 : : // Names of init functions that must be run before this fcn.
227 : : std::set<std::string> precursor_functions_;
228 : : // Priority for this function. See note above on obsolescence.
229 : : int priority_;
230 : : };
231 : :
232 : : // For sorting purposes.
233 : :
234 : : struct Import_init_lt {
235 : 12743765 : bool operator()(const Import_init* i1, const Import_init* i2) const
236 : : {
237 : 12743765 : return i1->init_name() < i2->init_name();
238 : : }
239 : : };
240 : :
241 : : // Set of import init objects.
242 : 4646 : class Import_init_set : public std::set<Import_init*, Import_init_lt> {
243 : : };
244 : :
245 : : inline bool
246 : 230053 : priority_compare(const Import_init* i1, const Import_init* i2)
247 : : {
248 : 230053 : if (i1->priority() < i2->priority())
249 : : return true;
250 : 119730 : if (i1->priority() > i2->priority())
251 : : return false;
252 : 57594 : if (i1->package_name() != i2->package_name())
253 : 57502 : return i1->package_name() < i2->package_name();
254 : 92 : return i1->init_name() < i2->init_name();
255 : : }
256 : :
257 : : // The holder for the internal representation of the entire
258 : : // compilation unit.
259 : :
260 : : class Gogo
261 : : {
262 : : public:
263 : : // Create the IR, passing in the sizes of the types "int" and
264 : : // "uintptr" in bits.
265 : : Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
266 : :
267 : : // Get the backend generator.
268 : : Backend*
269 : 86369909 : backend()
270 : 67744768 : { return this->backend_; }
271 : :
272 : : // Get the Location generator.
273 : : Linemap*
274 : 245990 : linemap()
275 : 125548 : { return this->linemap_; }
276 : :
277 : : // Get the package name.
278 : : const std::string&
279 : : package_name() const;
280 : :
281 : : // Set the package name.
282 : : void
283 : : set_package_name(const std::string&, Location);
284 : :
285 : : // Return whether this is the "main" package.
286 : : bool
287 : : is_main_package() const;
288 : :
289 : : // If necessary, adjust the name to use for a hidden symbol. We add
290 : : // the package name, so that hidden symbols in different packages do
291 : : // not collide.
292 : : std::string
293 : 6225586 : pack_hidden_name(const std::string& name, bool is_exported) const
294 : : {
295 : 6225586 : return (is_exported
296 : 6225586 : ? name
297 : 16756272 : : '.' + this->pkgpath() + '.' + name);
298 : : }
299 : :
300 : : // Unpack a name which may have been hidden. Returns the
301 : : // user-visible name of the object.
302 : : static std::string
303 : 24366779 : unpack_hidden_name(const std::string& name)
304 : 40984944 : { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
305 : :
306 : : // Return whether a possibly packed name is hidden.
307 : : static bool
308 : 9440319 : is_hidden_name(const std::string& name)
309 : 8394146 : { return name[0] == '.'; }
310 : :
311 : : // Return the package path of a hidden name.
312 : : static std::string
313 : 400197 : hidden_name_pkgpath(const std::string& name)
314 : : {
315 : 400197 : go_assert(Gogo::is_hidden_name(name));
316 : 400197 : return name.substr(1, name.rfind('.') - 1);
317 : : }
318 : :
319 : : // Given a name which may or may not have been hidden, append the
320 : : // appropriate version of the name to the result string.
321 : : static void
322 : : append_possibly_hidden_name(std::string *result, const std::string& name);
323 : :
324 : : // Given a name which may or may not have been hidden, return the
325 : : // name to use in an error message.
326 : : static std::string
327 : : message_name(const std::string& name);
328 : :
329 : : // Return whether a name is the blank identifier _.
330 : : static bool
331 : 27074660 : is_sink_name(const std::string& name)
332 : : {
333 : 27074660 : return (name[0] == '.'
334 : 14378044 : && name[name.length() - 1] == '_'
335 : 83786 : && name[name.length() - 2] == '.')
336 : 41390850 : || (name[0] == '_'
337 : 1461967 : && name.length() == 1);
338 : : }
339 : :
340 : : // Helper used when adding parameters (including receiver param) to the
341 : : // bindings of a function. If the specified parameter name is empty or
342 : : // corresponds to the sink name, param name is replaced with a new unique
343 : : // name. PNAME is the address of a string containing the parameter variable
344 : : // name to be checked/updated; TAG is a descriptive tag to be used in
345 : : // manufacturing the new unique name, and COUNT is the address of a counter
346 : : // holding the number of params renamed so far with the tag in question.
347 : : static void
348 : : rename_if_empty(std::string* pname, const char* tag, unsigned* count);
349 : :
350 : : // Convert a pkgpath into a string suitable for a symbol
351 : : static std::string
352 : : pkgpath_for_symbol(const std::string& pkgpath);
353 : :
354 : : // Compute a hash code for a string, given a seed.
355 : : static unsigned int
356 : : hash_string(const std::string&, unsigned int);
357 : :
358 : : // Return the package path to use for reflect.Type.PkgPath.
359 : : const std::string&
360 : : pkgpath() const;
361 : :
362 : : // Return the package path to use for a symbol name.
363 : : const std::string&
364 : : pkgpath_symbol() const;
365 : :
366 : : // Set the package path from a command line option.
367 : : void
368 : : set_pkgpath(const std::string&);
369 : :
370 : : // Set the prefix from a command line option.
371 : : void
372 : : set_prefix(const std::string&);
373 : :
374 : : // Return whether pkgpath was set from a command line option.
375 : : bool
376 : 29 : pkgpath_from_option() const
377 : 29 : { return this->pkgpath_from_option_; }
378 : :
379 : : // Return the relative import path as set from the command line.
380 : : // Returns an empty string if it was not set.
381 : : const std::string&
382 : : relative_import_path() const
383 : : { return this->relative_import_path_; }
384 : :
385 : : // Set the relative import path from a command line option.
386 : : void
387 : 16 : set_relative_import_path(const std::string& s)
388 : 16 : { this->relative_import_path_ = s; }
389 : :
390 : : // Set the C header file to write. This is used for the runtime
391 : : // package.
392 : : void
393 : 4 : set_c_header(const std::string& s)
394 : 4 : { this->c_header_ = s; }
395 : :
396 : : // Read an importcfg file.
397 : : void
398 : : read_importcfg(const char* filename);
399 : :
400 : : // Read an embedcfg file.
401 : : void
402 : : read_embedcfg(const char* filename);
403 : :
404 : : // Build an initializer for a variable with a go:embed directive.
405 : : Expression*
406 : : initializer_for_embeds(Type*, const std::vector<std::string>*, Location);
407 : :
408 : : // Return whether to check for division by zero in binary operations.
409 : : bool
410 : 23473 : check_divide_by_zero() const
411 : 23473 : { return this->check_divide_by_zero_; }
412 : :
413 : : // Set the option to check division by zero from a command line option.
414 : : void
415 : 4646 : set_check_divide_by_zero(bool b)
416 : 4646 : { this->check_divide_by_zero_ = b; }
417 : :
418 : : // Return whether to check for division overflow in binary operations.
419 : : bool
420 : 11583 : check_divide_overflow() const
421 : 11583 : { return this->check_divide_overflow_; }
422 : :
423 : : // Set the option to check division overflow from a command line option.
424 : : void
425 : 4646 : set_check_divide_overflow(bool b)
426 : 4646 : { this->check_divide_overflow_ = b; }
427 : :
428 : : // Return whether we are compiling the runtime package.
429 : : bool
430 : 491161 : compiling_runtime() const
431 : 491161 : { return this->compiling_runtime_; }
432 : :
433 : : // Set whether we are compiling the runtime package.
434 : : void
435 : 21 : set_compiling_runtime(bool b)
436 : 21 : { this->compiling_runtime_ = b; }
437 : :
438 : : // Return the level of escape analysis debug information to emit.
439 : : int
440 : 58718494 : debug_escape_level() const
441 : 58718494 : { return this->debug_escape_level_; }
442 : :
443 : : // Set the level of escape analysis debugging from a command line option.
444 : : void
445 : 4646 : set_debug_escape_level(int level)
446 : 4646 : { this->debug_escape_level_ = level; }
447 : :
448 : : // Return the hash for debug escape analysis.
449 : : std::string
450 : 1362002 : debug_escape_hash() const
451 : 2724004 : { return this->debug_escape_hash_; }
452 : :
453 : : // Set the hash value for debug escape analysis.
454 : : void
455 : 0 : set_debug_escape_hash(const std::string& s)
456 : 0 : { this->debug_escape_hash_ = s; }
457 : :
458 : : // Return whether to output optimization diagnostics.
459 : : bool
460 : 12960 : debug_optimization() const
461 : 12960 : { return this->debug_optimization_; }
462 : :
463 : : // Set the option to output optimization diagnostics.
464 : : void
465 : 5 : set_debug_optimization(bool b)
466 : 5 : { this->debug_optimization_ = b; }
467 : :
468 : : // Dump to stderr for debugging
469 : : void debug_dump();
470 : :
471 : : // Return the size threshold used to determine whether to issue
472 : : // a nil-check for a given pointer dereference. A threshold of -1
473 : : // implies that all potentially faulting dereference ops should
474 : : // be nil-checked. A positive threshold of N implies that a deref
475 : : // of *P where P has size less than N doesn't need a nil check.
476 : : int64_t
477 : 561176 : nil_check_size_threshold() const
478 : 561176 : { return this->nil_check_size_threshold_; }
479 : :
480 : : // Set the nil-check size threshold, as described above.
481 : : void
482 : 4646 : set_nil_check_size_threshold(int64_t bytes)
483 : 4646 : { this->nil_check_size_threshold_ = bytes; }
484 : :
485 : : // Return whether runtime.eqtype calls are needed when comparing
486 : : // type descriptors.
487 : : bool
488 : 23620 : need_eqtype() const
489 : 23620 : { return this->need_eqtype_; }
490 : :
491 : : // Set if calls to runtime.eqtype are needed.
492 : : void
493 : 0 : set_need_eqtype(bool b)
494 : 0 : { this->need_eqtype_ = b; }
495 : :
496 : : // Import a package. FILENAME is the file name argument, LOCAL_NAME
497 : : // is the local name to give to the package. If LOCAL_NAME is empty
498 : : // the declarations are added to the global scope.
499 : : void
500 : : import_package(const std::string& filename, const std::string& local_name,
501 : : bool is_local_name_exported, bool must_exist, Location);
502 : :
503 : : // Whether we are the global binding level.
504 : : bool
505 : : in_global_scope() const;
506 : :
507 : : // Look up a name in the current binding contours.
508 : : Named_object*
509 : : lookup(const std::string&, Named_object** pfunction) const;
510 : :
511 : : // Look up a name in the current block.
512 : : Named_object*
513 : : lookup_in_block(const std::string&) const;
514 : :
515 : : // Look up a name in the global namespace--the universal scope.
516 : : Named_object*
517 : : lookup_global(const char*) const;
518 : :
519 : : // Add a new imported package. REAL_NAME is the real name of the
520 : : // package. ALIAS is the alias of the package; this may be the same
521 : : // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
522 : : // this package should be added to the global namespace; this is
523 : : // true if the alias is ".". LOCATION is the location of the import
524 : : // statement. This returns the new package, or NULL on error.
525 : : Package*
526 : : add_imported_package(const std::string& real_name, const std::string& alias,
527 : : bool is_alias_exported,
528 : : const std::string& pkgpath,
529 : : const std::string& pkgpath_symbol,
530 : : Location location,
531 : : bool* padd_to_globals);
532 : :
533 : : // Register a package. This package may or may not be imported.
534 : : // This returns the Package structure for the package, creating if
535 : : // it necessary.
536 : : Package*
537 : : register_package(const std::string& pkgpath,
538 : : const std::string& pkgpath_symbol, Location);
539 : :
540 : : // Add the unsafe bindings to the unsafe package.
541 : : void
542 : : add_unsafe_bindings(Package*);
543 : :
544 : : // Look up a package by pkgpath, and return its pkgpath_symbol.
545 : : std::string
546 : : pkgpath_symbol_for_package(const std::string&);
547 : :
548 : : // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
549 : : // method function should be added to the type of its receiver.
550 : : Named_object*
551 : : start_function(const std::string& name, Function_type* type,
552 : : bool add_method_to_type, Location);
553 : :
554 : : // Finish compiling a function.
555 : : void
556 : : finish_function(Location);
557 : :
558 : : // Return the current function.
559 : : Named_object*
560 : : current_function() const;
561 : :
562 : : // Return the current block.
563 : : Block*
564 : : current_block();
565 : :
566 : : // Start a new block. This is not initially associated with a
567 : : // function.
568 : : void
569 : : start_block(Location);
570 : :
571 : : // Finish the current block and return it.
572 : : Block*
573 : : finish_block(Location);
574 : :
575 : : // Declare an erroneous name. This is used to avoid knock-on errors
576 : : // after a parsing error.
577 : : Named_object*
578 : : add_erroneous_name(const std::string& name);
579 : :
580 : : // Declare an unknown name. This is used while parsing. The name
581 : : // must be resolved by the end of the parse. Unknown names are
582 : : // always added at the package level.
583 : : Named_object*
584 : : add_unknown_name(const std::string& name, Location);
585 : :
586 : : // Declare a function.
587 : : Named_object*
588 : : declare_function(const std::string&, Function_type*, Location);
589 : :
590 : : // Declare a function at the package level. This is used for
591 : : // functions generated for a type.
592 : : Named_object*
593 : : declare_package_function(const std::string&, Function_type*, Location);
594 : :
595 : : // Add a function declaration to the list of functions we may want
596 : : // to inline.
597 : : void
598 : : add_imported_inlinable_function(Named_object*);
599 : :
600 : : // Add a function to the list of functions that we do want to
601 : : // inline.
602 : : void
603 : 12247 : add_imported_inline_function(Named_object* no)
604 : 12247 : { this->imported_inline_functions_.push_back(no); }
605 : :
606 : : // Add a label.
607 : : Label*
608 : : add_label_definition(const std::string&, Location);
609 : :
610 : : // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
611 : : // report errors for a goto from the current location to the label
612 : : // location.
613 : : Label*
614 : : add_label_reference(const std::string&, Location,
615 : : bool issue_goto_errors);
616 : :
617 : : // An analysis set is a list of functions paired with a boolean that indicates
618 : : // whether the list of functions are recursive.
619 : : typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
620 : :
621 : : // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
622 : : // package.
623 : : void
624 : 1357750 : add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
625 : 1357750 : { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
626 : :
627 : : // Return a snapshot of the current binding state.
628 : : Bindings_snapshot*
629 : : bindings_snapshot(Location);
630 : :
631 : : // Add a statement to the current block.
632 : : void
633 : : add_statement(Statement*);
634 : :
635 : : // Add a block to the current block.
636 : : void
637 : : add_block(Block*, Location);
638 : :
639 : : // Add a constant.
640 : : Named_object*
641 : : add_constant(const Typed_identifier&, Expression*, int iota_value);
642 : :
643 : : // Add a type.
644 : : void
645 : : add_type(const std::string&, Type*, Location);
646 : :
647 : : // Add a named type. This is used for builtin types, and to add an
648 : : // imported type to the global scope.
649 : : void
650 : : add_named_type(Named_type*);
651 : :
652 : : // Declare a type.
653 : : Named_object*
654 : : declare_type(const std::string&, Location);
655 : :
656 : : // Declare a type at the package level. This is used when the
657 : : // parser sees an unknown name where a type name is required.
658 : : Named_object*
659 : : declare_package_type(const std::string&, Location);
660 : :
661 : : // Define a type which was already declared.
662 : : void
663 : : define_type(Named_object*, Named_type*);
664 : :
665 : : // Add a variable.
666 : : Named_object*
667 : : add_variable(const std::string&, Variable*);
668 : :
669 : : // Add a sink--a reference to the blank identifier _.
670 : : Named_object*
671 : : add_sink();
672 : :
673 : : // Add a type which needs to be verified. This is used for sink
674 : : // types, just to give appropriate error messages.
675 : : void
676 : : add_type_to_verify(Type* type);
677 : :
678 : : // Add a named object to the current namespace. This is used for
679 : : // import . "package".
680 : : void
681 : : add_dot_import_object(Named_object*);
682 : :
683 : : // Add an identifier to the list of names seen in the file block.
684 : : void
685 : 101917 : add_file_block_name(const std::string& name, Location location)
686 : 101917 : { this->file_block_names_[name] = location; }
687 : :
688 : : // Add a linkname, from the go:linkname compiler directive. This
689 : : // changes the externally visible name of GO_NAME to be EXT_NAME.
690 : : // If EXT_NAME is the empty string, GO_NAME is unchanged, but the
691 : : // symbol is made publicly visible.
692 : : void
693 : : add_linkname(const std::string& go_name, bool is_exported,
694 : : const std::string& ext_name, Location location);
695 : :
696 : : // Mark all local variables in current bindings as used. This is
697 : : // used when there is a parse error to avoid useless errors.
698 : : void
699 : : mark_locals_used();
700 : :
701 : : // Note that we've seen an interface type. This is used to build
702 : : // all required interface method tables.
703 : : void
704 : : record_interface_type(Interface_type*);
705 : :
706 : : // Whether we need an initialization function.
707 : : bool
708 : 451472 : need_init_fn() const
709 : 451472 : { return this->need_init_fn_; }
710 : :
711 : : // Note that we need an initialization function.
712 : : void
713 : 58103 : set_need_init_fn()
714 : 5123 : { this->need_init_fn_ = true; }
715 : :
716 : : // Return whether the current file imported the unsafe package.
717 : : bool
718 : 538 : current_file_imported_unsafe() const
719 : 538 : { return this->current_file_imported_unsafe_; }
720 : :
721 : : // Return whether the current file imported the embed package.
722 : : bool
723 : 22 : current_file_imported_embed() const
724 : 22 : { return this->current_file_imported_embed_; }
725 : :
726 : : // Clear out all names in file scope. This is called when we start
727 : : // parsing a new file.
728 : : void
729 : : clear_file_scope();
730 : :
731 : : // Record that VAR1 must be initialized after VAR2. This is used
732 : : // when VAR2 does not appear in VAR1's INIT or PREINIT.
733 : : void
734 : 99 : record_var_depends_on(Variable* var1, Named_object* var2)
735 : : {
736 : 99 : go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
737 : 99 : this->var_deps_[var1] = var2;
738 : 99 : }
739 : :
740 : : // Return the variable that VAR depends on, or NULL if none.
741 : : Named_object*
742 : 7374598 : var_depends_on(Variable* var) const
743 : : {
744 : 7374598 : Var_deps::const_iterator p = this->var_deps_.find(var);
745 : 7374598 : return p != this->var_deps_.end() ? p->second : NULL;
746 : : }
747 : :
748 : : // Queue up a type-specific hash function to be written out. This
749 : : // is used when a type-specific hash function is needed when not at
750 : : // top level.
751 : : void
752 : : queue_hash_function(Type* type, int64_t size, Backend_name*,
753 : : Function_type* hash_fntype);
754 : :
755 : : // Queue up a type-specific equal function to be written out. This
756 : : // is used when a type-specific equal function is needed when not at
757 : : // top level.
758 : : void
759 : : queue_equal_function(Type* type, Named_type* name, int64_t size,
760 : : Backend_name*, Function_type* equal_fntype);
761 : :
762 : : // Write out queued specific type functions.
763 : : void
764 : : write_specific_type_functions();
765 : :
766 : : // Whether we are done writing out specific type functions.
767 : : bool
768 : 92659 : specific_type_functions_are_written() const
769 : 92659 : { return this->specific_type_functions_are_written_; }
770 : :
771 : : // Add a pointer that needs to be added to the list of objects
772 : : // traversed by the garbage collector. This should be an expression
773 : : // of pointer type that points to static storage. It's not
774 : : // necessary to add global variables to this list, just global
775 : : // variable initializers that would otherwise not be seen.
776 : : void
777 : 2660 : add_gc_root(Expression* expr)
778 : : {
779 : 2660 : this->set_need_init_fn();
780 : 2660 : this->gc_roots_.push_back(expr);
781 : : }
782 : :
783 : : // Add a type to the descriptor list.
784 : : void
785 : 176258 : add_type_descriptor(Type* type)
786 : 176258 : { this->type_descriptors_.push_back(type); }
787 : :
788 : : // Traverse the tree. See the Traverse class.
789 : : void
790 : : traverse(Traverse*);
791 : :
792 : : // Define the predeclared global names.
793 : : void
794 : : define_global_names();
795 : :
796 : : // Verify and complete all types.
797 : : void
798 : : verify_types();
799 : :
800 : : // Lower the parse tree.
801 : : void
802 : : lower_parse_tree();
803 : :
804 : : // Lower all the statements in a block.
805 : : void
806 : : lower_block(Named_object* function, Block*);
807 : :
808 : : // Lower an expression.
809 : : void
810 : : lower_expression(Named_object* function, Statement_inserter*, Expression**);
811 : :
812 : : // Lower a constant.
813 : : void
814 : : lower_constant(Named_object*);
815 : :
816 : : // Flatten all the statements in a block.
817 : : void
818 : : flatten_block(Named_object* function, Block*);
819 : :
820 : : // Flatten an expression.
821 : : void
822 : : flatten_expression(Named_object* function, Statement_inserter*, Expression**);
823 : :
824 : : // Create all necessary function descriptors.
825 : : void
826 : : create_function_descriptors();
827 : :
828 : : // Lower calls to builtin functions.
829 : : void
830 : : lower_builtin_calls();
831 : :
832 : : // Finalize the method lists and build stub methods for named types.
833 : : void
834 : : finalize_methods();
835 : :
836 : : // Finalize the method list for one type.
837 : : void
838 : : finalize_methods_for_type(Type*);
839 : :
840 : : // Work out the types to use for unspecified variables and
841 : : // constants.
842 : : void
843 : : determine_types();
844 : :
845 : : // Type check the program.
846 : : void
847 : : check_types();
848 : :
849 : : // Check the types in a single block. This is used for complicated
850 : : // go statements.
851 : : void
852 : : check_types_in_block(Block*);
853 : :
854 : : // Check for return statements.
855 : : void
856 : : check_return_statements();
857 : :
858 : : // Gather references from global variables initializers to other
859 : : // variables.
860 : : void
861 : : record_global_init_refs();
862 : :
863 : : // Remove deadcode.
864 : : void
865 : : remove_deadcode();
866 : :
867 : : // Make implicit type conversions explicit.
868 : : void
869 : : add_conversions();
870 : :
871 : : // Make implicit type conversions explicit in a block.
872 : : void
873 : : add_conversions_in_block(Block*);
874 : :
875 : : // Analyze the program flow for escape information.
876 : : void
877 : : analyze_escape();
878 : :
879 : : // Discover the groups of possibly recursive functions in this package.
880 : : void
881 : : discover_analysis_sets();
882 : :
883 : : // Build a connectivity graph between the objects in each analyzed function.
884 : : void
885 : : assign_connectivity(Escape_context*, Named_object*);
886 : :
887 : : // Traverse the objects in the connecitivty graph from the sink, adjusting the
888 : : // escape levels of each object.
889 : : void
890 : : propagate_escape(Escape_context*, Node*);
891 : :
892 : : // Add notes about the escape level of a function's input and output
893 : : // parameters for exporting and importing top level functions.
894 : : void
895 : : tag_function(Named_object*);
896 : :
897 : : // Reclaim memory of escape analysis Nodes.
898 : : void
899 : : reclaim_escape_nodes();
900 : :
901 : : // Do all exports.
902 : : void
903 : : do_exports();
904 : :
905 : : // Add an import control function for an imported package to the
906 : : // list.
907 : : void
908 : : add_import_init_fn(const std::string& package_name,
909 : : const std::string& init_name, int prio);
910 : :
911 : : // Return the Import_init for a given init name.
912 : : Import_init*
913 : : lookup_init(const std::string& init_name);
914 : :
915 : : // Turn short-cut operators (&&, ||) into explicit if statements.
916 : : void
917 : : remove_shortcuts();
918 : :
919 : : // Turn short-cut operators into explicit if statements in a block.
920 : : void
921 : : remove_shortcuts_in_block(Block*);
922 : :
923 : : // Use temporary variables to force order of evaluation.
924 : : void
925 : : order_evaluations();
926 : :
927 : : // Order evaluations in a block.
928 : : void
929 : : order_block(Block*);
930 : :
931 : : // Add write barriers as needed.
932 : : void
933 : : add_write_barriers();
934 : :
935 : : // Return whether an assignment that sets LHS to RHS needs a write
936 : : // barrier.
937 : : bool
938 : : assign_needs_write_barrier(Expression* lhs,
939 : : Unordered_set(const Named_object*)*);
940 : :
941 : : // Return whether EXPR is the address of a variable that can be set
942 : : // without a write barrier. That is, if this returns true, then an
943 : : // assignment to *EXPR does not require a write barrier.
944 : : bool
945 : : is_nonwb_pointer(Expression* expr, Unordered_set(const Named_object*)*);
946 : :
947 : : // Return an assignment that sets LHS to RHS using a write barrier.
948 : : // This returns an if statement that checks whether write barriers
949 : : // are enabled. If not, it does LHS = RHS, otherwise it calls the
950 : : // appropriate write barrier function.
951 : : Statement*
952 : : assign_with_write_barrier(Function*, Block*, Statement_inserter*,
953 : : Expression* lhs, Expression* rhs, Location);
954 : :
955 : : // Return a statement that tests whether write barriers are enabled
956 : : // and executes either the efficient code (WITHOUT) or the write
957 : : // barrier function call (WITH), depending.
958 : : Statement*
959 : : check_write_barrier(Block*, Statement* without, Statement* with);
960 : :
961 : : // Flatten parse tree.
962 : : void
963 : : flatten();
964 : :
965 : : // Build thunks for functions which call recover.
966 : : void
967 : : build_recover_thunks();
968 : :
969 : : // Simplify statements which might use thunks: go and defer
970 : : // statements.
971 : : void
972 : : simplify_thunk_statements();
973 : :
974 : : // Dump AST if -fgo-dump-ast is set.
975 : : void
976 : : dump_ast(const char* basename);
977 : :
978 : : // Dump Call Graph if -fgo-dump-calls is set.
979 : : void
980 : : dump_call_graph(const char* basename);
981 : :
982 : : // Dump Connection Graphs if -fgo-dump-connections is set.
983 : : void
984 : : dump_connection_graphs(const char* basename);
985 : :
986 : : // Convert named types to the backend representation.
987 : : void
988 : : convert_named_types();
989 : :
990 : : // Convert named types in a list of bindings.
991 : : void
992 : : convert_named_types_in_bindings(Bindings*);
993 : :
994 : : // True if named types have been converted to the backend
995 : : // representation.
996 : : bool
997 : 26394216 : named_types_are_converted() const
998 : 26394216 : { return this->named_types_are_converted_; }
999 : :
1000 : : // Give an error if the initialization of VAR depends on itself.
1001 : : void
1002 : : check_self_dep(Named_object*);
1003 : :
1004 : : // Write out the global values.
1005 : : void
1006 : : write_globals();
1007 : :
1008 : : // Build required interface method tables.
1009 : : void
1010 : : build_interface_method_tables();
1011 : :
1012 : : // Return an expression which allocates memory to hold values of type TYPE.
1013 : : Expression*
1014 : : allocate_memory(Type *type, Location);
1015 : :
1016 : : // Get the backend name to use for an exported function, a method,
1017 : : // or a function/method declaration.
1018 : : void
1019 : : function_backend_name(const std::string& go_name, const Package*,
1020 : : const Type* receiver, Backend_name*);
1021 : :
1022 : : // Return the name to use for a function descriptor.
1023 : : void
1024 : : function_descriptor_backend_name(Named_object*, Backend_name*);
1025 : :
1026 : : // Return the name to use for a generated stub method.
1027 : : std::string
1028 : : stub_method_name(const Package*, const std::string& method_name);
1029 : :
1030 : : // Get the backend name of the hash function for TYPE.
1031 : : void
1032 : : hash_function_name(const Type*, Backend_name*);
1033 : :
1034 : : // Get the backend name of the equal function for TYPE.
1035 : : void
1036 : : equal_function_name(const Type*, const Named_type*, Backend_name*);
1037 : :
1038 : : // Get the backend name to use for a global variable.
1039 : : void
1040 : : global_var_backend_name(const std::string& go_name, const Package*,
1041 : : Backend_name*);
1042 : :
1043 : : // Return a name to use for an error case. This should only be used
1044 : : // after reporting an error, and is used to avoid useless knockon
1045 : : // errors.
1046 : : static std::string
1047 : : erroneous_name();
1048 : :
1049 : : // Return whether the name indicates an error.
1050 : : static bool
1051 : : is_erroneous_name(const std::string&);
1052 : :
1053 : : // Return a name to use for a thunk function. A thunk function is
1054 : : // one we create during the compilation, for a go statement or a
1055 : : // defer statement or a method expression.
1056 : : std::string
1057 : : thunk_name();
1058 : :
1059 : : // Return whether an object is a thunk.
1060 : : static bool
1061 : : is_thunk(const Named_object*);
1062 : :
1063 : : // Return the name to use for an init function.
1064 : : std::string
1065 : : init_function_name();
1066 : :
1067 : : // Return the name to use for a nested function.
1068 : : std::string
1069 : : nested_function_name(Named_object* enclosing);
1070 : :
1071 : : // Return the name to use for a sink funciton.
1072 : : std::string
1073 : : sink_function_name();
1074 : :
1075 : : // Return the name to use for an (erroneous) redefined function.
1076 : : std::string
1077 : : redefined_function_name();
1078 : :
1079 : : // Return the name for use for a recover thunk.
1080 : : std::string
1081 : : recover_thunk_name(const std::string& name, const Type* rtype);
1082 : :
1083 : : // Return the name to use for the GC root variable.
1084 : : std::string
1085 : : gc_root_name();
1086 : :
1087 : : // Return the name to use for a composite literal or string
1088 : : // initializer.
1089 : : std::string
1090 : : initializer_name();
1091 : :
1092 : : // Return the name of the variable used to represent the zero value
1093 : : // of a map.
1094 : : std::string
1095 : : map_zero_value_name();
1096 : :
1097 : : // Get the name of the magic initialization function.
1098 : : const std::string&
1099 : : get_init_fn_name();
1100 : :
1101 : : // Return the name for a dummy init function, which is not a real
1102 : : // function but only for tracking transitive import.
1103 : : std::string
1104 : : dummy_init_fn_name();
1105 : :
1106 : : // Return the package path symbol from an init function name, which
1107 : : // can be a real init function or a dummy one.
1108 : : std::string
1109 : : pkgpath_symbol_from_init_fn_name(std::string);
1110 : :
1111 : : // Get the backend name for a type descriptor symbol.
1112 : : void
1113 : : type_descriptor_backend_name(const Type*, Named_type*, Backend_name*);
1114 : :
1115 : : // Return the name of the type descriptor list symbol of a package.
1116 : : // The argument is an encoded pkgpath, as with pkgpath_symbol.
1117 : : std::string
1118 : : type_descriptor_list_symbol(const std::string&);
1119 : :
1120 : : // Return the name of the list of all type descriptor lists.
1121 : : std::string
1122 : : typelists_symbol();
1123 : :
1124 : : // Return the assembler name for the GC symbol for a type.
1125 : : std::string
1126 : : gc_symbol_name(Type*);
1127 : :
1128 : : // Return the assembler name for a ptrmask variable.
1129 : : std::string
1130 : : ptrmask_symbol_name(const std::string& ptrmask_sym_name);
1131 : :
1132 : : // Return the name to use for an interface method table.
1133 : : std::string
1134 : : interface_method_table_name(Interface_type*, Type*, bool is_pointer);
1135 : :
1136 : : // If NAME is a special name used as a Go identifier, return the
1137 : : // position within the string where the special part of the name
1138 : : // occurs.
1139 : : static size_t
1140 : : special_name_pos(const std::string& name);
1141 : :
1142 : : // Read a file into memory.
1143 : : static bool
1144 : : read_file(const char* filename, Location loc, std::string* data);
1145 : :
1146 : : private:
1147 : : // During parsing, we keep a stack of functions. Each function on
1148 : : // the stack is one that we are currently parsing. For each
1149 : : // function, we keep track of the current stack of blocks.
1150 : 568692 : struct Open_function
1151 : : {
1152 : : // The function.
1153 : : Named_object* function;
1154 : : // The stack of active blocks in the function.
1155 : : std::vector<Block*> blocks;
1156 : : };
1157 : :
1158 : : // The stack of functions.
1159 : : typedef std::vector<Open_function> Open_functions;
1160 : :
1161 : : // Set up the built-in unsafe package.
1162 : : void
1163 : : import_unsafe(const std::string&, bool is_exported, Location);
1164 : :
1165 : : // Return the current binding contour.
1166 : : Bindings*
1167 : : current_bindings();
1168 : :
1169 : : const Bindings*
1170 : : current_bindings() const;
1171 : :
1172 : : void
1173 : : write_c_header();
1174 : :
1175 : : // Get the decl for the magic initialization function.
1176 : : Named_object*
1177 : : initialization_function_decl();
1178 : :
1179 : : // Create the magic initialization function.
1180 : : Named_object*
1181 : : create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
1182 : :
1183 : : // Initialize imported packages. BFUNCTION is the function
1184 : : // into which the package init calls will be placed.
1185 : : void
1186 : : init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
1187 : :
1188 : : // Register variables with the garbage collector.
1189 : : void
1190 : : register_gc_vars(const std::vector<Named_object*>&,
1191 : : std::vector<Bstatement*>&,
1192 : : Bfunction* init_bfunction);
1193 : :
1194 : : // Build the list of type descriptors.
1195 : : void
1196 : : build_type_descriptor_list();
1197 : :
1198 : : // Register the type descriptors with the runtime.
1199 : : void
1200 : : register_type_descriptors(std::vector<Bstatement*>&,
1201 : : Bfunction* init_bfunction);
1202 : :
1203 : : void
1204 : : propagate_writebarrierrec();
1205 : :
1206 : : Named_object*
1207 : : write_barrier_variable();
1208 : :
1209 : : static bool
1210 : : is_digits(const std::string&);
1211 : :
1212 : : // Type used to map go:embed patterns to a list of files.
1213 : : typedef Unordered_map(std::string, std::vector<std::string>) Embed_patterns;
1214 : :
1215 : : // Type used to map go:embed file names to their full path.
1216 : : typedef Unordered_map(std::string, std::string) Embed_files;
1217 : :
1218 : : // Type used to map import names to packages.
1219 : : typedef std::map<std::string, Package*> Imports;
1220 : :
1221 : : // Type used to map package names to packages.
1222 : : typedef std::map<std::string, Package*> Packages;
1223 : :
1224 : : // Type used to map variables to the function calls that set them.
1225 : : // This is used for initialization dependency analysis.
1226 : : typedef std::map<Variable*, Named_object*> Var_deps;
1227 : :
1228 : : // Type used to map identifiers in the file block to the location
1229 : : // where they were defined.
1230 : : typedef Unordered_map(std::string, Location) File_block_names;
1231 : :
1232 : : // Type used to queue writing a type specific function.
1233 : 76 : struct Specific_type_function
1234 : : {
1235 : : enum Specific_type_function_kind { SPECIFIC_HASH, SPECIFIC_EQUAL };
1236 : :
1237 : : Type* type;
1238 : : Named_type* name;
1239 : : int64_t size;
1240 : : Specific_type_function_kind kind;
1241 : : Backend_name bname;
1242 : : Function_type* fntype;
1243 : :
1244 : 76 : Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
1245 : : Specific_type_function_kind akind,
1246 : : Backend_name* abname,
1247 : : Function_type* afntype)
1248 : 76 : : type(atype), name(aname), size(asize), kind(akind),
1249 : 76 : bname(*abname), fntype(afntype)
1250 : : { }
1251 : : };
1252 : :
1253 : : // Recompute init priorities.
1254 : : void
1255 : : recompute_init_priorities();
1256 : :
1257 : : // Recursive helper used by the routine above.
1258 : : void
1259 : : update_init_priority(Import_init* ii,
1260 : : std::set<const Import_init *>* visited);
1261 : :
1262 : : // The backend generator.
1263 : : Backend* backend_;
1264 : : // The object used to keep track of file names and line numbers.
1265 : : Linemap* linemap_;
1266 : : // The package we are compiling.
1267 : : Package* package_;
1268 : : // The list of currently open functions during parsing.
1269 : : Open_functions functions_;
1270 : : // The global binding contour. This includes the builtin functions
1271 : : // and the package we are compiling.
1272 : : Bindings* globals_;
1273 : : // The list of names we have seen in the file block.
1274 : : File_block_names file_block_names_;
1275 : : // Mapping from import file names to packages.
1276 : : Imports imports_;
1277 : : // Whether the magic unsafe package was imported.
1278 : : bool imported_unsafe_;
1279 : : // Whether the magic unsafe package was imported by the current file.
1280 : : bool current_file_imported_unsafe_;
1281 : : // Whether the embed package was imported by the current file.
1282 : : bool current_file_imported_embed_;
1283 : : // Mapping from package names we have seen to packages. This does
1284 : : // not include the package we are compiling.
1285 : : Packages packages_;
1286 : : // The functions named "init", if there are any.
1287 : : std::vector<Named_object*> init_functions_;
1288 : : // A mapping from variables to the function calls that initialize
1289 : : // them, if it is not stored in the variable's init or preinit.
1290 : : // This is used for dependency analysis.
1291 : : Var_deps var_deps_;
1292 : : // Whether we need a magic initialization function.
1293 : : bool need_init_fn_;
1294 : : // The name of the magic initialization function.
1295 : : std::string init_fn_name_;
1296 : : // A list of import control variables for packages that we import.
1297 : : Import_init_set imported_init_fns_;
1298 : : // The package path used for reflection data.
1299 : : std::string pkgpath_;
1300 : : // The package path to use for a symbol name.
1301 : : std::string pkgpath_symbol_;
1302 : : // The prefix to use for symbols, from the -fgo-prefix option.
1303 : : std::string prefix_;
1304 : : // Whether pkgpath_ has been set.
1305 : : bool pkgpath_set_;
1306 : : // Whether an explicit package path was set by -fgo-pkgpath.
1307 : : bool pkgpath_from_option_;
1308 : : // Whether an explicit prefix was set by -fgo-prefix.
1309 : : bool prefix_from_option_;
1310 : : // The relative import path, from the -fgo-relative-import-path
1311 : : // option.
1312 : : std::string relative_import_path_;
1313 : : // The C header file to write, from the -fgo-c-header option.
1314 : : std::string c_header_;
1315 : : // Mapping from imports in the source file to the real import paths.
1316 : : Unordered_map(std::string, std::string) import_map_;
1317 : : // Mapping from import paths to files to read.
1318 : : Unordered_map(std::string, std::string) package_file_;
1319 : : // Patterns from an embedcfg file.
1320 : : Embed_patterns embed_patterns_;
1321 : : // Mapping from file to full path from an embedcfg file.
1322 : : Embed_files embed_files_;
1323 : : // Whether or not to check for division by zero, from the
1324 : : // -fgo-check-divide-zero option.
1325 : : bool check_divide_by_zero_;
1326 : : // Whether or not to check for division overflow, from the
1327 : : // -fgo-check-divide-overflow option.
1328 : : bool check_divide_overflow_;
1329 : : // Whether we are compiling the runtime package, from the
1330 : : // -fgo-compiling-runtime option.
1331 : : bool compiling_runtime_;
1332 : : // The level of escape analysis debug information to emit, from the
1333 : : // -fgo-debug-escape option.
1334 : : int debug_escape_level_;
1335 : : // A hash value for debug escape analysis, from the
1336 : : // -fgo-debug-escape-hash option. The analysis is run only on
1337 : : // functions with names that hash to the matching value.
1338 : : std::string debug_escape_hash_;
1339 : : // Whether to output optimization diagnostics, from the
1340 : : // -fgo-debug-optimization option.
1341 : : bool debug_optimization_;
1342 : : // Nil-check size threshhold.
1343 : : int64_t nil_check_size_threshold_;
1344 : : // Whether runtime.eqtype calls are needed when comparing type
1345 : : // descriptors.
1346 : : bool need_eqtype_;
1347 : : // A list of types to verify.
1348 : : std::vector<Type*> verify_types_;
1349 : : // A list of interface types defined while parsing.
1350 : : std::vector<Interface_type*> interface_types_;
1351 : : // Type specific functions to write out.
1352 : : std::vector<Specific_type_function*> specific_type_functions_;
1353 : : // Whether we are done writing out specific type functions.
1354 : : bool specific_type_functions_are_written_;
1355 : : // Whether named types have been converted.
1356 : : bool named_types_are_converted_;
1357 : : // A list containing groups of possibly mutually recursive functions to be
1358 : : // considered during escape analysis.
1359 : : std::vector<Analysis_set> analysis_sets_;
1360 : : // A list of objects to add to the GC roots.
1361 : : std::vector<Expression*> gc_roots_;
1362 : : // A list of type descriptors that we need to register.
1363 : : std::vector<Type*> type_descriptors_;
1364 : : // A list of function declarations with imported bodies that we may
1365 : : // want to inline.
1366 : : std::vector<Named_object*> imported_inlinable_functions_;
1367 : : // A list of functions that we want to inline. These will be sent
1368 : : // to the backend.
1369 : : std::vector<Named_object*> imported_inline_functions_;
1370 : : };
1371 : :
1372 : : // A block of statements.
1373 : :
1374 : 75389 : class Block
1375 : : {
1376 : : public:
1377 : : Block(Block* enclosing, Location);
1378 : :
1379 : : // Return the enclosing block.
1380 : : const Block*
1381 : 36385 : enclosing() const
1382 : 36385 : { return this->enclosing_; }
1383 : :
1384 : : // Return the bindings of the block.
1385 : : Bindings*
1386 : 10926771 : bindings()
1387 : 9392130 : { return this->bindings_; }
1388 : :
1389 : : const Bindings*
1390 : 30753 : bindings() const
1391 : 14 : { return this->bindings_; }
1392 : :
1393 : : // Look at the block's statements.
1394 : : const std::vector<Statement*>*
1395 : 11959 : statements() const
1396 : 1827796 : { return &this->statements_; }
1397 : :
1398 : : // Return the start location. This is normally the location of the
1399 : : // left curly brace which starts the block.
1400 : : Location
1401 : 417411 : start_location() const
1402 : 416958 : { return this->start_location_; }
1403 : :
1404 : : // Return the end location. This is normally the location of the
1405 : : // right curly brace which ends the block.
1406 : : Location
1407 : 421951 : end_location() const
1408 : 387611 : { return this->end_location_; }
1409 : :
1410 : : // Add a statement to the block.
1411 : : void
1412 : : add_statement(Statement*);
1413 : :
1414 : : // Add a statement to the front of the block.
1415 : : void
1416 : : add_statement_at_front(Statement*);
1417 : :
1418 : : // Replace a statement in a block.
1419 : : void
1420 : : replace_statement(size_t index, Statement*);
1421 : :
1422 : : // Add a Statement before statement number INDEX.
1423 : : void
1424 : : insert_statement_before(size_t index, Statement*);
1425 : :
1426 : : // Add a Statement after statement number INDEX.
1427 : : void
1428 : : insert_statement_after(size_t index, Statement*);
1429 : :
1430 : : // Set the end location of the block.
1431 : : void
1432 : 1557621 : set_end_location(Location location)
1433 : 308155 : { this->end_location_ = location; }
1434 : :
1435 : : // Traverse the tree.
1436 : : int
1437 : : traverse(Traverse*);
1438 : :
1439 : : // Set final types for unspecified variables and constants.
1440 : : void
1441 : : determine_types(Gogo*);
1442 : :
1443 : : // Return true if execution of this block may fall through to the
1444 : : // next block.
1445 : : bool
1446 : : may_fall_through() const;
1447 : :
1448 : : // Write the export data for the block's statements to the string.
1449 : : void
1450 : : export_block(Export_function_body*);
1451 : :
1452 : : // Turn exported block data into a block.
1453 : : static bool
1454 : : import_block(Block*, Import_function_body*, Location);
1455 : :
1456 : : // Convert the block to the backend representation.
1457 : : Bblock*
1458 : : get_backend(Translate_context*);
1459 : :
1460 : : // Iterate over statements.
1461 : :
1462 : : typedef std::vector<Statement*>::iterator iterator;
1463 : :
1464 : : iterator
1465 : 44123 : begin()
1466 : 44123 : { return this->statements_.begin(); }
1467 : :
1468 : : iterator
1469 : 122751 : end()
1470 : 122751 : { return this->statements_.end(); }
1471 : :
1472 : : private:
1473 : : // Enclosing block.
1474 : : Block* enclosing_;
1475 : : // Statements in the block.
1476 : : std::vector<Statement*> statements_;
1477 : : // Binding contour.
1478 : : Bindings* bindings_;
1479 : : // Location of start of block.
1480 : : Location start_location_;
1481 : : // Location of end of block.
1482 : : Location end_location_;
1483 : : };
1484 : :
1485 : : // A function.
1486 : :
1487 : : class Function
1488 : : {
1489 : : public:
1490 : : Function(Function_type* type, Named_object*, Block*, Location);
1491 : :
1492 : : // Return the function's type.
1493 : : Function_type*
1494 : 16250411 : type() const
1495 : 16250411 : { return this->type_; }
1496 : :
1497 : : // Return the enclosing function if there is one.
1498 : : Named_object*
1499 : 1527048 : enclosing() const
1500 : 1527048 : { return this->enclosing_; }
1501 : :
1502 : : // Set the enclosing function. This is used when building thunks
1503 : : // for functions which call recover.
1504 : : void
1505 : 737 : set_enclosing(Named_object* enclosing)
1506 : : {
1507 : 737 : go_assert(this->enclosing_ == NULL);
1508 : 737 : this->enclosing_ = enclosing;
1509 : 737 : }
1510 : :
1511 : : // The result variables.
1512 : : typedef std::vector<Named_object*> Results;
1513 : :
1514 : : // Create the result variables in the outer block.
1515 : : void
1516 : : create_result_variables(Gogo*);
1517 : :
1518 : : // Update the named result variables when cloning a function which
1519 : : // calls recover.
1520 : : void
1521 : : update_result_variables();
1522 : :
1523 : : // Return the result variables.
1524 : : Results*
1525 : 1471575 : result_variables()
1526 : 1471575 : { return this->results_; }
1527 : :
1528 : : bool
1529 : 285951 : is_sink() const
1530 : 285951 : { return this->is_sink_; }
1531 : :
1532 : : void
1533 : 305 : set_is_sink()
1534 : 305 : { this->is_sink_ = true; }
1535 : :
1536 : : // Whether the result variables have names.
1537 : : bool
1538 : 26857 : results_are_named() const
1539 : 26857 : { return this->results_are_named_; }
1540 : :
1541 : : // Return the assembler name.
1542 : : const std::string&
1543 : 83051 : asm_name() const
1544 : 83051 : { return this->asm_name_; }
1545 : :
1546 : : // Set the assembler name.
1547 : : void
1548 : 93627 : set_asm_name(const std::string& asm_name)
1549 : 93627 : { this->asm_name_ = asm_name; }
1550 : :
1551 : : // Mark this symbol as exported by a linkname directive.
1552 : : void
1553 : 1673 : set_is_exported_by_linkname()
1554 : 1673 : { this->is_exported_by_linkname_ = true; }
1555 : :
1556 : : // Return the pragmas for this function.
1557 : : unsigned int
1558 : 1913550 : pragmas() const
1559 : 1913550 : { return this->pragmas_; }
1560 : :
1561 : : // Set the pragmas for this function.
1562 : : void
1563 : 5854 : set_pragmas(unsigned int pragmas)
1564 : : {
1565 : 5854 : this->pragmas_ = pragmas;
1566 : 2414 : }
1567 : :
1568 : : // Return the index to use for a nested function.
1569 : : unsigned int
1570 : 24308 : next_nested_function_index()
1571 : : {
1572 : 24308 : ++this->nested_functions_;
1573 : 24308 : return this->nested_functions_;
1574 : : }
1575 : :
1576 : : // Whether this method should not be included in the type
1577 : : // descriptor.
1578 : : bool
1579 : : nointerface() const;
1580 : :
1581 : : // Record that this method should not be included in the type
1582 : : // descriptor.
1583 : : void
1584 : : set_nointerface();
1585 : :
1586 : : // Record that this function is a stub method created for an unnamed
1587 : : // type.
1588 : : void
1589 : 10064 : set_is_unnamed_type_stub_method()
1590 : : {
1591 : 10064 : go_assert(this->is_method());
1592 : 10064 : this->is_unnamed_type_stub_method_ = true;
1593 : 10064 : }
1594 : :
1595 : : // Return the amount of enclosed variables in this closure.
1596 : : size_t
1597 : : closure_field_count() const
1598 : : { return this->closure_fields_.size(); }
1599 : :
1600 : : // Add a new field to the closure variable.
1601 : : void
1602 : 25473 : add_closure_field(Named_object* var, Location loc)
1603 : 25473 : { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1604 : :
1605 : : // Whether this function needs a closure.
1606 : : bool
1607 : 155320 : needs_closure() const
1608 : 155320 : { return !this->closure_fields_.empty(); }
1609 : :
1610 : : // Return the closure variable, creating it if necessary. This is
1611 : : // passed to the function as a static chain parameter.
1612 : : Named_object*
1613 : : closure_var();
1614 : :
1615 : : // Set the closure variable. This is used when building thunks for
1616 : : // functions which call recover.
1617 : : void
1618 : 1234 : set_closure_var(Named_object* v)
1619 : : {
1620 : 1234 : go_assert(this->closure_var_ == NULL);
1621 : 1234 : this->closure_var_ = v;
1622 : 1234 : }
1623 : :
1624 : : // Return the variable for a reference to field INDEX in the closure
1625 : : // variable.
1626 : : Named_object*
1627 : : enclosing_var(unsigned int index)
1628 : : {
1629 : : go_assert(index < this->closure_fields_.size());
1630 : : return closure_fields_[index].first;
1631 : : }
1632 : :
1633 : : // Set the type of the closure variable if there is one.
1634 : : void
1635 : : set_closure_type();
1636 : :
1637 : : // Get the block of statements associated with the function.
1638 : : Block*
1639 : 2732416 : block() const
1640 : 2732416 : { return this->block_; }
1641 : :
1642 : : // Get the location of the start of the function.
1643 : : Location
1644 : 741699 : location() const
1645 : 741699 : { return this->location_; }
1646 : :
1647 : : // Return whether this function is actually a method.
1648 : : bool
1649 : : is_method() const;
1650 : :
1651 : : // Add a label definition to the function.
1652 : : Label*
1653 : : add_label_definition(Gogo*, const std::string& label_name, Location);
1654 : :
1655 : : // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1656 : : // if we should report errors for a goto from the current location
1657 : : // to the label location.
1658 : : Label*
1659 : : add_label_reference(Gogo*, const std::string& label_name,
1660 : : Location, bool issue_goto_errors);
1661 : :
1662 : : // Warn about labels that are defined but not used.
1663 : : void
1664 : : check_labels() const;
1665 : :
1666 : : // Note that a new local type has been added. Return its index.
1667 : : unsigned int
1668 : 1295 : new_local_type_index()
1669 : 1295 : { return this->local_type_count_++; }
1670 : :
1671 : : // Whether this function calls the predeclared recover function.
1672 : : bool
1673 : 192825 : calls_recover() const
1674 : 192825 : { return this->calls_recover_; }
1675 : :
1676 : : // Record that this function calls the predeclared recover function.
1677 : : // This is set during the lowering pass.
1678 : : void
1679 : 1705 : set_calls_recover()
1680 : 1705 : { this->calls_recover_ = true; }
1681 : :
1682 : : // Whether this is a recover thunk function.
1683 : : bool
1684 : 1662 : is_recover_thunk() const
1685 : 1662 : { return this->is_recover_thunk_; }
1686 : :
1687 : : // Record that this is a thunk built for a function which calls
1688 : : // recover.
1689 : : void
1690 : 831 : set_is_recover_thunk()
1691 : 831 : { this->is_recover_thunk_ = true; }
1692 : :
1693 : : // Whether this function already has a recover thunk.
1694 : : bool
1695 : 1662 : has_recover_thunk() const
1696 : 1662 : { return this->has_recover_thunk_; }
1697 : :
1698 : : // Record that this function already has a recover thunk.
1699 : : void
1700 : 831 : set_has_recover_thunk()
1701 : 831 : { this->has_recover_thunk_ = true; }
1702 : :
1703 : : // Record that this function is a thunk created for a defer
1704 : : // statement that calls the __go_set_defer_retaddr runtime function.
1705 : : void
1706 : 8840 : set_calls_defer_retaddr()
1707 : 8840 : { this->calls_defer_retaddr_ = true; }
1708 : :
1709 : : // Whether this is a type hash or equality function created by the
1710 : : // compiler.
1711 : : bool
1712 : 0 : is_type_specific_function()
1713 : 0 : { return this->is_type_specific_function_; }
1714 : :
1715 : : // Record that this function is a type hash or equality function
1716 : : // created by the compiler.
1717 : : void
1718 : 92659 : set_is_type_specific_function()
1719 : 92659 : { this->is_type_specific_function_ = true; }
1720 : :
1721 : : // Mark the function as going into a unique section.
1722 : : void
1723 : 0 : set_in_unique_section()
1724 : 0 : { this->in_unique_section_ = true; }
1725 : :
1726 : : // Return whether this function should be exported for inlining.
1727 : : bool
1728 : 192754 : export_for_inlining() const
1729 : 192754 : { return this->export_for_inlining_; }
1730 : :
1731 : : // Mark the function to be exported for inlining.
1732 : : void
1733 : 157372 : set_export_for_inlining()
1734 : 157372 : { this->export_for_inlining_ = true; }
1735 : :
1736 : : // Return whether this function is inline only.
1737 : : bool
1738 : 12247 : is_inline_only() const
1739 : 12247 : { return this->is_inline_only_; }
1740 : :
1741 : : // Mark the function as inline only: the body should not be emitted
1742 : : // if it is not inlined.
1743 : : void
1744 : 12247 : set_is_inline_only()
1745 : 12247 : { this->is_inline_only_ = true; }
1746 : :
1747 : : // Report whether the function is referenced by an inline body.
1748 : : bool
1749 : 186751 : is_referenced_by_inline() const
1750 : 186751 : { return this->is_referenced_by_inline_; }
1751 : :
1752 : : // Mark the function as referenced by an inline body.
1753 : : void
1754 : 17560 : set_is_referenced_by_inline()
1755 : 17560 : { this->is_referenced_by_inline_ = true; }
1756 : :
1757 : : // Set the receiver type. This is used to remove aliases.
1758 : : void
1759 : : set_receiver_type(Type* rtype);
1760 : :
1761 : : // Swap with another function. Used only for the thunk which calls
1762 : : // recover.
1763 : : void
1764 : : swap_for_recover(Function *);
1765 : :
1766 : : // Traverse the tree.
1767 : : int
1768 : : traverse(Traverse*);
1769 : :
1770 : : // Determine types in the function.
1771 : : void
1772 : : determine_types(Gogo*);
1773 : :
1774 : : // Return an expression for the function descriptor, given the named
1775 : : // object for this function. This may only be called for functions
1776 : : // without a closure. This will be an immutable struct with one
1777 : : // field that points to the function's code.
1778 : : Expression*
1779 : : descriptor(Gogo*, Named_object*);
1780 : :
1781 : : // Set the descriptor for this function. This is used when a
1782 : : // function declaration is followed by a function definition.
1783 : : void
1784 : 0 : set_descriptor(Expression* descriptor)
1785 : : {
1786 : 0 : go_assert(this->descriptor_ == NULL);
1787 : 0 : this->descriptor_ = descriptor;
1788 : 0 : }
1789 : :
1790 : : // Return the backend representation.
1791 : : Bfunction*
1792 : : get_or_make_decl(Gogo*, Named_object*);
1793 : :
1794 : : // Return the function's decl after it has been built.
1795 : : Bfunction*
1796 : : get_decl() const;
1797 : :
1798 : : // Set the function decl to hold a backend representation of the function
1799 : : // code.
1800 : : void
1801 : : build(Gogo*, Named_object*);
1802 : :
1803 : : // Get the statement that assigns values to this function's result struct.
1804 : : Bstatement*
1805 : : return_value(Gogo*, Named_object*, Location) const;
1806 : :
1807 : : // Get the backend name of this function.
1808 : : void
1809 : : backend_name(Gogo*, Named_object*, Backend_name*);
1810 : :
1811 : : // Get an expression for the variable holding the defer stack.
1812 : : Expression*
1813 : : defer_stack(Location);
1814 : :
1815 : : // Export the function.
1816 : : void
1817 : : export_func(Export*, const Named_object*) const;
1818 : :
1819 : : // Export a function with a type.
1820 : : static void
1821 : : export_func_with_type(Export*, const Named_object*,
1822 : : const Function_type*, Results*, bool nointerface,
1823 : : const std::string& asm_name, Block* block, Location);
1824 : :
1825 : : // Import a function. Reports whether the import succeeded.
1826 : : static bool
1827 : : import_func(Import*, std::string* pname, Package** pkg,
1828 : : bool* is_exported, Typed_identifier** receiver,
1829 : : Typed_identifier_list** pparameters,
1830 : : Typed_identifier_list** presults, bool* is_varargs,
1831 : : bool* nointerface, std::string* asm_name, std::string* body);
1832 : :
1833 : : private:
1834 : : // Type for mapping from label names to Label objects.
1835 : : typedef Unordered_map(std::string, Label*) Labels;
1836 : :
1837 : : void
1838 : : build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1839 : :
1840 : : typedef std::vector<std::pair<Named_object*,
1841 : : Location> > Closure_fields;
1842 : :
1843 : : // The function's type.
1844 : : Function_type* type_;
1845 : : // The enclosing function. This is NULL when there isn't one, which
1846 : : // is the normal case.
1847 : : Named_object* enclosing_;
1848 : : // The result variables, if any.
1849 : : Results* results_;
1850 : : // If there is a closure, this is the list of variables which appear
1851 : : // in the closure. This is created by the parser, and then resolved
1852 : : // to a real type when we lower parse trees.
1853 : : Closure_fields closure_fields_;
1854 : : // The closure variable, passed as a parameter using the static
1855 : : // chain parameter. Normally NULL.
1856 : : Named_object* closure_var_;
1857 : : // The outer block of statements in the function.
1858 : : Block* block_;
1859 : : // The source location of the start of the function.
1860 : : Location location_;
1861 : : // Labels defined or referenced in the function.
1862 : : Labels labels_;
1863 : : // The number of local types defined in this function.
1864 : : unsigned int local_type_count_;
1865 : : // The assembler name: this is the name that will be put in the object file.
1866 : : // Set by the go:linkname compiler directive. This is normally empty.
1867 : : std::string asm_name_;
1868 : : // The function descriptor, if any.
1869 : : Expression* descriptor_;
1870 : : // The function decl.
1871 : : Bfunction* fndecl_;
1872 : : // The defer stack variable. A pointer to this variable is used to
1873 : : // distinguish the defer stack for one function from another. This
1874 : : // is NULL unless we actually need a defer stack.
1875 : : Temporary_statement* defer_stack_;
1876 : : // Pragmas for this function. This is a set of GOPRAGMA bits.
1877 : : unsigned int pragmas_;
1878 : : // Number of nested functions defined within this function.
1879 : : unsigned int nested_functions_;
1880 : : // True if this function is sink-named. No code is generated.
1881 : : bool is_sink_ : 1;
1882 : : // True if the result variables are named.
1883 : : bool results_are_named_ : 1;
1884 : : // True if this function is a stub method created for an unnamed
1885 : : // type.
1886 : : bool is_unnamed_type_stub_method_ : 1;
1887 : : // True if this function calls the predeclared recover function.
1888 : : bool calls_recover_ : 1;
1889 : : // True if this a thunk built for a function which calls recover.
1890 : : bool is_recover_thunk_ : 1;
1891 : : // True if this function already has a recover thunk.
1892 : : bool has_recover_thunk_ : 1;
1893 : : // True if this is a thunk built for a defer statement that calls
1894 : : // the __go_set_defer_retaddr runtime function.
1895 : : bool calls_defer_retaddr_ : 1;
1896 : : // True if this is a function built by the compiler to as a hash or
1897 : : // equality function for some type.
1898 : : bool is_type_specific_function_ : 1;
1899 : : // True if this function should be put in a unique section. This is
1900 : : // turned on for field tracking.
1901 : : bool in_unique_section_ : 1;
1902 : : // True if we should export the body of this function for
1903 : : // cross-package inlining.
1904 : : bool export_for_inlining_ : 1;
1905 : : // True if this function is inline only: if it should not be emitted
1906 : : // if it is not inlined.
1907 : : bool is_inline_only_ : 1;
1908 : : // True if this function is referenced from an inlined body that
1909 : : // will be put into the export data.
1910 : : bool is_referenced_by_inline_ : 1;
1911 : : // True if we should make this function visible to other packages
1912 : : // because of a go:linkname directive.
1913 : : bool is_exported_by_linkname_ : 1;
1914 : : };
1915 : :
1916 : : // A snapshot of the current binding state.
1917 : :
1918 : 1565 : class Bindings_snapshot
1919 : : {
1920 : : public:
1921 : : Bindings_snapshot(const Block*, Location);
1922 : :
1923 : : // Report any errors appropriate for a goto from the current binding
1924 : : // state of B to this one.
1925 : : void
1926 : : check_goto_from(const Block* b, Location);
1927 : :
1928 : : // Report any errors appropriate for a goto from this binding state
1929 : : // to the current state of B.
1930 : : void
1931 : : check_goto_to(const Block* b);
1932 : :
1933 : : private:
1934 : : bool
1935 : : check_goto_block(Location, const Block*, const Block*, size_t*);
1936 : :
1937 : : void
1938 : : check_goto_defs(Location, const Block*, size_t, size_t);
1939 : :
1940 : : // The current block.
1941 : : const Block* block_;
1942 : : // The number of names currently defined in each open block.
1943 : : // Element 0 is this->block_, element 1 is
1944 : : // this->block_->enclosing(), etc.
1945 : : std::vector<size_t> counts_;
1946 : : // The location where this snapshot was taken.
1947 : : Location location_;
1948 : : };
1949 : :
1950 : : // A function declaration.
1951 : :
1952 : : class Function_declaration
1953 : : {
1954 : : public:
1955 : 4153761 : Function_declaration(Function_type* fntype, Location location)
1956 : 4153761 : : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1957 : 4153761 : fndecl_(NULL), pragmas_(0), imported_body_(),
1958 : 4153761 : is_on_inlinable_list_(false)
1959 : : { }
1960 : :
1961 : : Function_type*
1962 : 47107614 : type() const
1963 : 47107614 : { return this->fntype_; }
1964 : :
1965 : : Location
1966 : 735555 : location() const
1967 : 735555 : { return this->location_; }
1968 : :
1969 : : // Return whether this function declaration is a method.
1970 : : bool
1971 : : is_method() const;
1972 : :
1973 : : const std::string&
1974 : : asm_name() const
1975 : : { return this->asm_name_; }
1976 : :
1977 : : // Set the assembler name.
1978 : : void
1979 : 747086 : set_asm_name(const std::string& asm_name)
1980 : 747086 : { this->asm_name_ = asm_name; }
1981 : :
1982 : : // Return the pragmas for this function.
1983 : : unsigned int
1984 : 1197927 : pragmas() const
1985 : 1197927 : { return this->pragmas_; }
1986 : :
1987 : : // Set the pragmas for this function.
1988 : : void
1989 : 1608 : set_pragmas(unsigned int pragmas)
1990 : : {
1991 : 1608 : this->pragmas_ = pragmas;
1992 : 1608 : }
1993 : :
1994 : : // Whether this method should not be included in the type
1995 : : // descriptor.
1996 : : bool
1997 : : nointerface() const;
1998 : :
1999 : : // Record that this method should not be included in the type
2000 : : // descriptor.
2001 : : void
2002 : : set_nointerface();
2003 : :
2004 : : // Whether we have an imported function body.
2005 : : bool
2006 : 1238048 : has_imported_body() const
2007 : 1238048 : { return !this->imported_body_.empty(); }
2008 : :
2009 : : // Record the imported body of this function.
2010 : : void
2011 : 492072 : set_imported_body(Import* imp, const std::string& imported_body)
2012 : : {
2013 : 492072 : this->imp_ = imp;
2014 : 492072 : this->imported_body_ = imported_body;
2015 : 492072 : }
2016 : :
2017 : : // Whether this declaration is on the list of inlinable functions.
2018 : : bool
2019 : 82289 : is_on_inlinable_list() const
2020 : 82289 : { return this->is_on_inlinable_list_; }
2021 : :
2022 : : // Set that this function is on the list of inlinable functions.
2023 : : void
2024 : 12247 : set_is_on_inlinable_list()
2025 : 12247 : { this->is_on_inlinable_list_ = true; }
2026 : :
2027 : : // Set the receiver type. This is used to remove aliases.
2028 : : void
2029 : : set_receiver_type(Type* rtype);
2030 : :
2031 : : // Import the function body, creating a function.
2032 : : void
2033 : : import_function_body(Gogo*, Named_object*);
2034 : :
2035 : : // Return an expression for the function descriptor, given the named
2036 : : // object for this function. This may only be called for functions
2037 : : // without a closure. This will be an immutable struct with one
2038 : : // field that points to the function's code.
2039 : : Expression*
2040 : : descriptor(Gogo*, Named_object*);
2041 : :
2042 : : // Return true if we have created a descriptor for this declaration.
2043 : : bool
2044 : 106670 : has_descriptor() const
2045 : 106670 : { return this->descriptor_ != NULL; }
2046 : :
2047 : : // Return a backend representation.
2048 : : Bfunction*
2049 : : get_or_make_decl(Gogo*, Named_object*);
2050 : :
2051 : : // If there is a descriptor, build it into the backend
2052 : : // representation.
2053 : : void
2054 : : build_backend_descriptor(Gogo*);
2055 : :
2056 : : // Get the backend name of this function declaration.
2057 : : void
2058 : : backend_name(Gogo*, Named_object*, Backend_name*);
2059 : :
2060 : : // Export a function declaration.
2061 : : void
2062 : 301334 : export_func(Export* exp, const Named_object* no) const
2063 : : {
2064 : 301334 : Function::export_func_with_type(exp, no, this->fntype_, NULL,
2065 : 301334 : this->is_method() && this->nointerface(),
2066 : 301334 : this->asm_name_, NULL, this->location_);
2067 : 301334 : }
2068 : :
2069 : : // Check that the types used in this declaration's signature are defined.
2070 : : void
2071 : : check_types() const;
2072 : :
2073 : : private:
2074 : : // The type of the function.
2075 : : Function_type* fntype_;
2076 : : // The location of the declaration.
2077 : : Location location_;
2078 : : // The assembler name: this is the name to use in references to the
2079 : : // function. This is normally empty.
2080 : : std::string asm_name_;
2081 : : // The function descriptor, if any.
2082 : : Expression* descriptor_;
2083 : : // The function decl if needed.
2084 : : Bfunction* fndecl_;
2085 : : // Pragmas for this function. This is a set of GOPRAGMA bits.
2086 : : unsigned int pragmas_;
2087 : : // Importer for function body if imported from a different package.
2088 : : Import* imp_;
2089 : : // Export data for function body if imported from a different package.
2090 : : std::string imported_body_;
2091 : : // Whether this declaration is already on the list of inlinable functions.
2092 : : bool is_on_inlinable_list_;
2093 : : };
2094 : :
2095 : : // A variable.
2096 : :
2097 : : class Variable
2098 : : {
2099 : : public:
2100 : : Variable(Type*, Expression*, bool is_global, bool is_parameter,
2101 : : bool is_receiver, Location);
2102 : :
2103 : : // Get the type of the variable.
2104 : : Type*
2105 : : type();
2106 : :
2107 : : Type*
2108 : : type() const;
2109 : :
2110 : : // Return whether the type is defined yet.
2111 : : bool
2112 : : has_type() const;
2113 : :
2114 : : // Get the initial value.
2115 : : Expression*
2116 : 5931152 : init() const
2117 : 5931152 : { return this->init_; }
2118 : :
2119 : : // Return whether there are any preinit statements.
2120 : : bool
2121 : 1780208 : has_pre_init() const
2122 : 1780208 : { return this->preinit_ != NULL; }
2123 : :
2124 : : // Return the preinit statements if any.
2125 : : Block*
2126 : 11992 : preinit() const
2127 : 11992 : { return this->preinit_; }
2128 : :
2129 : : // Return whether this is a global variable.
2130 : : bool
2131 : 12695630 : is_global() const
2132 : 12654918 : { return this->is_global_; }
2133 : :
2134 : : // Return whether this is a function parameter.
2135 : : bool
2136 : 16794814 : is_parameter() const
2137 : 16794814 : { return this->is_parameter_; }
2138 : :
2139 : : // Return whether this is a closure (static chain) parameter.
2140 : : bool
2141 : 1305045 : is_closure() const
2142 : 1305045 : { return this->is_closure_; }
2143 : :
2144 : : // Change this parameter to be a closure.
2145 : : void
2146 : 14291 : set_is_closure()
2147 : : {
2148 : 14291 : this->is_closure_ = true;
2149 : : }
2150 : :
2151 : : // Return whether this is the receiver parameter of a method.
2152 : : bool
2153 : 875973 : is_receiver() const
2154 : 875973 : { return this->is_receiver_; }
2155 : :
2156 : : // Change this parameter to be a receiver. This is used when
2157 : : // creating the thunks created for functions which call recover.
2158 : : void
2159 : 42 : set_is_receiver()
2160 : : {
2161 : 42 : go_assert(this->is_parameter_);
2162 : 42 : this->is_receiver_ = true;
2163 : 42 : }
2164 : :
2165 : : // Change this parameter to not be a receiver. This is used when
2166 : : // creating the thunks created for functions which call recover.
2167 : : void
2168 : 42 : set_is_not_receiver()
2169 : : {
2170 : 42 : go_assert(this->is_parameter_);
2171 : 42 : this->is_receiver_ = false;
2172 : 42 : }
2173 : :
2174 : : // Return whether this is the varargs parameter of a function.
2175 : : bool
2176 : 0 : is_varargs_parameter() const
2177 : 0 : { return this->is_varargs_parameter_; }
2178 : :
2179 : : // Return whether this is a global sink variable, created only to
2180 : : // run an initializer.
2181 : : bool
2182 : 25471 : is_global_sink() const
2183 : 25471 : { return this->is_global_sink_; }
2184 : :
2185 : : // Record that this is a global sink variable.
2186 : : void
2187 : 1114 : set_is_global_sink()
2188 : : {
2189 : 1114 : go_assert(this->is_global_);
2190 : 1114 : this->is_global_sink_ = true;
2191 : 1114 : }
2192 : :
2193 : : // Whether this variable's address is taken.
2194 : : bool
2195 : 0 : is_address_taken() const
2196 : 0 : { return this->is_address_taken_; }
2197 : :
2198 : : // Whether this variable should live in the heap.
2199 : : bool
2200 : 8023081 : is_in_heap() const
2201 : 8023081 : { return this->is_address_taken_ && !this->is_global_; }
2202 : :
2203 : : // Note that something takes the address of this variable.
2204 : : void
2205 : 354067 : set_address_taken()
2206 : 354067 : { this->is_address_taken_ = true; }
2207 : :
2208 : : // Return whether the address is taken but does not escape.
2209 : : bool
2210 : 424828 : is_non_escaping_address_taken() const
2211 : 424828 : { return this->is_non_escaping_address_taken_; }
2212 : :
2213 : : // Note that something takes the address of this variable such that
2214 : : // the address does not escape the function.
2215 : : void
2216 : 1100354 : set_non_escaping_address_taken()
2217 : 1100354 : { this->is_non_escaping_address_taken_ = true; }
2218 : :
2219 : : // Get the source location of the variable's declaration.
2220 : : Location
2221 : 1525110 : location() const
2222 : 1525110 : { return this->location_; }
2223 : :
2224 : : // Record that this is the varargs parameter of a function.
2225 : : void
2226 : 2599 : set_is_varargs_parameter()
2227 : : {
2228 : 2599 : go_assert(this->is_parameter_);
2229 : 2599 : this->is_varargs_parameter_ = true;
2230 : 2599 : }
2231 : :
2232 : : // Return whether the variable has been used.
2233 : : bool
2234 : 986750 : is_used() const
2235 : 986750 : { return this->is_used_; }
2236 : :
2237 : : // Mark that the variable has been used.
2238 : : void
2239 : 2959331 : set_is_used()
2240 : 2959331 : { this->is_used_ = true; }
2241 : :
2242 : : // Clear the initial value; used for error handling and write barriers.
2243 : : void
2244 : 18249 : clear_init()
2245 : 18249 : { this->init_ = NULL; }
2246 : :
2247 : : // Set the initial value; used for converting shortcuts.
2248 : : void
2249 : 83287 : set_init(Expression* init)
2250 : 83287 : { this->init_ = init; }
2251 : :
2252 : : // Get the preinit block, a block of statements to be run before the
2253 : : // initialization expression.
2254 : : Block*
2255 : : preinit_block(Gogo*);
2256 : :
2257 : : // Add a statement to be run before the initialization expression.
2258 : : // This is only used for global variables.
2259 : : void
2260 : : add_preinit_statement(Gogo*, Statement*);
2261 : :
2262 : : // Lower the initialization expression after parsing is complete.
2263 : : void
2264 : : lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
2265 : :
2266 : : // Flatten the initialization expression after ordering evaluations.
2267 : : void
2268 : : flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
2269 : :
2270 : : // A special case: the init value is used only to determine the
2271 : : // type. This is used if the variable is defined using := with the
2272 : : // comma-ok form of a map index or a receive expression. The init
2273 : : // value is actually the map index expression or receive expression.
2274 : : // We use this because we may not know the right type at parse time.
2275 : : void
2276 : 1594 : set_type_from_init_tuple()
2277 : 1594 : { this->type_from_init_tuple_ = true; }
2278 : :
2279 : : // Another special case: the init value is used only to determine
2280 : : // the type. This is used if the variable is defined using := with
2281 : : // a range clause. The init value is the range expression. The
2282 : : // type of the variable is the index type of the range expression
2283 : : // (i.e., the first value returned by a range).
2284 : : void
2285 : 9240 : set_type_from_range_index()
2286 : 9240 : { this->type_from_range_index_ = true; }
2287 : :
2288 : : // Another special case: like set_type_from_range_index, but the
2289 : : // type is the value type of the range expression (i.e., the second
2290 : : // value returned by a range).
2291 : : void
2292 : 22169 : set_type_from_range_value()
2293 : 22169 : { this->type_from_range_value_ = true; }
2294 : :
2295 : : // Another special case: the init value is used only to determine
2296 : : // the type. This is used if the variable is defined using := with
2297 : : // a case in a select statement. The init value is the channel.
2298 : : // The type of the variable is the channel's element type.
2299 : : void
2300 : 524 : set_type_from_chan_element()
2301 : 524 : { this->type_from_chan_element_ = true; }
2302 : :
2303 : : // After we lower the select statement, we once again set the type
2304 : : // from the initialization expression.
2305 : : void
2306 : 524 : clear_type_from_chan_element()
2307 : : {
2308 : 524 : go_assert(this->type_from_chan_element_);
2309 : 524 : this->type_from_chan_element_ = false;
2310 : 524 : }
2311 : :
2312 : : // TRUE if this variable was created for a type switch clause.
2313 : : bool
2314 : : is_type_switch_var() const
2315 : : { return this->is_type_switch_var_; }
2316 : :
2317 : : // Note that this variable was created for a type switch clause.
2318 : : void
2319 : 9915 : set_is_type_switch_var()
2320 : 9915 : { this->is_type_switch_var_ = true; }
2321 : :
2322 : : // Mark the variable as going into a unique section.
2323 : : void
2324 : 0 : set_in_unique_section()
2325 : : {
2326 : 0 : go_assert(this->is_global_);
2327 : 0 : this->in_unique_section_ = true;
2328 : 0 : }
2329 : :
2330 : : // Mark the variable as referenced by an inline body.
2331 : : void
2332 : 20936 : set_is_referenced_by_inline()
2333 : : {
2334 : 20936 : go_assert(this->is_global_);
2335 : 20936 : this->is_referenced_by_inline_ = true;
2336 : 20936 : }
2337 : :
2338 : : // Attach any go:embed comments for this variable.
2339 : : void
2340 : 22 : set_embeds(std::vector<std::string>* embeds)
2341 : : {
2342 : 22 : go_assert(this->is_global_
2343 : : && this->init_ == NULL
2344 : : && this->preinit_ == NULL);
2345 : 22 : this->embeds_ = embeds;
2346 : 22 : }
2347 : :
2348 : : // Return the top-level declaration for this variable.
2349 : : Statement*
2350 : 26703 : toplevel_decl()
2351 : 26703 : { return this->toplevel_decl_; }
2352 : :
2353 : : // Set the top-level declaration for this variable. Only used for local
2354 : : // variables
2355 : : void
2356 : 6520 : set_toplevel_decl(Statement* s)
2357 : : {
2358 : 6520 : go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_);
2359 : 6520 : this->toplevel_decl_ = s;
2360 : 6520 : }
2361 : :
2362 : : // Note that the initializer of this global variable refers to VAR.
2363 : : void
2364 : : add_init_ref(Named_object* var);
2365 : :
2366 : : // The variables that this variable's initializers refer to.
2367 : : const std::vector<Named_object*>*
2368 : 20058 : init_refs() const
2369 : 20058 : { return this->init_refs_; }
2370 : :
2371 : : // Traverse the initializer expression.
2372 : : int
2373 : : traverse_expression(Traverse*, unsigned int traverse_mask);
2374 : :
2375 : : // Determine the type of the variable if necessary.
2376 : : void
2377 : : determine_type(Gogo*);
2378 : :
2379 : : // Get the backend representation of the variable.
2380 : : Bvariable*
2381 : : get_backend_variable(Gogo*, Named_object*, const Package*,
2382 : : const std::string&);
2383 : :
2384 : : // Get the initial value of the variable. This may only
2385 : : // be called if has_pre_init() returns false.
2386 : : Bexpression*
2387 : : get_init(Gogo*, Named_object* function);
2388 : :
2389 : : // Return a series of statements which sets the value of the
2390 : : // variable in DECL. This should only be called is has_pre_init()
2391 : : // returns true. DECL may be NULL for a sink variable.
2392 : : Bstatement*
2393 : : get_init_block(Gogo*, Named_object* function, Bvariable* decl);
2394 : :
2395 : : // Export the variable.
2396 : : void
2397 : : export_var(Export*, const Named_object*) const;
2398 : :
2399 : : // Import a variable. Reports whether the import succeeded.
2400 : : static bool
2401 : : import_var(Import*, std::string* pname, Package** pkg, bool* is_exported,
2402 : : Type** ptype);
2403 : :
2404 : : private:
2405 : : // The type of a tuple.
2406 : : Type*
2407 : : type_from_tuple(Expression*, bool) const;
2408 : :
2409 : : // The type of a range.
2410 : : Type*
2411 : : type_from_range(Expression*, bool, bool) const;
2412 : :
2413 : : // The element type of a channel.
2414 : : Type*
2415 : : type_from_chan_element(Expression*, bool) const;
2416 : :
2417 : : // The variable's type. This may be NULL if the type is set from
2418 : : // the expression.
2419 : : Type* type_;
2420 : : // The initial value. This may be NULL if the variable should be
2421 : : // initialized to the default value for the type.
2422 : : Expression* init_;
2423 : : // Statements to run before the init statement.
2424 : : Block* preinit_;
2425 : : // Location of variable definition.
2426 : : Location location_;
2427 : : // The top-level declaration for this variable. Only used for local
2428 : : // variables. Must be a Temporary_statement if not NULL.
2429 : : Statement* toplevel_decl_;
2430 : : // Variables that the initializer of a global variable refers to.
2431 : : // Used for initializer ordering.
2432 : : std::vector<Named_object*>* init_refs_;
2433 : : // Any associated go:embed comments.
2434 : : std::vector<std::string>* embeds_;
2435 : : // Backend representation.
2436 : : Bvariable* backend_;
2437 : : // Whether this is a global variable.
2438 : : bool is_global_ : 1;
2439 : : // Whether this is a function parameter.
2440 : : bool is_parameter_ : 1;
2441 : : // Whether this is a closure parameter.
2442 : : bool is_closure_ : 1;
2443 : : // Whether this is the receiver parameter of a method.
2444 : : bool is_receiver_ : 1;
2445 : : // Whether this is the varargs parameter of a function.
2446 : : bool is_varargs_parameter_ : 1;
2447 : : // Whether this is a global sink variable created to run an
2448 : : // initializer.
2449 : : bool is_global_sink_ : 1;
2450 : : // Whether this variable is ever referenced.
2451 : : bool is_used_ : 1;
2452 : : // Whether something takes the address of this variable. For a
2453 : : // local variable this implies that the variable has to be on the
2454 : : // heap if it escapes from its function.
2455 : : bool is_address_taken_ : 1;
2456 : : // Whether something takes the address of this variable such that
2457 : : // the address does not escape the function.
2458 : : bool is_non_escaping_address_taken_ : 1;
2459 : : // True if we have seen this variable in a traversal.
2460 : : bool seen_ : 1;
2461 : : // True if we have lowered the initialization expression.
2462 : : bool init_is_lowered_ : 1;
2463 : : // True if we have flattened the initialization expression.
2464 : : bool init_is_flattened_ : 1;
2465 : : // True if init is a tuple used to set the type.
2466 : : bool type_from_init_tuple_ : 1;
2467 : : // True if init is a range clause and the type is the index type.
2468 : : bool type_from_range_index_ : 1;
2469 : : // True if init is a range clause and the type is the value type.
2470 : : bool type_from_range_value_ : 1;
2471 : : // True if init is a channel and the type is the channel's element type.
2472 : : bool type_from_chan_element_ : 1;
2473 : : // True if this is a variable created for a type switch case.
2474 : : bool is_type_switch_var_ : 1;
2475 : : // True if we have determined types.
2476 : : bool determined_type_ : 1;
2477 : : // True if this variable should be put in a unique section. This is
2478 : : // used for field tracking.
2479 : : bool in_unique_section_ : 1;
2480 : : // True if this variable is referenced from an inlined body that
2481 : : // will be put into the export data.
2482 : : bool is_referenced_by_inline_ : 1;
2483 : : };
2484 : :
2485 : : // A variable which is really the name for a function return value, or
2486 : : // part of one.
2487 : :
2488 : : class Result_variable
2489 : : {
2490 : : public:
2491 : 244746 : Result_variable(Type* type, Function* function, int index,
2492 : : Location location)
2493 : 244746 : : type_(type), function_(function), index_(index), location_(location),
2494 : 244746 : backend_(NULL), is_address_taken_(false),
2495 : 244746 : is_non_escaping_address_taken_(false)
2496 : : { }
2497 : :
2498 : : // Get the type of the result variable.
2499 : : Type*
2500 : 11496899 : type() const
2501 : 11496899 : { return this->type_; }
2502 : :
2503 : : // Get the function that this is associated with.
2504 : : Function*
2505 : : function() const
2506 : : { return this->function_; }
2507 : :
2508 : : // Index in the list of function results.
2509 : : int
2510 : 29476 : index() const
2511 : 29476 : { return this->index_; }
2512 : :
2513 : : // The location of the variable definition.
2514 : : Location
2515 : 244589 : location() const
2516 : 244589 : { return this->location_; }
2517 : :
2518 : : // Whether this variable's address is taken.
2519 : : bool
2520 : 0 : is_address_taken() const
2521 : 0 : { return this->is_address_taken_; }
2522 : :
2523 : : // Note that something takes the address of this variable.
2524 : : void
2525 : 346 : set_address_taken()
2526 : 346 : { this->is_address_taken_ = true; }
2527 : :
2528 : : // Return whether the address is taken but does not escape.
2529 : : bool
2530 : 0 : is_non_escaping_address_taken() const
2531 : 0 : { return this->is_non_escaping_address_taken_; }
2532 : :
2533 : : // Note that something takes the address of this variable such that
2534 : : // the address does not escape the function.
2535 : : void
2536 : 3086 : set_non_escaping_address_taken()
2537 : 3086 : { this->is_non_escaping_address_taken_ = true; }
2538 : :
2539 : : // Whether this variable should live in the heap.
2540 : : bool
2541 : 1613245 : is_in_heap() const
2542 : 1613245 : { return this->is_address_taken_; }
2543 : :
2544 : : // Set the function. This is used when cloning functions which call
2545 : : // recover.
2546 : : void
2547 : 6 : set_function(Function* function)
2548 : 6 : { this->function_ = function; }
2549 : :
2550 : : // Get the backend representation of the variable.
2551 : : Bvariable*
2552 : : get_backend_variable(Gogo*, Named_object*, const std::string&);
2553 : :
2554 : : private:
2555 : : // Type of result variable.
2556 : : Type* type_;
2557 : : // Function with which this is associated.
2558 : : Function* function_;
2559 : : // Index in list of results.
2560 : : int index_;
2561 : : // Where the result variable is defined.
2562 : : Location location_;
2563 : : // Backend representation.
2564 : : Bvariable* backend_;
2565 : : // Whether something takes the address of this variable.
2566 : : bool is_address_taken_;
2567 : : // Whether something takes the address of this variable such that
2568 : : // the address does not escape the function.
2569 : : bool is_non_escaping_address_taken_;
2570 : : };
2571 : :
2572 : : // The value we keep for a named constant. This lets us hold a type
2573 : : // and an expression.
2574 : :
2575 : : class Named_constant
2576 : : {
2577 : : public:
2578 : 1013689 : Named_constant(Type* type, Expression* expr, int iota_value,
2579 : : Location location)
2580 : 1013689 : : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2581 : 1013689 : lowering_(false), is_sink_(false), type_is_determined_(false),
2582 : 1013689 : bconst_(NULL)
2583 : : { }
2584 : :
2585 : : Type*
2586 : 16056766 : type() const
2587 : 16056766 : { return this->type_; }
2588 : :
2589 : : void
2590 : : set_type(Type* t);
2591 : :
2592 : : Expression*
2593 : 5008133 : expr() const
2594 : 5008133 : { return this->expr_; }
2595 : :
2596 : : int
2597 : : iota_value() const
2598 : : { return this->iota_value_; }
2599 : :
2600 : : Location
2601 : 35969 : location() const
2602 : 35969 : { return this->location_; }
2603 : :
2604 : : // Whether we are lowering.
2605 : : bool
2606 : 2195390 : lowering() const
2607 : 2195390 : { return this->lowering_; }
2608 : :
2609 : : // Set that we are lowering.
2610 : : void
2611 : 2195390 : set_lowering()
2612 : 2195390 : { this->lowering_ = true; }
2613 : :
2614 : : // We are no longer lowering.
2615 : : void
2616 : 2195390 : clear_lowering()
2617 : 2195390 : { this->lowering_ = false; }
2618 : :
2619 : : bool
2620 : 167979 : is_sink() const
2621 : 167979 : { return this->is_sink_; }
2622 : :
2623 : : void
2624 : 1209 : set_is_sink()
2625 : 1209 : { this->is_sink_ = true; }
2626 : :
2627 : : // Traverse the expression.
2628 : : int
2629 : : traverse_expression(Traverse*);
2630 : :
2631 : : // Determine the type of the constant if necessary.
2632 : : void
2633 : : determine_type(Gogo*);
2634 : :
2635 : : // Indicate that we found and reported an error for this constant.
2636 : : void
2637 : : set_error();
2638 : :
2639 : : // Export the constant.
2640 : : void
2641 : : export_const(Export*, const std::string& name) const;
2642 : :
2643 : : // Import a constant.
2644 : : static void
2645 : : import_const(Import*, std::string*, Type**, Expression**);
2646 : :
2647 : : // Get the backend representation of the constant value.
2648 : : Bexpression*
2649 : : get_backend(Gogo*, Named_object*);
2650 : :
2651 : : private:
2652 : : // The type of the constant.
2653 : : Type* type_;
2654 : : // The expression for the constant.
2655 : : Expression* expr_;
2656 : : // If the predeclared constant iota is used in EXPR_, this is the
2657 : : // value it will have. We do this because at parse time we don't
2658 : : // know whether the name "iota" will refer to the predeclared
2659 : : // constant or to something else. We put in the right value in when
2660 : : // we lower.
2661 : : int iota_value_;
2662 : : // The location of the definition.
2663 : : Location location_;
2664 : : // Whether we are currently lowering this constant.
2665 : : bool lowering_;
2666 : : // Whether this constant is blank named and needs only type checking.
2667 : : bool is_sink_;
2668 : : // Whether we have determined the type of the constants.
2669 : : bool type_is_determined_;
2670 : : // The backend representation of the constant value.
2671 : : Bexpression* bconst_;
2672 : : };
2673 : :
2674 : : // A type declaration.
2675 : :
2676 : 622954 : class Type_declaration
2677 : : {
2678 : : public:
2679 : 623472 : Type_declaration(Location location)
2680 : 623472 : : location_(location), in_function_(NULL), in_function_index_(0),
2681 : 623472 : methods_(), issued_warning_(false)
2682 : : { }
2683 : :
2684 : : // Return the location.
2685 : : Location
2686 : 0 : location() const
2687 : 0 : { return this->location_; }
2688 : :
2689 : : // Return the function in which this type is declared. This will
2690 : : // return NULL for a type declared in global scope.
2691 : : Named_object*
2692 : 622954 : in_function(unsigned int* pindex)
2693 : : {
2694 : 622954 : *pindex = this->in_function_index_;
2695 : 622954 : return this->in_function_;
2696 : : }
2697 : :
2698 : : // Set the function in which this type is declared.
2699 : : void
2700 : 1295 : set_in_function(Named_object* f, unsigned int index)
2701 : : {
2702 : 1295 : this->in_function_ = f;
2703 : 1295 : this->in_function_index_ = index;
2704 : 1295 : }
2705 : :
2706 : : // Add a method to this type. This is used when methods are defined
2707 : : // before the type.
2708 : : Named_object*
2709 : : add_method(const std::string& name, Function* function);
2710 : :
2711 : : // Add a method declaration to this type.
2712 : : Named_object*
2713 : : add_method_declaration(const std::string& name, Package*,
2714 : : Function_type* type, Location location);
2715 : :
2716 : : // Add an already created object as a method.
2717 : : void
2718 : 1 : add_existing_method(Named_object* no)
2719 : 1 : { this->methods_.push_back(no); }
2720 : :
2721 : : // Return whether any methods were defined.
2722 : : bool
2723 : : has_methods() const;
2724 : :
2725 : : // Return the methods.
2726 : : const std::vector<Named_object*>*
2727 : : methods() const
2728 : 26 : { return &this->methods_; }
2729 : :
2730 : : // Define methods when the real type is known.
2731 : : void
2732 : : define_methods(Named_type*);
2733 : :
2734 : : // This is called if we are trying to use this type. It returns
2735 : : // true if we should issue a warning.
2736 : : bool
2737 : : using_type();
2738 : :
2739 : : private:
2740 : : // The location of the type declaration.
2741 : : Location location_;
2742 : : // If this type is declared in a function, a pointer back to the
2743 : : // function in which it is defined.
2744 : : Named_object* in_function_;
2745 : : // The index of this type in IN_FUNCTION_.
2746 : : unsigned int in_function_index_;
2747 : : // Methods defined before the type is defined.
2748 : : std::vector<Named_object*> methods_;
2749 : : // True if we have issued a warning about a use of this type
2750 : : // declaration when it is undefined.
2751 : : bool issued_warning_;
2752 : : };
2753 : :
2754 : : // An unknown object. These are created by the parser for forward
2755 : : // references to names which have not been seen before. In a correct
2756 : : // program, these will always point to a real definition by the end of
2757 : : // the parse. Because they point to another Named_object, these may
2758 : : // only be referenced by Unknown_expression objects.
2759 : :
2760 : : class Unknown_name
2761 : : {
2762 : : public:
2763 : 85542 : Unknown_name(Location location)
2764 : 85542 : : location_(location), real_named_object_(NULL)
2765 : : { }
2766 : :
2767 : : // Return the location where this name was first seen.
2768 : : Location
2769 : 533 : location() const
2770 : 533 : { return this->location_; }
2771 : :
2772 : : // Return the real named object that this points to, or NULL if it
2773 : : // was never resolved.
2774 : : Named_object*
2775 : 1856898216 : real_named_object() const
2776 : 1793776 : { return this->real_named_object_; }
2777 : :
2778 : : // Set the real named object that this points to.
2779 : : void
2780 : : set_real_named_object(Named_object* no);
2781 : :
2782 : : private:
2783 : : // The location where this name was first seen.
2784 : : Location location_;
2785 : : // The real named object when it is known.
2786 : : Named_object*
2787 : : real_named_object_;
2788 : : };
2789 : :
2790 : : // A named object named. This is the result of a declaration. We
2791 : : // don't use a superclass because they all have to be handled
2792 : : // differently.
2793 : :
2794 : : class Named_object
2795 : : {
2796 : : public:
2797 : : enum Classification
2798 : : {
2799 : : // An uninitialized Named_object. We should never see this.
2800 : : NAMED_OBJECT_UNINITIALIZED,
2801 : : // An erroneous name. This indicates a parse error, to avoid
2802 : : // later errors about undefined references.
2803 : : NAMED_OBJECT_ERRONEOUS,
2804 : : // An unknown name. This is used for forward references. In a
2805 : : // correct program, these will all be resolved by the end of the
2806 : : // parse.
2807 : : NAMED_OBJECT_UNKNOWN,
2808 : : // A const.
2809 : : NAMED_OBJECT_CONST,
2810 : : // A type.
2811 : : NAMED_OBJECT_TYPE,
2812 : : // A forward type declaration.
2813 : : NAMED_OBJECT_TYPE_DECLARATION,
2814 : : // A var.
2815 : : NAMED_OBJECT_VAR,
2816 : : // A result variable in a function.
2817 : : NAMED_OBJECT_RESULT_VAR,
2818 : : // The blank identifier--the special variable named _.
2819 : : NAMED_OBJECT_SINK,
2820 : : // A func.
2821 : : NAMED_OBJECT_FUNC,
2822 : : // A forward func declaration.
2823 : : NAMED_OBJECT_FUNC_DECLARATION,
2824 : : // A package.
2825 : : NAMED_OBJECT_PACKAGE
2826 : : };
2827 : :
2828 : : // Return the classification.
2829 : : Classification
2830 : 5127628 : classification() const
2831 : 5127628 : { return this->classification_; }
2832 : :
2833 : : // Classifiers.
2834 : :
2835 : : bool
2836 : 750265 : is_erroneous() const
2837 : 750264 : { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2838 : :
2839 : : bool
2840 : 2005288037 : is_unknown() const
2841 : 3308955 : { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2842 : :
2843 : : bool
2844 : 4092986 : is_const() const
2845 : 4092986 : { return this->classification_ == NAMED_OBJECT_CONST; }
2846 : :
2847 : : bool
2848 : 966788539 : is_type() const
2849 : 966788534 : { return this->classification_ == NAMED_OBJECT_TYPE; }
2850 : :
2851 : : bool
2852 : 9331017 : is_type_declaration() const
2853 : 9331017 : { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2854 : :
2855 : : bool
2856 : 151016051 : is_variable() const
2857 : 151016051 : { return this->classification_ == NAMED_OBJECT_VAR; }
2858 : :
2859 : : bool
2860 : 61281633 : is_result_variable() const
2861 : 61281633 : { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2862 : :
2863 : : bool
2864 : 4322736 : is_sink() const
2865 : 4322736 : { return this->classification_ == NAMED_OBJECT_SINK; }
2866 : :
2867 : : bool
2868 : 58049174 : is_function() const
2869 : 58049174 : { return this->classification_ == NAMED_OBJECT_FUNC; }
2870 : :
2871 : : bool
2872 : 57998821 : is_function_declaration() const
2873 : 57998821 : { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2874 : :
2875 : : bool
2876 : 6847316 : is_package() const
2877 : 6847316 : { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2878 : :
2879 : : // Creators.
2880 : :
2881 : : static Named_object*
2882 : 14 : make_erroneous_name(const std::string& name)
2883 : 14 : { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2884 : :
2885 : : static Named_object*
2886 : : make_unknown_name(const std::string& name, Location);
2887 : :
2888 : : static Named_object*
2889 : : make_constant(const Typed_identifier&, const Package*, Expression*,
2890 : : int iota_value);
2891 : :
2892 : : static Named_object*
2893 : : make_type(const std::string&, const Package*, Type*, Location);
2894 : :
2895 : : static Named_object*
2896 : : make_type_declaration(const std::string&, const Package*, Location);
2897 : :
2898 : : static Named_object*
2899 : : make_variable(const std::string&, const Package*, Variable*);
2900 : :
2901 : : static Named_object*
2902 : : make_result_variable(const std::string&, Result_variable*);
2903 : :
2904 : : static Named_object*
2905 : : make_sink();
2906 : :
2907 : : static Named_object*
2908 : : make_function(const std::string&, const Package*, Function*);
2909 : :
2910 : : static Named_object*
2911 : : make_function_declaration(const std::string&, const Package*, Function_type*,
2912 : : Location);
2913 : :
2914 : : static Named_object*
2915 : : make_package(const std::string& alias, Package* package);
2916 : :
2917 : : // Getters.
2918 : :
2919 : : Unknown_name*
2920 : 1856928015 : unknown_value()
2921 : : {
2922 : 1856928015 : go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2923 : 1856928015 : return this->u_.unknown_value;
2924 : : }
2925 : :
2926 : : const Unknown_name*
2927 : 548 : unknown_value() const
2928 : : {
2929 : 548 : go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2930 : 548 : return this->u_.unknown_value;
2931 : : }
2932 : :
2933 : : Named_constant*
2934 : 34515559 : const_value()
2935 : : {
2936 : 34515559 : go_assert(this->classification_ == NAMED_OBJECT_CONST);
2937 : 34515559 : return this->u_.const_value;
2938 : : }
2939 : :
2940 : : const Named_constant*
2941 : 91812 : const_value() const
2942 : : {
2943 : 91812 : go_assert(this->classification_ == NAMED_OBJECT_CONST);
2944 : 91812 : return this->u_.const_value;
2945 : : }
2946 : :
2947 : : Named_type*
2948 : 451146973 : type_value()
2949 : : {
2950 : 451146973 : go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2951 : 451146973 : return this->u_.type_value;
2952 : : }
2953 : :
2954 : : const Named_type*
2955 : 591144469 : type_value() const
2956 : : {
2957 : 591144469 : go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2958 : 591144469 : return this->u_.type_value;
2959 : : }
2960 : :
2961 : : Type_declaration*
2962 : 3870 : type_declaration_value()
2963 : : {
2964 : 3870 : go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2965 : 3870 : return this->u_.type_declaration;
2966 : : }
2967 : :
2968 : : const Type_declaration*
2969 : 0 : type_declaration_value() const
2970 : : {
2971 : 0 : go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2972 : 0 : return this->u_.type_declaration;
2973 : : }
2974 : :
2975 : : Variable*
2976 : 163234188 : var_value()
2977 : : {
2978 : 163234188 : go_assert(this->classification_ == NAMED_OBJECT_VAR);
2979 : 163234188 : return this->u_.var_value;
2980 : : }
2981 : :
2982 : : const Variable*
2983 : 1752031 : var_value() const
2984 : : {
2985 : 1752031 : go_assert(this->classification_ == NAMED_OBJECT_VAR);
2986 : 1752031 : return this->u_.var_value;
2987 : : }
2988 : :
2989 : : Result_variable*
2990 : 12896800 : result_var_value()
2991 : : {
2992 : 12896800 : go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2993 : 12896800 : return this->u_.result_var_value;
2994 : : }
2995 : :
2996 : : const Result_variable*
2997 : 252079 : result_var_value() const
2998 : : {
2999 : 252079 : go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
3000 : 252079 : return this->u_.result_var_value;
3001 : : }
3002 : :
3003 : : Function*
3004 : 41515090 : func_value()
3005 : : {
3006 : 41515090 : go_assert(this->classification_ == NAMED_OBJECT_FUNC);
3007 : 41515090 : return this->u_.func_value;
3008 : : }
3009 : :
3010 : : const Function*
3011 : 627734 : func_value() const
3012 : : {
3013 : 627734 : go_assert(this->classification_ == NAMED_OBJECT_FUNC);
3014 : 627734 : return this->u_.func_value;
3015 : : }
3016 : :
3017 : : Function_declaration*
3018 : 51910188 : func_declaration_value()
3019 : : {
3020 : 51910188 : go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
3021 : 51910188 : return this->u_.func_declaration_value;
3022 : : }
3023 : :
3024 : : const Function_declaration*
3025 : 2730329 : func_declaration_value() const
3026 : : {
3027 : 2730329 : go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
3028 : 2730329 : return this->u_.func_declaration_value;
3029 : : }
3030 : :
3031 : : Package*
3032 : 378192 : package_value()
3033 : : {
3034 : 378192 : go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
3035 : 378192 : return this->u_.package_value;
3036 : : }
3037 : :
3038 : : const Package*
3039 : 39426 : package_value() const
3040 : : {
3041 : 39426 : go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
3042 : 39426 : return this->u_.package_value;
3043 : : }
3044 : :
3045 : : const std::string&
3046 : 39488263 : name() const
3047 : 36211105 : { return this->name_; }
3048 : :
3049 : : // Return the name to use in an error message. The difference is
3050 : : // that if this Named_object is defined in a different package, this
3051 : : // will return PACKAGE.NAME.
3052 : : std::string
3053 : : message_name() const;
3054 : :
3055 : : const Package*
3056 : 20473528 : package() const
3057 : 19798359 : { return this->package_; }
3058 : :
3059 : : // Resolve an unknown value if possible. This returns the same
3060 : : // Named_object or a new one.
3061 : : Named_object*
3062 : 2001979082 : resolve()
3063 : : {
3064 : 2001979082 : Named_object* ret = this;
3065 : 2001979082 : if (this->is_unknown())
3066 : : {
3067 : 1855104440 : Named_object* r = this->unknown_value()->real_named_object();
3068 : 1855104440 : if (r != NULL)
3069 : 2001979082 : ret = r;
3070 : : }
3071 : 2001979082 : return ret;
3072 : : }
3073 : :
3074 : : const Named_object*
3075 : : resolve() const
3076 : : {
3077 : : const Named_object* ret = this;
3078 : : if (this->is_unknown())
3079 : : {
3080 : : const Named_object* r = this->unknown_value()->real_named_object();
3081 : : if (r != NULL)
3082 : : ret = r;
3083 : : }
3084 : : return ret;
3085 : : }
3086 : :
3087 : : // The location where this object was defined or referenced.
3088 : : Location
3089 : : location() const;
3090 : :
3091 : : // Traverse a Named_object.
3092 : : int
3093 : : traverse(Traverse*, bool is_global);
3094 : :
3095 : : // Convert a variable to the backend representation.
3096 : : Bvariable*
3097 : : get_backend_variable(Gogo*, Named_object* function);
3098 : :
3099 : : // Get the backend representation of this object.
3100 : : void
3101 : : get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
3102 : : std::vector<Bfunction*>&);
3103 : :
3104 : : // Define a type declaration.
3105 : : void
3106 : : set_type_value(Named_type*);
3107 : :
3108 : : // Define a function declaration.
3109 : : void
3110 : : set_function_value(Function*);
3111 : :
3112 : : // Declare an unknown name as a type declaration.
3113 : : void
3114 : : declare_as_type();
3115 : :
3116 : : // Export this object.
3117 : : void
3118 : : export_named_object(Export*) const;
3119 : :
3120 : : // Mark this named object as an invalid redefinition of another object.
3121 : : void
3122 : 28 : set_is_redefinition()
3123 : 28 : { this->is_redefinition_ = true; }
3124 : :
3125 : : // Return whether or not this object is a invalid redefinition of another
3126 : : // object.
3127 : : bool
3128 : 3884285 : is_redefinition() const
3129 : 3884285 : { return this->is_redefinition_; }
3130 : :
3131 : : private:
3132 : : Named_object(const std::string&, const Package*, Classification);
3133 : :
3134 : : // The name of the object.
3135 : : std::string name_;
3136 : : // The package that this object is in. This is NULL if it is in the
3137 : : // file we are compiling.
3138 : : const Package* package_;
3139 : : // The type of object this is.
3140 : : Classification classification_;
3141 : : // The real data.
3142 : : union
3143 : : {
3144 : : Unknown_name* unknown_value;
3145 : : Named_constant* const_value;
3146 : : Named_type* type_value;
3147 : : Type_declaration* type_declaration;
3148 : : Variable* var_value;
3149 : : Result_variable* result_var_value;
3150 : : Function* func_value;
3151 : : Function_declaration* func_declaration_value;
3152 : : Package* package_value;
3153 : : } u_;
3154 : : // True if this object is an invalid redefinition of another object.
3155 : : bool is_redefinition_;
3156 : : };
3157 : :
3158 : : // A binding contour. This binds names to objects.
3159 : :
3160 : 5 : class Bindings
3161 : : {
3162 : : public:
3163 : : // Type for mapping from names to objects.
3164 : : typedef Unordered_map(std::string, Named_object*) Contour;
3165 : :
3166 : : Bindings(Bindings* enclosing);
3167 : :
3168 : : // Add an erroneous name.
3169 : : Named_object*
3170 : 14 : add_erroneous_name(const std::string& name)
3171 : 14 : { return this->add_named_object(Named_object::make_erroneous_name(name)); }
3172 : :
3173 : : // Add an unknown name.
3174 : : Named_object*
3175 : 85542 : add_unknown_name(const std::string& name, Location location)
3176 : : {
3177 : 85542 : return this->add_named_object(Named_object::make_unknown_name(name,
3178 : 85542 : location));
3179 : : }
3180 : :
3181 : : // Add a constant.
3182 : : Named_object*
3183 : 1013689 : add_constant(const Typed_identifier& tid, const Package* package,
3184 : : Expression* expr, int iota_value)
3185 : : {
3186 : 1013689 : return this->add_named_object(Named_object::make_constant(tid, package,
3187 : : expr,
3188 : 1013689 : iota_value));
3189 : : }
3190 : :
3191 : : // Add a type.
3192 : : Named_object*
3193 : 589952 : add_type(const std::string& name, const Package* package, Type* type,
3194 : : Location location)
3195 : : {
3196 : 589952 : return this->add_named_object(Named_object::make_type(name, package, type,
3197 : 589952 : location));
3198 : : }
3199 : :
3200 : : // Add a named type. This is used for builtin types, and to add an
3201 : : // imported type to the global scope.
3202 : : Named_object*
3203 : : add_named_type(Named_type* named_type);
3204 : :
3205 : : // Add a type declaration.
3206 : : Named_object*
3207 : 618825 : add_type_declaration(const std::string& name, const Package* package,
3208 : : Location location)
3209 : : {
3210 : 618825 : Named_object* no = Named_object::make_type_declaration(name, package,
3211 : : location);
3212 : 1237650 : return this->add_named_object(no);
3213 : : }
3214 : :
3215 : : // Add a variable.
3216 : : Named_object*
3217 : 1210269 : add_variable(const std::string& name, const Package* package,
3218 : : Variable* variable)
3219 : : {
3220 : 1210269 : return this->add_named_object(Named_object::make_variable(name, package,
3221 : 1210269 : variable));
3222 : : }
3223 : :
3224 : : // Add a result variable.
3225 : : Named_object*
3226 : 244750 : add_result_variable(const std::string& name, Result_variable* result)
3227 : : {
3228 : 244750 : return this->add_named_object(Named_object::make_result_variable(name,
3229 : 244750 : result));
3230 : : }
3231 : :
3232 : : // Add a function.
3233 : : Named_object*
3234 : : add_function(const std::string& name, const Package*, Function* function);
3235 : :
3236 : : // Add a function declaration.
3237 : : Named_object*
3238 : : add_function_declaration(const std::string& name, const Package* package,
3239 : : Function_type* type, Location location);
3240 : :
3241 : : // Add a package. The location is the location of the import
3242 : : // statement.
3243 : : Named_object*
3244 : 39417 : add_package(const std::string& alias, Package* package)
3245 : : {
3246 : 39417 : Named_object* no = Named_object::make_package(alias, package);
3247 : 78834 : return this->add_named_object(no);
3248 : : }
3249 : :
3250 : : // Define a type which was already declared.
3251 : : void
3252 : : define_type(Named_object*, Named_type*);
3253 : :
3254 : : // Add a method to the list of objects. This is not added to the
3255 : : // lookup table.
3256 : : void
3257 : : add_method(Named_object*);
3258 : :
3259 : : // Add a named object to this binding.
3260 : : Named_object*
3261 : 7475964 : add_named_object(Named_object* no)
3262 : 7475664 : { return this->add_named_object_to_contour(&this->bindings_, no); }
3263 : :
3264 : : // Clear all names in file scope from the bindings.
3265 : : void
3266 : : clear_file_scope(Gogo*);
3267 : :
3268 : : // Look up a name in this binding contour and in any enclosing
3269 : : // binding contours. This returns NULL if the name is not found.
3270 : : Named_object*
3271 : : lookup(const std::string&) const;
3272 : :
3273 : : // Look up a name in this binding contour without looking in any
3274 : : // enclosing binding contours. Returns NULL if the name is not found.
3275 : : Named_object*
3276 : : lookup_local(const std::string&) const;
3277 : :
3278 : : // Remove a name.
3279 : : void
3280 : : remove_binding(Named_object*);
3281 : :
3282 : : // Mark all variables as used. This is used for some types of parse
3283 : : // error.
3284 : : void
3285 : : mark_locals_used();
3286 : :
3287 : : // Traverse the tree. See the Traverse class.
3288 : : int
3289 : : traverse(Traverse*, bool is_global);
3290 : :
3291 : : // Determine types for the objects.
3292 : : void
3293 : : determine_types(Gogo*);
3294 : :
3295 : : // Iterate over definitions. This does not include things which
3296 : : // were only declared.
3297 : :
3298 : : typedef std::vector<Named_object*>::const_iterator
3299 : : const_definitions_iterator;
3300 : :
3301 : : const_definitions_iterator
3302 : 42520045 : begin_definitions() const
3303 : 42520045 : { return this->named_objects_.begin(); }
3304 : :
3305 : : const_definitions_iterator
3306 : 76136321 : end_definitions() const
3307 : 76136321 : { return this->named_objects_.end(); }
3308 : :
3309 : : // Return the number of definitions.
3310 : : size_t
3311 : 2671492 : size_definitions() const
3312 : 2671492 : { return this->named_objects_.size(); }
3313 : :
3314 : : // Return whether there are no definitions.
3315 : : bool
3316 : : empty_definitions() const
3317 : : { return this->named_objects_.empty(); }
3318 : :
3319 : : // Iterate over declarations. This is everything that has been
3320 : : // declared, which includes everything which has been defined.
3321 : :
3322 : : typedef Contour::const_iterator const_declarations_iterator;
3323 : :
3324 : : const_declarations_iterator
3325 : 2733486 : begin_declarations() const
3326 : 2728840 : { return this->bindings_.begin(); }
3327 : :
3328 : : const_declarations_iterator
3329 : 57183851 : end_declarations() const
3330 : 56844207 : { return this->bindings_.end(); }
3331 : :
3332 : : // Return the number of declarations.
3333 : : size_t
3334 : : size_declarations() const
3335 : : { return this->bindings_.size(); }
3336 : :
3337 : : // Return whether there are no declarations.
3338 : : bool
3339 : : empty_declarations() const
3340 : : { return this->bindings_.empty(); }
3341 : :
3342 : : // Return the first declaration.
3343 : : Named_object*
3344 : : first_declaration()
3345 : : { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
3346 : :
3347 : : // Dump to stderr for debugging
3348 : : void debug_dump();
3349 : :
3350 : : private:
3351 : : Named_object*
3352 : : add_named_object_to_contour(Contour*, Named_object*);
3353 : :
3354 : : Named_object*
3355 : : new_definition(Named_object*, Named_object*);
3356 : :
3357 : : // Enclosing bindings.
3358 : : Bindings* enclosing_;
3359 : : // The list of objects.
3360 : : std::vector<Named_object*> named_objects_;
3361 : : // The mapping from names to objects.
3362 : : Contour bindings_;
3363 : : };
3364 : :
3365 : : // A label.
3366 : :
3367 : : class Label
3368 : : {
3369 : : public:
3370 : 10530 : Label(const std::string& name)
3371 : 21060 : : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
3372 : 10530 : refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
3373 : 10530 : { }
3374 : :
3375 : : // Return the label's name.
3376 : : const std::string&
3377 : 1415 : name() const
3378 : 1415 : { return this->name_; }
3379 : :
3380 : : // Return whether the label has been defined.
3381 : : bool
3382 : 11498 : is_defined() const
3383 : 11498 : { return !Linemap::is_unknown_location(this->location_); }
3384 : :
3385 : : // Return whether the label has been used.
3386 : : bool
3387 : 1687 : is_used() const
3388 : 1687 : { return this->is_used_; }
3389 : :
3390 : : // Record that the label is used.
3391 : : void
3392 : 12310 : set_is_used()
3393 : 12310 : { this->is_used_ = true; }
3394 : :
3395 : : // Return whether this label is looping.
3396 : : bool
3397 : 1507 : looping() const
3398 : 1507 : { return this->depth_ == DEPTH_LOOPING; }
3399 : :
3400 : : // Set this label as looping.
3401 : : void
3402 : 229 : set_looping()
3403 : 229 : { this->depth_ = DEPTH_LOOPING; }
3404 : :
3405 : : // Return whether this label is nonlooping.
3406 : : bool
3407 : 1975 : nonlooping() const
3408 : 1975 : { return this->depth_ == DEPTH_NONLOOPING; }
3409 : :
3410 : : // Set this label as nonlooping.
3411 : : void
3412 : 1507 : set_nonlooping()
3413 : 1507 : { this->depth_ = DEPTH_NONLOOPING; }
3414 : :
3415 : : // Return the location of the definition.
3416 : : Location
3417 : 9 : location() const
3418 : 9 : { return this->location_; }
3419 : :
3420 : : // Return the bindings snapshot.
3421 : : Bindings_snapshot*
3422 : 2101 : snapshot() const
3423 : 2101 : { return this->snapshot_; }
3424 : :
3425 : : // Add a snapshot of a goto which refers to this label.
3426 : : void
3427 : 1568 : add_snapshot_ref(Bindings_snapshot* snapshot)
3428 : : {
3429 : 1568 : go_assert(Linemap::is_unknown_location(this->location_));
3430 : 1568 : this->refs_.push_back(snapshot);
3431 : 1568 : }
3432 : :
3433 : : // Return the list of snapshots of goto statements which refer to
3434 : : // this label.
3435 : : const std::vector<Bindings_snapshot*>&
3436 : : refs() const
3437 : 10524 : { return this->refs_; }
3438 : :
3439 : : // Clear the references.
3440 : : void
3441 : : clear_refs();
3442 : :
3443 : : // Define the label at LOCATION with the given bindings snapshot.
3444 : : void
3445 : 10524 : define(Location location, Bindings_snapshot* snapshot)
3446 : : {
3447 : 10524 : if (this->is_dummy_label())
3448 : : return;
3449 : 10521 : go_assert(Linemap::is_unknown_location(this->location_)
3450 : : && this->snapshot_ == NULL);
3451 : 10521 : this->location_ = location;
3452 : 10521 : this->snapshot_ = snapshot;
3453 : : }
3454 : :
3455 : : // Return the backend representation for this label.
3456 : : Blabel*
3457 : : get_backend_label(Translate_context*);
3458 : :
3459 : : // Return an expression for the address of this label. This is used
3460 : : // to get the return address of a deferred function to see whether
3461 : : // the function may call recover.
3462 : : Bexpression*
3463 : : get_addr(Translate_context*, Location location);
3464 : :
3465 : : // Return a dummy label, representing any instance of the blank label.
3466 : : static Label*
3467 : : create_dummy_label();
3468 : :
3469 : : // Return TRUE if this is a dummy label.
3470 : : bool
3471 : 20947 : is_dummy_label() const
3472 : 20947 : { return this->name_ == "_"; }
3473 : :
3474 : : // A classification of a label's looping depth.
3475 : : enum Loop_depth
3476 : : {
3477 : : DEPTH_UNKNOWN,
3478 : : // A label never jumped to.
3479 : : DEPTH_NONLOOPING,
3480 : : // A label jumped to.
3481 : : DEPTH_LOOPING
3482 : : };
3483 : :
3484 : : private:
3485 : : // The name of the label.
3486 : : std::string name_;
3487 : : // The location of the definition. This is 0 if the label has not
3488 : : // yet been defined.
3489 : : Location location_;
3490 : : // A snapshot of the set of bindings defined at this label, used to
3491 : : // issue errors about invalid goto statements.
3492 : : Bindings_snapshot* snapshot_;
3493 : : // A list of snapshots of goto statements which refer to this label.
3494 : : std::vector<Bindings_snapshot*> refs_;
3495 : : // Whether the label has been used.
3496 : : bool is_used_;
3497 : : // The backend representation.
3498 : : Blabel* blabel_;
3499 : : // The looping depth of this label, for escape analysis.
3500 : : Loop_depth depth_;
3501 : : };
3502 : :
3503 : : // An unnamed label. These are used when lowering loops.
3504 : :
3505 : : class Unnamed_label
3506 : : {
3507 : : public:
3508 : 175017 : Unnamed_label(Location location)
3509 : 175017 : : location_(location), derived_from_(NULL), blabel_(NULL)
3510 : : { }
3511 : :
3512 : : // Get the location where the label is defined.
3513 : : Location
3514 : 167230 : location() const
3515 : 167230 : { return this->location_; }
3516 : :
3517 : : // Set the location where the label is defined.
3518 : : void
3519 : 453 : set_location(Location location)
3520 : 453 : { this->location_ = location; }
3521 : :
3522 : : // Get the top level statement this unnamed label is derived from.
3523 : : Statement*
3524 : 0 : derived_from() const
3525 : 0 : { return this->derived_from_; }
3526 : :
3527 : : // Set the top level statement this unnamed label is derived from.
3528 : : void
3529 : 62186 : set_derived_from(Statement* s)
3530 : 62186 : { this->derived_from_ = s; }
3531 : :
3532 : : // Return a statement which defines this label.
3533 : : Bstatement*
3534 : : get_definition(Translate_context*);
3535 : :
3536 : : // Return a goto to this label from LOCATION.
3537 : : Bstatement*
3538 : : get_goto(Translate_context*, Location location);
3539 : :
3540 : : private:
3541 : : // Return the backend representation.
3542 : : Blabel*
3543 : : get_blabel(Translate_context*);
3544 : :
3545 : : // The location where the label is defined.
3546 : : Location location_;
3547 : : // The top-level statement this unnamed label was derived/lowered from.
3548 : : // This is NULL is this label is not the top-level of a lowered statement.
3549 : : Statement* derived_from_;
3550 : : // The backend representation of this label.
3551 : : Blabel* blabel_;
3552 : : };
3553 : :
3554 : : // An alias for an imported package.
3555 : :
3556 : : class Package_alias
3557 : : {
3558 : : public:
3559 : 39756 : Package_alias(Location location)
3560 : 39756 : : location_(location), used_(0)
3561 : : { }
3562 : :
3563 : : // The location of the import statement.
3564 : : Location
3565 : 13 : location()
3566 : 13 : { return this->location_; }
3567 : :
3568 : : // How many symbols from the package were used under this alias.
3569 : : size_t
3570 : 40058 : used() const
3571 : 40058 : { return this->used_; }
3572 : :
3573 : : // Note that some symbol was used under this alias.
3574 : : void
3575 : 322202 : note_usage()
3576 : 322202 : { this->used_++; }
3577 : :
3578 : : private:
3579 : : // The location of the import statement.
3580 : : Location location_;
3581 : : // The amount of times some name from this package was used under this alias.
3582 : : size_t used_;
3583 : : };
3584 : :
3585 : : // An imported package.
3586 : :
3587 : : class Package
3588 : : {
3589 : : public:
3590 : : Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3591 : : Location location);
3592 : :
3593 : : // Get the package path used for all symbols exported from this
3594 : : // package.
3595 : : const std::string&
3596 : 4150275 : pkgpath() const
3597 : 5812494 : { return this->pkgpath_; }
3598 : :
3599 : : // Return the package path to use for a symbol name.
3600 : : std::string
3601 : : pkgpath_symbol() const;
3602 : :
3603 : : // Set the package path symbol.
3604 : : void
3605 : : set_pkgpath_symbol(const std::string&);
3606 : :
3607 : : // Return the location of the most recent import statement.
3608 : : Location
3609 : 665868 : location() const
3610 : 665868 : { return this->location_; }
3611 : :
3612 : : // Return whether we know the name of this package yet.
3613 : : bool
3614 : 18104 : has_package_name() const
3615 : 18104 : { return !this->package_name_.empty(); }
3616 : :
3617 : : // The name that this package uses in its package clause. This may
3618 : : // be different from the name in the associated Named_object if the
3619 : : // import statement used an alias.
3620 : : const std::string&
3621 : 920734 : package_name() const
3622 : : {
3623 : 920734 : go_assert(!this->package_name_.empty());
3624 : 920734 : return this->package_name_;
3625 : : }
3626 : :
3627 : : // Return the bindings.
3628 : : Bindings*
3629 : 6282221 : bindings() const
3630 : 6617219 : { return this->bindings_; }
3631 : :
3632 : : // Type used to map import names to package aliases.
3633 : : typedef std::map<std::string, Package_alias*> Aliases;
3634 : :
3635 : : // Return the set of package aliases.
3636 : : const Aliases&
3637 : : aliases() const
3638 : 389161 : { return this->aliases_; }
3639 : :
3640 : : // Note that some symbol from this package was used and qualified by ALIAS.
3641 : : // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3642 : : void
3643 : : note_usage(const std::string& alias) const;
3644 : :
3645 : : // Note that USAGE might be a fake usage of this package.
3646 : : void
3647 : 377 : note_fake_usage(Expression* usage) const
3648 : 377 : { this->fake_uses_.insert(usage); }
3649 : :
3650 : : // Forget a given USAGE of this package.
3651 : : void
3652 : : forget_usage(Expression* usage) const;
3653 : :
3654 : : // Clear the used field for the next file.
3655 : : void
3656 : : clear_used();
3657 : :
3658 : : // Look up a name in the package. Returns NULL if the name is not
3659 : : // found.
3660 : : Named_object*
3661 : 296179 : lookup(const std::string& name) const
3662 : 296179 : { return this->bindings_->lookup(name); }
3663 : :
3664 : : // Set the name of the package.
3665 : : void
3666 : : set_package_name(const std::string& name, Location);
3667 : :
3668 : : // Set the location of the package. This is used to record the most
3669 : : // recent import location.
3670 : : void
3671 : 339159 : set_location(Location location)
3672 : 339159 : { this->location_ = location; }
3673 : :
3674 : : // Add a package name as an ALIAS for this package.
3675 : : Package_alias*
3676 : : add_alias(const std::string& alias, Location);
3677 : :
3678 : : // Add a constant to the package.
3679 : : Named_object*
3680 : 842111 : add_constant(const Typed_identifier& tid, Expression* expr)
3681 : 842111 : { return this->bindings_->add_constant(tid, this, expr, 0); }
3682 : :
3683 : : // Add a type to the package.
3684 : : Named_object*
3685 : 586329 : add_type(const std::string& name, Type* type, Location location)
3686 : 586329 : { return this->bindings_->add_type(name, this, type, location); }
3687 : :
3688 : : // Add a type declaration to the package.
3689 : : Named_object*
3690 : 586329 : add_type_declaration(const std::string& name, Location location)
3691 : 586329 : { return this->bindings_->add_type_declaration(name, this, location); }
3692 : :
3693 : : // Add a variable to the package.
3694 : : Named_object*
3695 : 326311 : add_variable(const std::string& name, Variable* variable)
3696 : 326311 : { return this->bindings_->add_variable(name, this, variable); }
3697 : :
3698 : : // Add a function declaration to the package.
3699 : : Named_object*
3700 : 1271385 : add_function_declaration(const std::string& name, Function_type* type,
3701 : : Location loc)
3702 : 1271385 : { return this->bindings_->add_function_declaration(name, this, type, loc); }
3703 : :
3704 : : // Determine types of constants.
3705 : : void
3706 : : determine_types(Gogo*);
3707 : :
3708 : : private:
3709 : : // The package path for type reflection data.
3710 : : std::string pkgpath_;
3711 : : // The package path for symbol names.
3712 : : std::string pkgpath_symbol_;
3713 : : // The name that this package uses in the package clause. This may
3714 : : // be the empty string if it is not yet known.
3715 : : std::string package_name_;
3716 : : // The names in this package.
3717 : : Bindings* bindings_;
3718 : : // The location of the most recent import statement.
3719 : : Location location_;
3720 : : // The set of aliases associated with this package.
3721 : : Aliases aliases_;
3722 : : // A set of possibly fake uses of this package. This is mutable because we
3723 : : // can track fake uses of a package even if we have a const pointer to it.
3724 : : mutable std::set<Expression*> fake_uses_;
3725 : : };
3726 : :
3727 : : // Return codes for the traversal functions. This is not an enum
3728 : : // because we want to be able to declare traversal functions in other
3729 : : // header files without including this one.
3730 : :
3731 : : // Continue traversal as usual.
3732 : : const int TRAVERSE_CONTINUE = -1;
3733 : :
3734 : : // Exit traversal.
3735 : : const int TRAVERSE_EXIT = 0;
3736 : :
3737 : : // Continue traversal, but skip components of the current object.
3738 : : // E.g., if this is returned by Traverse::statement, we do not
3739 : : // traverse the expressions in the statement even if
3740 : : // traverse_expressions is set in the traverse_mask.
3741 : : const int TRAVERSE_SKIP_COMPONENTS = 1;
3742 : :
3743 : : // This class is used when traversing the parse tree. The caller uses
3744 : : // a subclass which overrides functions as desired.
3745 : :
3746 : : class Traverse
3747 : : {
3748 : : public:
3749 : : // These bitmasks say what to traverse.
3750 : : static const unsigned int traverse_variables = 0x1;
3751 : : static const unsigned int traverse_constants = 0x2;
3752 : : static const unsigned int traverse_functions = 0x4;
3753 : : static const unsigned int traverse_blocks = 0x8;
3754 : : static const unsigned int traverse_statements = 0x10;
3755 : : static const unsigned int traverse_expressions = 0x20;
3756 : : static const unsigned int traverse_types = 0x40;
3757 : : static const unsigned int traverse_func_declarations = 0x80;
3758 : :
3759 : 17874673 : Traverse(unsigned int traverse_mask)
3760 : 15414721 : : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3761 : : { }
3762 : :
3763 : : virtual ~Traverse();
3764 : :
3765 : : // The bitmask of what to traverse.
3766 : : unsigned int
3767 : 3584691579 : traverse_mask() const
3768 : 3584691579 : { return this->traverse_mask_; }
3769 : :
3770 : : // Record that we are going to traverse a type. This returns true
3771 : : // if the type has already been seen in this traversal. This is
3772 : : // required because types, unlike expressions, can form a circular
3773 : : // graph.
3774 : : bool
3775 : : remember_type(const Type*);
3776 : :
3777 : : // Record that we are going to see an expression. This returns true
3778 : : // if the expression has already been seen in this traversal. This
3779 : : // is only needed for cases where multiple expressions can point to
3780 : : // a single one.
3781 : : bool
3782 : : remember_expression(const Expression*);
3783 : :
3784 : : // These functions return one of the TRAVERSE codes defined above.
3785 : :
3786 : : // If traverse_variables is set in the mask, this is called for
3787 : : // every variable in the tree.
3788 : : virtual int
3789 : : variable(Named_object*);
3790 : :
3791 : : // If traverse_constants is set in the mask, this is called for
3792 : : // every named constant in the tree. The bool parameter is true for
3793 : : // a global constant.
3794 : : virtual int
3795 : : constant(Named_object*, bool);
3796 : :
3797 : : // If traverse_functions is set in the mask, this is called for
3798 : : // every function in the tree.
3799 : : virtual int
3800 : : function(Named_object*);
3801 : :
3802 : : // If traverse_blocks is set in the mask, this is called for every
3803 : : // block in the tree.
3804 : : virtual int
3805 : : block(Block*);
3806 : :
3807 : : // If traverse_statements is set in the mask, this is called for
3808 : : // every statement in the tree.
3809 : : virtual int
3810 : : statement(Block*, size_t* index, Statement*);
3811 : :
3812 : : // If traverse_expressions is set in the mask, this is called for
3813 : : // every expression in the tree.
3814 : : virtual int
3815 : : expression(Expression**);
3816 : :
3817 : : // If traverse_types is set in the mask, this is called for every
3818 : : // type in the tree.
3819 : : virtual int
3820 : : type(Type*);
3821 : :
3822 : : // If traverse_func_declarations is set in the mask, this is called
3823 : : // for every function declarations in the tree.
3824 : : virtual int
3825 : : function_declaration(Named_object*);
3826 : :
3827 : : private:
3828 : : // A hash table for types we have seen during this traversal. Note
3829 : : // that this uses the default hash functions for pointers rather
3830 : : // than Type_hash_identical and Type_identical. This is because for
3831 : : // traversal we care about seeing a specific type structure. If
3832 : : // there are two separate instances of identical types, we want to
3833 : : // traverse both.
3834 : : typedef Unordered_set(const Type*) Types_seen;
3835 : :
3836 : : typedef Unordered_set(const Expression*) Expressions_seen;
3837 : :
3838 : : // Bitmask of what sort of objects to traverse.
3839 : : unsigned int traverse_mask_;
3840 : : // Types which have been seen in this traversal.
3841 : : Types_seen* types_seen_;
3842 : : // Expressions which have been seen in this traversal.
3843 : : Expressions_seen* expressions_seen_;
3844 : : };
3845 : :
3846 : : // This class looks for interface types to finalize methods of inherited
3847 : : // interfaces.
3848 : :
3849 : 27017 : class Finalize_methods : public Traverse
3850 : : {
3851 : : public:
3852 : 27017 : Finalize_methods(Gogo* gogo)
3853 : 27017 : : Traverse(traverse_types),
3854 : 27017 : gogo_(gogo)
3855 : : { }
3856 : :
3857 : : int
3858 : : type(Type*);
3859 : :
3860 : : private:
3861 : : Gogo* gogo_;
3862 : : };
3863 : :
3864 : : // A class which makes it easier to insert new statements before the
3865 : : // current statement during a traversal.
3866 : :
3867 : : class Statement_inserter
3868 : : {
3869 : : public:
3870 : : typedef Unordered_set(Statement*) Statements;
3871 : :
3872 : : // Empty constructor.
3873 : 2892967 : Statement_inserter()
3874 : 2892967 : : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL),
3875 : 2892967 : statements_added_(NULL)
3876 : : { }
3877 : :
3878 : : // Constructor for a statement in a block.
3879 : 10312956 : Statement_inserter(Block* block, size_t *pindex, Statements *added = NULL)
3880 : 72166 : : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL),
3881 : 72166 : statements_added_(added)
3882 : : { }
3883 : :
3884 : : // Constructor for a global variable.
3885 : 40712 : Statement_inserter(Gogo* gogo, Variable* var, Statements *added = NULL)
3886 : 40712 : : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var),
3887 : 40712 : statements_added_(added)
3888 : 40712 : { go_assert(var->is_global()); }
3889 : :
3890 : : // We use the default copy constructor and assignment operator.
3891 : :
3892 : : // Insert S before the statement we are traversing, or before the
3893 : : // initialization expression of a global variable.
3894 : : void
3895 : : insert(Statement* s);
3896 : :
3897 : : private:
3898 : : // The block that the statement is in.
3899 : : Block* block_;
3900 : : // The index of the statement that we are traversing.
3901 : : size_t* pindex_;
3902 : : // The IR, needed when looking at an initializer expression for a
3903 : : // global variable.
3904 : : Gogo* gogo_;
3905 : : // The global variable, when looking at an initializer expression.
3906 : : Variable* var_;
3907 : : // If non-null, a set to record new statements inserted (non-owned).
3908 : : Statements* statements_added_;
3909 : : };
3910 : :
3911 : : // When translating the gogo IR into the backend data structure, this
3912 : : // is the context we pass down the blocks and statements.
3913 : :
3914 : : class Translate_context
3915 : : {
3916 : : public:
3917 : 8079005 : Translate_context(Gogo* gogo, Named_object* function, Block* block,
3918 : : Bblock* bblock)
3919 : 8079005 : : gogo_(gogo), backend_(gogo->backend()), function_(function),
3920 : 7783676 : block_(block), bblock_(bblock), is_const_(false)
3921 : : { }
3922 : :
3923 : : // Accessors.
3924 : :
3925 : : Gogo*
3926 : 50004639 : gogo()
3927 : 45158177 : { return this->gogo_; }
3928 : :
3929 : : Backend*
3930 : 20868698 : backend()
3931 : 18237203 : { return this->backend_; }
3932 : :
3933 : : Named_object*
3934 : 16584497 : function()
3935 : 16202043 : { return this->function_; }
3936 : :
3937 : : Block*
3938 : 130060 : block()
3939 : 130060 : { return this->block_; }
3940 : :
3941 : : Bblock*
3942 : 4904691 : bblock()
3943 : 4904691 : { return this->bblock_; }
3944 : :
3945 : : bool
3946 : 287144 : is_const()
3947 : 287144 : { return this->is_const_; }
3948 : :
3949 : : // Make a constant context.
3950 : : void
3951 : 413456 : set_is_const()
3952 : 413456 : { this->is_const_ = true; }
3953 : :
3954 : : private:
3955 : : // The IR for the entire compilation unit.
3956 : : Gogo* gogo_;
3957 : : // The generator for the backend data structures.
3958 : : Backend* backend_;
3959 : : // The function we are currently translating. NULL if not in a
3960 : : // function, e.g., the initializer of a global variable.
3961 : : Named_object* function_;
3962 : : // The block we are currently translating. NULL if not in a
3963 : : // function.
3964 : : Block *block_;
3965 : : // The backend representation of the current block. NULL if block_
3966 : : // is NULL.
3967 : : Bblock* bblock_;
3968 : : // Whether this is being evaluated in a constant context. This is
3969 : : // used for type descriptor initializers.
3970 : : bool is_const_;
3971 : : };
3972 : :
3973 : : // This is used by some of the langhooks.
3974 : : extern Gogo* go_get_gogo();
3975 : :
3976 : : // Whether we have seen any errors. FIXME: Replace with a backend
3977 : : // interface.
3978 : : extern bool saw_errors();
3979 : :
3980 : : // For use in the debugger
3981 : : extern void debug_go_gogo(Gogo*);
3982 : : extern void debug_go_named_object(Named_object*);
3983 : : extern void debug_go_bindings(Bindings*);
3984 : :
3985 : :
3986 : : #endif // !defined(GO_GOGO_H)
|