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