1 // names.cc -- Names used by gofrontend generated code.
2
3 // Copyright 2017 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 #include "go-system.h"
8
9 #include "gogo.h"
10 #include "go-encode-id.h"
11 #include "types.h"
12 #include "expressions.h"
13
14 // This file contains functions that generate names that appear in the
15 // assembly code. This is not used for names that appear only in the
16 // debug info.
17
18 // Our external names contain only ASCII alphanumeric characters,
19 // underscore, and dot. (According to the GCC sources, dot is not
20 // permitted in assembler symbols on VxWorks and MMIX. We will not
21 // support those systems.) Go names can not contain dot, so we rely
22 // on using dot to encode Unicode characters, and to separate Go
23 // symbols by package, and so forth. We assume that none of the
24 // non-Go symbols in the final link will contain a dot, so we don't
25 // worry about conflicts.
26 //
27 // We first describe the basic symbol names, used to represent Go
28 // functions and variables. These never start with a dot, never end
29 // with a dot, never contain two consecutive dots, and never contain a
30 // dot followed by a digit.
31 //
32 // The external name for a normal Go symbol NAME, a function or
33 // variable, is simply "PKGPATH.NAME". Note that NAME is not the
34 // packed form used for the "hidden" name internally in the compiler;
35 // it is the name that appears in the source code. PKGPATH is the
36 // -fgo-pkgpath option as adjusted by Gogo::pkgpath_for_symbol. Note
37 // that PKGPATH can not contain a dot and neither can NAME. Also,
38 // NAME may not begin with a digit. NAME may require further encoding
39 // for non-ASCII characters as described below, but until that
40 // encoding these symbols contain exactly one dot, and they do not
41 // start with a dot.
42 //
43 // The external name for a method NAME for a named type TYPE is
44 // "PKGPATH.TYPE.NAME". Unlike the gc compiler, the external name
45 // does not indicate whether this is a pointer method or a value
46 // method; a named type can not have both a pointer and value method
47 // with the same name, so there is no ambiguity. PKGPATH is the
48 // package path of the package in which TYPE is defined. Here none of
49 // PKGPATH, TYPE, or NAME can be empty or contain a dot, and neither
50 // TYPE nor NAME may begin with a digit. Before encoding these names
51 // contain exactly two dots, not consecutive, and they do not start
52 // with a dot.
53 //
54 // It's uncommon, but the use of type literals with embedded fields
55 // can cause us to have methods on unnamed types. The external names
56 // for these are also PKGPATH.TYPE.NAME, where TYPE is an
57 // approximately readable version of the type literal, described
58 // below. As the type literal encoding always contains multiple dots,
59 // these names always contain more than two dots. Although the type
60 // literal encoding contains dots, neither PKGPATH nor NAME can
61 // contain a dot, and neither TYPE nor NAME can begin with a digit.
62 // The effect is that PKGPATH is always the portion of the name before
63 // the first dot and NAME is always the portion after the last dot.
64 // There is no ambiguity as long as encoded type literals are
65 // unambiguous.
66 //
67 // Also uncommon is an external name that must refer to a named type
68 // defined within a function. While such a type can not have methods
69 // itself, it can pick up embedded methods, and those methods need
70 // names. These are treated as a kind of type literal written as,
71 // before type literal encoding, FNNAME.TYPENAME(INDEX) or, for a
72 // method, TYPE.MNAME.TYPENAME(INDEX). INDEX is the index of that
73 // named type within the function, as a single function can have
74 // multiple types with the same name. This is unambiguous as
75 // parentheses can not appear in a type literal in this form (they can
76 // only appear in interface method declarations).
77 //
78 // That is the end of the list of basic names. The remaining names
79 // exist for special purposes, and are differentiated from the basic
80 // names by containing two consecutive dots.
81 //
82 // The hash function for a type is treated as a method whose name is
83 // ".hash". That is, the method name begins with a dot. The effect
84 // is that there will be two consecutive dots in the name; the name
85 // will always end with "..hash".
86 //
87 // Similarly the equality function for a type is treated as a method
88 // whose name is ".eq".
89 //
90 // The function descriptor for a function is the same as the name of
91 // the function with an added suffix "..f".
92 //
93 // A thunk for a go or defer statement is treated as a function whose
94 // name is ".thunkNN" where NN is a sequence of digits (these
95 // functions are never globally visible). Thus the final name of a
96 // thunk will be PKGPATH..thunkNN.
97 //
98 // An init function is treated as a function whose name is ".initNN"
99 // where NN is a sequence of digits (these functions are never
100 // globally visible). Thus the final name of an init function will be
101 // PKGPATH..initNN.
102 //
103 // A nested function is given the name of outermost enclosing function
104 // or method with an added suffix "..funcNN" where NN is a sequence of
105 // digits. Note that the function descriptor of a nested function, if
106 // needed, will end with "..funcNN..f".
107 //
108 // A recover thunk is the same as the name of the function with an
109 // added suffix "..r".
110 //
111 // The name of a type descriptor for a named type is PKGPATH.TYPE..d.
112 //
113 // The name of a type descriptor for an unnamed type is type..TYPE.
114 // That is, the string "type.." followed by the type literal encoding.
115 // These names are common symbols, in the linker's sense of the word
116 // common: in the final executable there is only one instance of the
117 // type descriptor for a given unnamed type. The type literal
118 // encoding can never start with a digit or with 'u' or 'U'.
119 //
120 // The name of the GC symbol for a named type is PKGPATH.TYPE..g.
121 //
122 // The name of the GC symbol for an unnamed type is typeg..TYPE.
123 // These are common symbols.
124 //
125 // The name of a ptrmask symbol is gcbits..B32 where B32 is an
126 // encoding of the ptrmask bits using only ASCII letters without 'u'
127 // or 'U'. These are common symbols.
128 //
129 // An interface method table for assigning the non-interface type TYPE
130 // to the interface type ITYPE is named imt..ITYPE..TYPE. If ITYPE or
131 // TYPE is a named type, they are written as PKGPATH.TYPE. Otherwise
132 // they are written as a type literal. An interface method table for
133 // a pointer method set uses pimt instead of imt.
134 //
135 // The names of composite literal initializers, including the GC root
136 // variable, are not referenced. They must not conflict with any C
137 // language names, but the names are otherwise unimportant. They are
138 // named "go..CNN" where NN is a sequence of digits. The names do not
139 // include the PKGPATH.
140 //
141 // The map zero value, a common symbol that represents the zero value
142 // of a map, is named simply "go..zerovalue". The name does not
143 // include the PKGPATH.
144 //
145 // The import function for the main package is referenced by C code,
146 // and is named __go_init_main. For other packages it is
147 // PKGPATH..import.
148 //
149 // The type literal encoding is essentially a single line version of
150 // the type literal, such as "struct { pkgpath.i int; J int }". In
151 // this representation unexported names use their pkgpath, exported
152 // names omit it.
153 //
154 // The type literal encoding is not quite valid Go, as some aspects of
155 // compiler generated types can not be represented. For example,
156 // incomparable struct types have an extra field "{x}". Struct tags
157 // are quoted inside curly braces, rather than introduce an encoding
158 // for quotes. Struct tags can contain any character, so any single
159 // byte Unicode character that is not alphanumeric or underscore is
160 // replaced with .xNN where NN is the hex encoding.
161 //
162 // There is a simple encoding for glue characters in type literals:
163 // .0 - ' '
164 // .1 - '*'
165 // .2 - ';'
166 // .3 - ','
167 // .4 - '{'
168 // .5 - '}'
169 // .6 - '['
170 // .7 - ']'
171 // .8 - '('
172 // .9 - ')'
173 // This is unambiguous as, although the type literal can contain a dot
174 // as shown above, those dots are always followed by a name and names
175 // can not begin with a digit. A dot is always followed by a name or
176 // a digit, and a type literal can neither start nor end with a dot,
177 // so this never introduces consecutive dots.
178 //
179 // Struct tags can contain any character, so they need special
180 // treatment. Alphanumerics, underscores, and Unicode characters that
181 // require more than a single byte are left alone (Unicode characters
182 // will be encoded later, as described below). Other single bytes
183 // characters are replace with .xNN where NN is the hex encoding.
184 //
185 // Since Go identifiers can contain Unicode characters, we must encode
186 // them into ASCII. We do this last, after the name is generated as
187 // described above and after type literals are encoded. To make the
188 // encoding unambiguous, we introduce it with two consecutive dots.
189 // This is followed by the letter u and four hex digits or the letter
190 // U and eight digits, just as in the language only using ..u and ..U
191 // instead of \u and \U. The compiler also produces identifiers that
192 // are qualified by package path, which means that there may also be ASCII
193 // characters that are not assembler-friendly (ex: '=', '/'). The encoding
194 // scheme translates such characters into the "..zNN" where NN is the
195 // hex value for the character. Since before this encoding names can never
196 // contain consecutive dots followed by 'z', 'u' or 'U', and after this
197 // encoding "..z", "..u" and "..U" are followed by a known number of
198 // characters, this is unambiguous.
199 //
200 // Demangling these names is straightforward:
201 // - replace ..zXX with an ASCII character
202 // - replace ..uXXXX with a unicode character
203 // - replace ..UXXXXXXXX with a unicode character
204 // - replace .D, where D is a digit, with the character from the above
205 // That will get you as close as possible to a readable name.
206
207 // Return the assembler name to use for an exported function, a
208 // method, or a function/method declaration. This is not called if
209 // the function has been given an explicit name via a magic //extern
210 // or //go:linkname comment. GO_NAME is the name that appears in the
211 // Go code. PACKAGE is the package where the function is defined, and
212 // is NULL for the package being compiled. For a method, RTYPE is
213 // the method's receiver type; for a function, RTYPE is NULL.
214
215 std::string
function_asm_name(const std::string & go_name,const Package * package,const Type * rtype)216 Gogo::function_asm_name(const std::string& go_name, const Package* package,
217 const Type* rtype)
218 {
219 std::string ret;
220 if (rtype != NULL)
221 ret = rtype->deref()->mangled_name(this);
222 else if (package == NULL)
223 ret = this->pkgpath();
224 else
225 ret = package->pkgpath();
226 ret.push_back('.');
227 // Check for special names that will break if we use
228 // Gogo::unpack_hidden_name.
229 if (Gogo::is_special_name(go_name))
230 ret.append(go_name);
231 else
232 ret.append(Gogo::unpack_hidden_name(go_name));
233 return go_encode_id(ret);
234 }
235
236 // Return the name to use for a function descriptor. These symbols
237 // are globally visible.
238
239 std::string
function_descriptor_name(Named_object * no)240 Gogo::function_descriptor_name(Named_object* no)
241 {
242 if (no->is_function() && !no->func_value()->asm_name().empty())
243 return no->func_value()->asm_name() + "..f";
244 else if (no->is_function_declaration()
245 && !no->func_declaration_value()->asm_name().empty())
246 return no->func_declaration_value()->asm_name() + "..f";
247 std::string ret = this->function_asm_name(no->name(), no->package(), NULL);
248 ret.append("..f");
249 return ret;
250 }
251
252 // Return the name to use for a generated stub method. MNAME is the
253 // method name. PACKAGE is the package where the type that needs this
254 // stub method is defined. These functions are globally visible.
255 // Note that this is the function name that corresponds to the name
256 // used for the method in Go source code, if this stub method were
257 // written in Go. The assembler name will be generated by
258 // Gogo::function_asm_name, and because this is a method that name
259 // will include the receiver type.
260
261 std::string
stub_method_name(const Package * package,const std::string & mname)262 Gogo::stub_method_name(const Package* package, const std::string& mname)
263 {
264 if (!Gogo::is_hidden_name(mname))
265 return mname + "..stub";
266
267 const std::string& ppkgpath(package == NULL
268 ? this->pkgpath()
269 : package->pkgpath());
270 std::string mpkgpath = Gogo::hidden_name_pkgpath(mname);
271 if (mpkgpath == ppkgpath)
272 return Gogo::unpack_hidden_name(mname) + "..stub";
273
274 // We are creating a stub method for an unexported method of an
275 // imported embedded type. We need to disambiguate the method name.
276 std::string ret = mpkgpath;
277 ret.push_back('.');
278 ret.append(Gogo::unpack_hidden_name(mname));
279 ret.append("..stub");
280 return ret;
281 }
282
283 // Return the names of the hash and equality functions for TYPE. If
284 // NAME is not NULL it is the name of the type. Set *HASH_NAME and
285 // *EQUAL_NAME.
286
287 void
specific_type_function_names(const Type * type,const Named_type * name,std::string * hash_name,std::string * equal_name)288 Gogo::specific_type_function_names(const Type* type, const Named_type* name,
289 std::string *hash_name,
290 std::string *equal_name)
291 {
292 const Type* rtype = type;
293 if (name != NULL)
294 rtype = name;
295 std::string tname = rtype->mangled_name(this);
296 *hash_name = tname + "..hash";
297 *equal_name = tname + "..eq";
298 }
299
300 // Return the assembler name to use for a global variable. GO_NAME is
301 // the name that appears in the Go code. PACKAGE is the package where
302 // the variable is defined, and is NULL for the package being
303 // compiled.
304
305 std::string
global_var_asm_name(const std::string & go_name,const Package * package)306 Gogo::global_var_asm_name(const std::string& go_name, const Package* package)
307 {
308 std::string ret;
309 if (package == NULL)
310 ret = this->pkgpath();
311 else
312 ret = package->pkgpath();
313 ret.append(1, '.');
314 ret.append(Gogo::unpack_hidden_name(go_name));
315 return go_encode_id(ret);
316 }
317
318 // Return an erroneous name that indicates that an error has already
319 // been reported.
320
321 std::string
erroneous_name()322 Gogo::erroneous_name()
323 {
324 go_assert(saw_errors());
325 static int erroneous_count;
326 char name[50];
327 snprintf(name, sizeof name, ".erroneous%d", erroneous_count);
328 ++erroneous_count;
329 return name;
330 }
331
332 // Return whether a name is an erroneous name.
333
334 bool
is_erroneous_name(const std::string & name)335 Gogo::is_erroneous_name(const std::string& name)
336 {
337 return name.compare(0, 10, ".erroneous") == 0;
338 }
339
340 // Return a name for a thunk object.
341
342 std::string
thunk_name()343 Gogo::thunk_name()
344 {
345 static int thunk_count;
346 char thunk_name[50];
347 snprintf(thunk_name, sizeof thunk_name, "..thunk%d", thunk_count);
348 ++thunk_count;
349 std::string ret = this->pkgpath();
350 return ret + thunk_name;
351 }
352
353 // Return whether a function is a thunk.
354
355 bool
is_thunk(const Named_object * no)356 Gogo::is_thunk(const Named_object* no)
357 {
358 const std::string& name(no->name());
359 size_t i = name.find("..thunk");
360 if (i == std::string::npos)
361 return false;
362 for (i += 7; i < name.size(); ++i)
363 if (name[i] < '0' || name[i] > '9')
364 return false;
365 return true;
366 }
367
368 // Return the name to use for an init function. There can be multiple
369 // functions named "init" so each one needs a different name.
370
371 std::string
init_function_name()372 Gogo::init_function_name()
373 {
374 static int init_count;
375 char buf[30];
376 snprintf(buf, sizeof buf, "..init%d", init_count);
377 ++init_count;
378 std::string ret = this->pkgpath();
379 return ret + buf;
380 }
381
382 // Return the name to use for a nested function.
383
384 std::string
nested_function_name(Named_object * enclosing)385 Gogo::nested_function_name(Named_object* enclosing)
386 {
387 std::string prefix;
388 unsigned int index;
389 if (enclosing == NULL)
390 {
391 // A function literal at top level, as in
392 // var f = func() {}
393 static unsigned int toplevel_index;
394 ++toplevel_index;
395 index = toplevel_index;
396 prefix = ".go";
397 }
398 else
399 {
400 while (true)
401 {
402 Named_object* parent = enclosing->func_value()->enclosing();
403 if (parent == NULL)
404 break;
405 enclosing = parent;
406 }
407 const Typed_identifier* rcvr =
408 enclosing->func_value()->type()->receiver();
409 if (rcvr != NULL)
410 {
411 prefix = rcvr->type()->mangled_name(this);
412 prefix.push_back('.');
413 }
414 prefix.append(Gogo::unpack_hidden_name(enclosing->name()));
415 index = enclosing->func_value()->next_nested_function_index();
416 }
417 char buf[30];
418 snprintf(buf, sizeof buf, "..func%u", index);
419 return prefix + buf;
420 }
421
422 // Return the name to use for a sink function, a function whose name
423 // is simply underscore. We don't really need these functions but we
424 // do have to generate them for error checking.
425
426 std::string
sink_function_name()427 Gogo::sink_function_name()
428 {
429 static int sink_count;
430 char buf[30];
431 snprintf(buf, sizeof buf, ".sink%d", sink_count);
432 ++sink_count;
433 return buf;
434 }
435
436 // Return the name to use for a redefined function. These functions
437 // are erroneous but we still generate them for further error
438 // checking.
439
440 std::string
redefined_function_name()441 Gogo::redefined_function_name()
442 {
443 static int redefinition_count;
444 char buf[30];
445 snprintf(buf, sizeof buf, ".redefined%d", redefinition_count);
446 ++redefinition_count;
447 return buf;
448 }
449
450 // Return the name to use for a recover thunk for the function NAME.
451 // If the function is a method, RTYPE is the receiver type.
452
453 std::string
recover_thunk_name(const std::string & name,const Type * rtype)454 Gogo::recover_thunk_name(const std::string& name, const Type* rtype)
455 {
456 std::string ret;
457 if (rtype != NULL)
458 {
459 ret = rtype->mangled_name(this);
460 ret.append(1, '.');
461 }
462 if (Gogo::is_special_name(name))
463 ret.append(name);
464 else
465 ret.append(Gogo::unpack_hidden_name(name));
466 ret.append("..r");
467 return ret;
468 }
469
470 // Return the name to use for a GC root variable. The GC root
471 // variable is a composite literal that is passed to
472 // runtime.registerGCRoots. There is at most one of these variables
473 // per compilation.
474
475 std::string
gc_root_name()476 Gogo::gc_root_name()
477 {
478 return "go..C0";
479 }
480
481 // Return the name to use for a composite literal or string
482 // initializer. This is a local name never referenced outside of this
483 // file.
484
485 std::string
initializer_name()486 Gogo::initializer_name()
487 {
488 static unsigned int counter;
489 char buf[30];
490 ++counter;
491 snprintf(buf, sizeof buf, "go..C%u", counter);
492 return buf;
493 }
494
495 // Return the name of the variable used to represent the zero value of
496 // a map. This is a globally visible common symbol.
497
498 std::string
map_zero_value_name()499 Gogo::map_zero_value_name()
500 {
501 return "go..zerovalue";
502 }
503
504 // Return the name to use for the import control function.
505
506 const std::string&
get_init_fn_name()507 Gogo::get_init_fn_name()
508 {
509 if (this->init_fn_name_.empty())
510 {
511 go_assert(this->package_ != NULL);
512 if (this->is_main_package())
513 {
514 // Use a name that the runtime knows.
515 this->init_fn_name_ = "__go_init_main";
516 }
517 else
518 {
519 std::string s = this->pkgpath_symbol();
520 s.append("..import");
521 this->init_fn_name_ = s;
522 }
523 }
524
525 return this->init_fn_name_;
526 }
527
528 // Return a mangled name for a type. These names appear in symbol
529 // names in the assembler file for things like type descriptors and
530 // methods.
531
532 std::string
mangled_name(Gogo * gogo) const533 Type::mangled_name(Gogo* gogo) const
534 {
535 std::string ret;
536
537 // The do_mangled_name virtual function will set RET to the mangled
538 // name before glue character mapping.
539 this->do_mangled_name(gogo, &ret);
540
541 // Type descriptor names and interface method table names use a ".."
542 // before the mangled name of a type, so to avoid ambiguity the
543 // mangled name must not start with 'u' or 'U' or a digit.
544 go_assert((ret[0] < '0' || ret[0] > '9') && ret[0] != ' ');
545 if (ret[0] == 'u' || ret[0] == 'U')
546 ret = " " + ret;
547
548 // Map glue characters as described above.
549
550 // The mapping is only unambiguous if there is no .DIGIT in the
551 // string, so check that.
552 for (size_t i = ret.find('.');
553 i != std::string::npos;
554 i = ret.find('.', i + 1))
555 {
556 if (i + 1 < ret.size())
557 {
558 char c = ret[i + 1];
559 go_assert(c < '0' || c > '9');
560 }
561 }
562
563 // The order of these characters is the replacement code.
564 const char * const replace = " *;,{}[]()";
565
566 const size_t rlen = strlen(replace);
567 char buf[2];
568 buf[0] = '.';
569 for (size_t ri = 0; ri < rlen; ++ri)
570 {
571 buf[1] = '0' + ri;
572 while (true)
573 {
574 size_t i = ret.find(replace[ri]);
575 if (i == std::string::npos)
576 break;
577 ret.replace(i, 1, buf, 2);
578 }
579 }
580
581 return ret;
582 }
583
584 // The mangled name is implemented as a method on each instance of
585 // Type.
586
587 void
do_mangled_name(Gogo *,std::string * ret) const588 Error_type::do_mangled_name(Gogo*, std::string* ret) const
589 {
590 ret->append("{error}");
591 }
592
593 void
do_mangled_name(Gogo *,std::string * ret) const594 Void_type::do_mangled_name(Gogo*, std::string* ret) const
595 {
596 ret->append("{void}");
597 }
598
599 void
do_mangled_name(Gogo *,std::string * ret) const600 Boolean_type::do_mangled_name(Gogo*, std::string* ret) const
601 {
602 ret->append("bool");
603 }
604
605 void
do_mangled_name(Gogo *,std::string * ret) const606 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
607 {
608 char buf[100];
609 snprintf(buf, sizeof buf, "%s%si%d",
610 this->is_abstract_ ? "{abstract}" : "",
611 this->is_unsigned_ ? "u" : "",
612 this->bits_);
613 ret->append(buf);
614 }
615
616 void
do_mangled_name(Gogo *,std::string * ret) const617 Float_type::do_mangled_name(Gogo*, std::string* ret) const
618 {
619 char buf[100];
620 snprintf(buf, sizeof buf, "%sfloat%d",
621 this->is_abstract_ ? "{abstract}" : "",
622 this->bits_);
623 ret->append(buf);
624 }
625
626 void
do_mangled_name(Gogo *,std::string * ret) const627 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
628 {
629 char buf[100];
630 snprintf(buf, sizeof buf, "%sc%d",
631 this->is_abstract_ ? "{abstract}" : "",
632 this->bits_);
633 ret->append(buf);
634 }
635
636 void
do_mangled_name(Gogo *,std::string * ret) const637 String_type::do_mangled_name(Gogo*, std::string* ret) const
638 {
639 ret->append("string");
640 }
641
642 void
do_mangled_name(Gogo * gogo,std::string * ret) const643 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
644 {
645 ret->append("func");
646
647 if (this->receiver_ != NULL)
648 {
649 ret->push_back('(');
650 this->append_mangled_name(this->receiver_->type(), gogo, ret);
651 ret->append(")");
652 }
653
654 ret->push_back('(');
655 const Typed_identifier_list* params = this->parameters();
656 if (params != NULL)
657 {
658 bool first = true;
659 for (Typed_identifier_list::const_iterator p = params->begin();
660 p != params->end();
661 ++p)
662 {
663 if (first)
664 first = false;
665 else
666 ret->push_back(',');
667 if (this->is_varargs_ && p + 1 == params->end())
668 {
669 // We can't use "..." here because the mangled name
670 // might start with 'u' or 'U', which would be ambiguous
671 // with the encoding of Unicode characters.
672 ret->append(",,,");
673 }
674 this->append_mangled_name(p->type(), gogo, ret);
675 }
676 }
677 ret->push_back(')');
678
679 ret->push_back('(');
680 const Typed_identifier_list* results = this->results();
681 if (results != NULL)
682 {
683 bool first = true;
684 for (Typed_identifier_list::const_iterator p = results->begin();
685 p != results->end();
686 ++p)
687 {
688 if (first)
689 first = false;
690 else
691 ret->append(",");
692 this->append_mangled_name(p->type(), gogo, ret);
693 }
694 }
695 ret->push_back(')');
696 }
697
698 void
do_mangled_name(Gogo * gogo,std::string * ret) const699 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
700 {
701 ret->push_back('*');
702 this->append_mangled_name(this->to_type_, gogo, ret);
703 }
704
705 void
do_mangled_name(Gogo *,std::string * ret) const706 Nil_type::do_mangled_name(Gogo*, std::string* ret) const
707 {
708 ret->append("{nil}");
709 }
710
711 void
do_mangled_name(Gogo * gogo,std::string * ret) const712 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
713 {
714 ret->append("struct{");
715
716 if (this->is_struct_incomparable_)
717 ret->append("{x}");
718
719 const Struct_field_list* fields = this->fields_;
720 if (fields != NULL)
721 {
722 bool first = true;
723 for (Struct_field_list::const_iterator p = fields->begin();
724 p != fields->end();
725 ++p)
726 {
727 if (first)
728 first = false;
729 else
730 ret->push_back(';');
731
732 if (!p->is_anonymous())
733 {
734 Gogo::append_possibly_hidden_name(ret, p->field_name());
735 ret->push_back(' ');
736 }
737
738 // For an anonymous field with an alias type, the field name
739 // is the alias name.
740 if (p->is_anonymous()
741 && p->type()->named_type() != NULL
742 && p->type()->named_type()->is_alias())
743 p->type()->named_type()->append_mangled_type_name(gogo, true, ret);
744 else
745 this->append_mangled_name(p->type(), gogo, ret);
746
747 if (p->has_tag())
748 {
749 // Use curly braces around a struct tag, since they are
750 // unambiguous here and we have no encoding for
751 // quotation marks.
752 ret->push_back('{');
753 ret->append(go_mangle_struct_tag(p->tag()));
754 ret->push_back('}');
755 }
756 }
757 }
758
759 ret->push_back('}');
760 }
761
762 void
do_mangled_name(Gogo * gogo,std::string * ret) const763 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
764 {
765 ret->push_back('[');
766 if (this->length_ != NULL)
767 {
768 Numeric_constant nc;
769 if (!this->length_->numeric_constant_value(&nc))
770 {
771 go_assert(saw_errors());
772 return;
773 }
774 mpz_t val;
775 if (!nc.to_int(&val))
776 {
777 go_assert(saw_errors());
778 return;
779 }
780 char *s = mpz_get_str(NULL, 10, val);
781 ret->append(s);
782 free(s);
783 mpz_clear(val);
784 if (this->is_array_incomparable_)
785 ret->append("x");
786 }
787 ret->push_back(']');
788 this->append_mangled_name(this->element_type_, gogo, ret);
789 }
790
791 void
do_mangled_name(Gogo * gogo,std::string * ret) const792 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
793 {
794 ret->append("map[");
795 this->append_mangled_name(this->key_type_, gogo, ret);
796 ret->push_back(']');
797 this->append_mangled_name(this->val_type_, gogo, ret);
798 }
799
800 void
do_mangled_name(Gogo * gogo,std::string * ret) const801 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
802 {
803 if (!this->may_send_)
804 ret->append("{}");
805 ret->append("chan");
806 if (!this->may_receive_)
807 ret->append("{}");
808 ret->push_back(' ');
809 this->append_mangled_name(this->element_type_, gogo, ret);
810 }
811
812 void
do_mangled_name(Gogo * gogo,std::string * ret) const813 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
814 {
815 go_assert(this->methods_are_finalized_);
816
817 ret->append("interface{");
818
819 const Typed_identifier_list* methods = this->all_methods_;
820 if (methods != NULL && !this->seen_)
821 {
822 this->seen_ = true;
823 bool first = true;
824 for (Typed_identifier_list::const_iterator p = methods->begin();
825 p != methods->end();
826 ++p)
827 {
828 if (first)
829 first = false;
830 else
831 ret->push_back(';');
832
833 if (!p->name().empty())
834 {
835 Gogo::append_possibly_hidden_name(ret, p->name());
836 ret->push_back(' ');
837 }
838
839 this->append_mangled_name(p->type(), gogo, ret);
840 }
841 this->seen_ = false;
842 }
843
844 ret->push_back('}');
845 }
846
847 void
do_mangled_name(Gogo * gogo,std::string * ret) const848 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
849 {
850 this->append_mangled_type_name(gogo, false, ret);
851 }
852
853 void
do_mangled_name(Gogo * gogo,std::string * ret) const854 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
855 {
856 if (this->is_defined())
857 this->append_mangled_name(this->real_type(), gogo, ret);
858 else
859 {
860 const Named_object* no = this->named_object();
861 if (no->package() == NULL)
862 ret->append(gogo->pkgpath());
863 else
864 ret->append(no->package()->pkgpath());
865 ret->push_back('.');
866 ret->append(Gogo::unpack_hidden_name(no->name()));
867 }
868 }
869
870 // Append the mangled name for a named type to RET. For an alias we
871 // normally use the real name, but if USE_ALIAS is true we use the
872 // alias name itself.
873
874 void
append_mangled_type_name(Gogo * gogo,bool use_alias,std::string * ret) const875 Named_type::append_mangled_type_name(Gogo* gogo, bool use_alias,
876 std::string* ret) const
877 {
878 if (this->is_error_)
879 return;
880 if (this->is_alias_ && !use_alias)
881 {
882 if (this->seen_alias_)
883 return;
884 this->seen_alias_ = true;
885 this->append_mangled_name(this->type_, gogo, ret);
886 this->seen_alias_ = false;
887 return;
888 }
889 Named_object* no = this->named_object_;
890 std::string name;
891 if (this->is_builtin())
892 go_assert(this->in_function_ == NULL);
893 else
894 {
895 if (this->in_function_ != NULL)
896 {
897 const Typed_identifier* rcvr =
898 this->in_function_->func_value()->type()->receiver();
899 if (rcvr != NULL)
900 ret->append(rcvr->type()->deref()->mangled_name(gogo));
901 else if (this->in_function_->package() == NULL)
902 ret->append(gogo->pkgpath());
903 else
904 ret->append(this->in_function_->package()->pkgpath());
905 ret->push_back('.');
906 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
907 }
908 else
909 {
910 if (no->package() == NULL)
911 ret->append(gogo->pkgpath());
912 else
913 ret->append(no->package()->pkgpath());
914 }
915 ret->push_back('.');
916 }
917
918 ret->append(Gogo::unpack_hidden_name(no->name()));
919
920 if (this->in_function_ != NULL && this->in_function_index_ > 0)
921 {
922 char buf[30];
923 snprintf(buf, sizeof buf, "..i%u", this->in_function_index_);
924 ret->append(buf);
925 }
926 }
927
928 // Return the name for the type descriptor symbol for TYPE. This can
929 // be a global, common, or local symbol, depending. NT is not NULL if
930 // it is the name to use.
931
932 std::string
type_descriptor_name(Type * type,Named_type * nt)933 Gogo::type_descriptor_name(Type* type, Named_type* nt)
934 {
935 // The type descriptor symbol for the unsafe.Pointer type is defined
936 // in libgo/runtime/go-unsafe-pointer.c, so just use a reference to
937 // that symbol for all unsafe pointer types.
938 if (type->is_unsafe_pointer_type())
939 return "unsafe.Pointer..d";
940
941 if (nt == NULL)
942 return "type.." + type->mangled_name(this);
943
944 std::string ret;
945 Named_object* no = nt->named_object();
946 unsigned int index;
947 const Named_object* in_function = nt->in_function(&index);
948 if (nt->is_builtin())
949 go_assert(in_function == NULL);
950 else
951 {
952 if (in_function != NULL)
953 {
954 const Typed_identifier* rcvr =
955 in_function->func_value()->type()->receiver();
956 if (rcvr != NULL)
957 ret.append(rcvr->type()->deref()->mangled_name(this));
958 else if (in_function->package() == NULL)
959 ret.append(this->pkgpath());
960 else
961 ret.append(in_function->package()->pkgpath());
962 ret.push_back('.');
963 ret.append(Gogo::unpack_hidden_name(in_function->name()));
964 ret.push_back('.');
965 }
966
967 if (no->package() == NULL)
968 ret.append(this->pkgpath());
969 else
970 ret.append(no->package()->pkgpath());
971 ret.push_back('.');
972 }
973
974 Gogo::append_possibly_hidden_name(&ret, no->name());
975
976 if (in_function != NULL && index > 0)
977 {
978 char buf[30];
979 snprintf(buf, sizeof buf, "..i%u", index);
980 ret.append(buf);
981 }
982
983 ret.append("..d");
984
985 return ret;
986 }
987
988 // Return the name for the GC symbol for a type. This is used to
989 // initialize the gcdata field of a type descriptor. This is a local
990 // name never referenced outside of this assembly file. (Note that
991 // some type descriptors will initialize the gcdata field with a name
992 // generated by ptrmask_symbol_name rather than this method.)
993
994 std::string
gc_symbol_name(Type * type)995 Gogo::gc_symbol_name(Type* type)
996 {
997 return this->type_descriptor_name(type, type->named_type()) + "..g";
998 }
999
1000 // Return the name for a ptrmask variable. PTRMASK_SYM_NAME is a
1001 // base32 string encoding the ptrmask (as returned by Ptrmask::symname
1002 // in types.cc). This name is used to intialize the gcdata field of a
1003 // type descriptor. These names are globally visible. (Note that
1004 // some type descriptors will initialize the gcdata field with a name
1005 // generated by gc_symbol_name rather than this method.)
1006
1007 std::string
ptrmask_symbol_name(const std::string & ptrmask_sym_name)1008 Gogo::ptrmask_symbol_name(const std::string& ptrmask_sym_name)
1009 {
1010 return "gcbits.." + ptrmask_sym_name;
1011 }
1012
1013 // Return the name to use for an interface method table used for the
1014 // ordinary type TYPE converted to the interface type ITYPE.
1015 // IS_POINTER is true if this is for the method set for a pointer
1016 // receiver.
1017
1018 std::string
interface_method_table_name(Interface_type * itype,Type * type,bool is_pointer)1019 Gogo::interface_method_table_name(Interface_type* itype, Type* type,
1020 bool is_pointer)
1021 {
1022 return ((is_pointer ? "pimt.." : "imt..")
1023 + itype->mangled_name(this)
1024 + ".."
1025 + type->mangled_name(this));
1026 }
1027
1028 // Return whether NAME is a special name that can not be passed to
1029 // unpack_hidden_name. This is needed because various special names
1030 // use "..SUFFIX", but unpack_hidden_name just looks for '.'.
1031
1032 bool
is_special_name(const std::string & name)1033 Gogo::is_special_name(const std::string& name)
1034 {
1035 return (name.find("..hash") != std::string::npos
1036 || name.find("..eq") != std::string::npos
1037 || name.find("..stub") != std::string::npos
1038 || name.find("..func") != std::string::npos
1039 || name.find("..r") != std::string::npos
1040 || name.find("..init") != std::string::npos
1041 || name.find("..thunk") != std::string::npos
1042 || name.find("..import") != std::string::npos);
1043 }
1044