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.  Since before this encoding names can never
192 // contain consecutive dots followed by 'u' or 'U', and after this
193 // encoding "..u" and "..U" are followed by a known number of
194 // characters, this is unambiguous.
195 //
196 // Demangling these names is straightforward:
197 //  - replace ..uXXXX with a unicode character
198 //  - replace ..UXXXXXXXX with a unicode character
199 //  - replace .D, where D is a digit, with the character from the above
200 // That will get you as close as possible to a readable name.
201 
202 // Return the assembler name to use for an exported function, a
203 // method, or a function/method declaration.  This is not called if
204 // the function has been given an explicit name via a magic //extern
205 // or //go:linkname comment.  GO_NAME is the name that appears in the
206 // Go code.  PACKAGE is the package where the function is defined, and
207 // is NULL for the package being compiled.  For a method, RTYPE is
208 // the method's receiver type; for a function, RTYPE is NULL.
209 
210 std::string
function_asm_name(const std::string & go_name,const Package * package,const Type * rtype)211 Gogo::function_asm_name(const std::string& go_name, const Package* package,
212 			const Type* rtype)
213 {
214   std::string ret;
215   if (rtype != NULL)
216     ret = rtype->deref()->mangled_name(this);
217   else if (package == NULL)
218     ret = this->pkgpath_symbol();
219   else
220     ret = package->pkgpath_symbol();
221   ret.push_back('.');
222   // Check for special names that will break if we use
223   // Gogo::unpack_hidden_name.
224   if (Gogo::is_special_name(go_name))
225     ret.append(go_name);
226   else
227     ret.append(Gogo::unpack_hidden_name(go_name));
228   return go_encode_id(ret);
229 }
230 
231 // Return the name to use for a function descriptor.  These symbols
232 // are globally visible.
233 
234 std::string
function_descriptor_name(Named_object * no)235 Gogo::function_descriptor_name(Named_object* no)
236 {
237   if (no->is_function() && !no->func_value()->asm_name().empty())
238     return no->func_value()->asm_name() + "..f";
239   else if (no->is_function_declaration()
240 	   && !no->func_declaration_value()->asm_name().empty())
241     return no->func_declaration_value()->asm_name() + "..f";
242   std::string ret = this->function_asm_name(no->name(), no->package(), NULL);
243   ret.append("..f");
244   return ret;
245 }
246 
247 // Return the name to use for a generated stub method.  MNAME is the
248 // method name.  PACKAGE is the package where the type that needs this
249 // stub method is defined.  These functions are globally visible.
250 // Note that this is the function name that corresponds to the name
251 // used for the method in Go source code, if this stub method were
252 // written in Go.  The assembler name will be generated by
253 // Gogo::function_asm_name, and because this is a method that name
254 // will include the receiver type.
255 
256 std::string
stub_method_name(const Package * package,const std::string & mname)257 Gogo::stub_method_name(const Package* package, const std::string& mname)
258 {
259   if (!Gogo::is_hidden_name(mname))
260     return mname + "..stub";
261 
262   const std::string& ppkgpath(package == NULL
263 			      ? this->pkgpath()
264 			      : package->pkgpath());
265   std::string mpkgpath = Gogo::hidden_name_pkgpath(mname);
266   if (mpkgpath == ppkgpath)
267     return Gogo::unpack_hidden_name(mname) + "..stub";
268 
269   // We are creating a stub method for an unexported method of an
270   // imported embedded type.  We need to disambiguate the method name.
271   std::string ret = this->pkgpath_symbol_for_package(mpkgpath);
272   ret.push_back('.');
273   ret.append(Gogo::unpack_hidden_name(mname));
274   ret.append("..stub");
275   return ret;
276 }
277 
278 // Return the names of the hash and equality functions for TYPE.  If
279 // NAME is not NULL it is the name of the type.  Set *HASH_NAME and
280 // *EQUAL_NAME.
281 
282 void
specific_type_function_names(const Type * type,const Named_type * name,std::string * hash_name,std::string * equal_name)283 Gogo::specific_type_function_names(const Type* type, const Named_type* name,
284 				   std::string *hash_name,
285 				   std::string *equal_name)
286 {
287   const Type* rtype = type;
288   if (name != NULL)
289     rtype = name;
290   std::string tname = rtype->mangled_name(this);
291   *hash_name = tname + "..hash";
292   *equal_name = tname + "..eq";
293 }
294 
295 // Return the assembler name to use for a global variable.  GO_NAME is
296 // the name that appears in the Go code.  PACKAGE is the package where
297 // the variable is defined, and is NULL for the package being
298 // compiled.
299 
300 std::string
global_var_asm_name(const std::string & go_name,const Package * package)301 Gogo::global_var_asm_name(const std::string& go_name, const Package* package)
302 {
303   std::string ret;
304   if (package == NULL)
305     ret = this->pkgpath_symbol();
306   else
307     ret = package->pkgpath_symbol();
308   ret.append(1, '.');
309   ret.append(Gogo::unpack_hidden_name(go_name));
310   return go_encode_id(ret);
311 }
312 
313 // Return an erroneous name that indicates that an error has already
314 // been reported.
315 
316 std::string
erroneous_name()317 Gogo::erroneous_name()
318 {
319   go_assert(saw_errors());
320   static int erroneous_count;
321   char name[50];
322   snprintf(name, sizeof name, ".erroneous%d", erroneous_count);
323   ++erroneous_count;
324   return name;
325 }
326 
327 // Return whether a name is an erroneous name.
328 
329 bool
is_erroneous_name(const std::string & name)330 Gogo::is_erroneous_name(const std::string& name)
331 {
332   return name.compare(0, 10, ".erroneous") == 0;
333 }
334 
335 // Return a name for a thunk object.
336 
337 std::string
thunk_name()338 Gogo::thunk_name()
339 {
340   static int thunk_count;
341   char thunk_name[50];
342   snprintf(thunk_name, sizeof thunk_name, "..thunk%d", thunk_count);
343   ++thunk_count;
344   std::string ret = this->pkgpath_symbol();
345   return ret + thunk_name;
346 }
347 
348 // Return whether a function is a thunk.
349 
350 bool
is_thunk(const Named_object * no)351 Gogo::is_thunk(const Named_object* no)
352 {
353   const std::string& name(no->name());
354   size_t i = name.find("..thunk");
355   if (i == std::string::npos)
356     return false;
357   for (i += 7; i < name.size(); ++i)
358     if (name[i] < '0' || name[i] > '9')
359       return false;
360   return true;
361 }
362 
363 // Return the name to use for an init function.  There can be multiple
364 // functions named "init" so each one needs a different name.
365 
366 std::string
init_function_name()367 Gogo::init_function_name()
368 {
369   static int init_count;
370   char buf[30];
371   snprintf(buf, sizeof buf, "..init%d", init_count);
372   ++init_count;
373   std::string ret = this->pkgpath_symbol();
374   return ret + buf;
375 }
376 
377 // Return the name to use for a nested function.
378 
379 std::string
nested_function_name(Named_object * enclosing)380 Gogo::nested_function_name(Named_object* enclosing)
381 {
382   std::string prefix;
383   unsigned int index;
384   if (enclosing == NULL)
385     {
386       // A function literal at top level, as in
387       // var f = func() {}
388       static unsigned int toplevel_index;
389       ++toplevel_index;
390       index = toplevel_index;
391       prefix = ".go";
392     }
393   else
394     {
395       while (true)
396 	{
397 	  Named_object* parent = enclosing->func_value()->enclosing();
398 	  if (parent == NULL)
399 	    break;
400 	  enclosing = parent;
401 	}
402       const Typed_identifier* rcvr =
403 	enclosing->func_value()->type()->receiver();
404       if (rcvr != NULL)
405 	{
406 	  prefix = rcvr->type()->mangled_name(this);
407 	  prefix.push_back('.');
408 	}
409       prefix.append(Gogo::unpack_hidden_name(enclosing->name()));
410       index = enclosing->func_value()->next_nested_function_index();
411     }
412   char buf[30];
413   snprintf(buf, sizeof buf, "..func%u", index);
414   return prefix + buf;
415 }
416 
417 // Return the name to use for a sink function, a function whose name
418 // is simply underscore.  We don't really need these functions but we
419 // do have to generate them for error checking.
420 
421 std::string
sink_function_name()422 Gogo::sink_function_name()
423 {
424   static int sink_count;
425   char buf[30];
426   snprintf(buf, sizeof buf, ".sink%d", sink_count);
427   ++sink_count;
428   return buf;
429 }
430 
431 // Return the name to use for a redefined function.  These functions
432 // are erroneous but we still generate them for further error
433 // checking.
434 
435 std::string
redefined_function_name()436 Gogo::redefined_function_name()
437 {
438   static int redefinition_count;
439   char buf[30];
440   snprintf(buf, sizeof buf, ".redefined%d", redefinition_count);
441   ++redefinition_count;
442   return buf;
443 }
444 
445 // Return the name to use for a recover thunk for the function NAME.
446 // If the function is a method, RTYPE is the receiver type.
447 
448 std::string
recover_thunk_name(const std::string & name,const Type * rtype)449 Gogo::recover_thunk_name(const std::string& name, const Type* rtype)
450 {
451   std::string ret;
452   if (rtype != NULL)
453     {
454       ret = rtype->mangled_name(this);
455       ret.append(1, '.');
456     }
457   if (Gogo::is_special_name(name))
458     ret.append(name);
459   else
460     ret.append(Gogo::unpack_hidden_name(name));
461   ret.append("..r");
462   return ret;
463 }
464 
465 // Return the name to use for a GC root variable.  The GC root
466 // variable is a composite literal that is passed to
467 // runtime.registerGCRoots.  There is at most one of these variables
468 // per compilation.
469 
470 std::string
gc_root_name()471 Gogo::gc_root_name()
472 {
473   return "go..C0";
474 }
475 
476 // Return the name to use for a composite literal or string
477 // initializer.  This is a local name never referenced outside of this
478 // file.
479 
480 std::string
initializer_name()481 Gogo::initializer_name()
482 {
483   static unsigned int counter;
484   char buf[30];
485   ++counter;
486   snprintf(buf, sizeof buf, "go..C%u", counter);
487   return buf;
488 }
489 
490 // Return the name of the variable used to represent the zero value of
491 // a map.  This is a globally visible common symbol.
492 
493 std::string
map_zero_value_name()494 Gogo::map_zero_value_name()
495 {
496   return "go..zerovalue";
497 }
498 
499 // Return the name to use for the import control function.
500 
501 const std::string&
get_init_fn_name()502 Gogo::get_init_fn_name()
503 {
504   if (this->init_fn_name_.empty())
505     {
506       go_assert(this->package_ != NULL);
507       if (this->is_main_package())
508 	{
509 	  // Use a name that the runtime knows.
510 	  this->init_fn_name_ = "__go_init_main";
511 	}
512       else
513 	{
514 	  std::string s = this->pkgpath_symbol();
515 	  s.append("..import");
516 	  this->init_fn_name_ = s;
517 	}
518     }
519 
520   return this->init_fn_name_;
521 }
522 
523 // Return a mangled name for a type.  These names appear in symbol
524 // names in the assembler file for things like type descriptors and
525 // methods.
526 
527 std::string
mangled_name(Gogo * gogo) const528 Type::mangled_name(Gogo* gogo) const
529 {
530   std::string ret;
531 
532   // The do_mangled_name virtual function will set RET to the mangled
533   // name before glue character mapping.
534   this->do_mangled_name(gogo, &ret);
535 
536   // Type descriptor names and interface method table names use a ".."
537   // before the mangled name of a type, so to avoid ambiguity the
538   // mangled name must not start with 'u' or 'U' or a digit.
539   go_assert((ret[0] < '0' || ret[0] > '9') && ret[0] != ' ');
540   if (ret[0] == 'u' || ret[0] == 'U')
541     ret = " " + ret;
542 
543   // Map glue characters as described above.
544 
545   // The mapping is only unambiguous if there is no .DIGIT in the
546   // string, so check that.
547   for (size_t i = ret.find('.');
548        i != std::string::npos;
549        i = ret.find('.', i + 1))
550     {
551       if (i + 1 < ret.size())
552 	{
553 	  char c = ret[i + 1];
554 	  go_assert(c < '0' || c > '9');
555 	}
556     }
557 
558   // The order of these characters is the replacement code.
559   const char * const replace = " *;,{}[]()";
560 
561   const size_t rlen = strlen(replace);
562   char buf[2];
563   buf[0] = '.';
564   for (size_t ri = 0; ri < rlen; ++ri)
565     {
566       buf[1] = '0' + ri;
567       while (true)
568 	{
569 	  size_t i = ret.find(replace[ri]);
570 	  if (i == std::string::npos)
571 	    break;
572 	  ret.replace(i, 1, buf, 2);
573 	}
574     }
575 
576   return ret;
577 }
578 
579 // The mangled name is implemented as a method on each instance of
580 // Type.
581 
582 void
do_mangled_name(Gogo *,std::string * ret) const583 Error_type::do_mangled_name(Gogo*, std::string* ret) const
584 {
585   ret->append("{error}");
586 }
587 
588 void
do_mangled_name(Gogo *,std::string * ret) const589 Void_type::do_mangled_name(Gogo*, std::string* ret) const
590 {
591   ret->append("{void}");
592 }
593 
594 void
do_mangled_name(Gogo *,std::string * ret) const595 Boolean_type::do_mangled_name(Gogo*, std::string* ret) const
596 {
597   ret->append("bool");
598 }
599 
600 void
do_mangled_name(Gogo *,std::string * ret) const601 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
602 {
603   char buf[100];
604   snprintf(buf, sizeof buf, "%s%si%d",
605 	   this->is_abstract_ ? "{abstract}" : "",
606 	   this->is_unsigned_ ? "u" : "",
607 	   this->bits_);
608   ret->append(buf);
609 }
610 
611 void
do_mangled_name(Gogo *,std::string * ret) const612 Float_type::do_mangled_name(Gogo*, std::string* ret) const
613 {
614   char buf[100];
615   snprintf(buf, sizeof buf, "%sfloat%d",
616 	   this->is_abstract_ ? "{abstract}" : "",
617 	   this->bits_);
618   ret->append(buf);
619 }
620 
621 void
do_mangled_name(Gogo *,std::string * ret) const622 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
623 {
624   char buf[100];
625   snprintf(buf, sizeof buf, "%sc%d",
626 	   this->is_abstract_ ? "{abstract}" : "",
627 	   this->bits_);
628   ret->append(buf);
629 }
630 
631 void
do_mangled_name(Gogo *,std::string * ret) const632 String_type::do_mangled_name(Gogo*, std::string* ret) const
633 {
634   ret->append("string");
635 }
636 
637 void
do_mangled_name(Gogo * gogo,std::string * ret) const638 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
639 {
640   ret->append("func");
641 
642   if (this->receiver_ != NULL)
643     {
644       ret->push_back('(');
645       this->append_mangled_name(this->receiver_->type(), gogo, ret);
646       ret->append(")");
647     }
648 
649   ret->push_back('(');
650   const Typed_identifier_list* params = this->parameters();
651   if (params != NULL)
652     {
653       bool first = true;
654       for (Typed_identifier_list::const_iterator p = params->begin();
655 	   p != params->end();
656 	   ++p)
657 	{
658 	  if (first)
659 	    first = false;
660 	  else
661 	    ret->push_back(',');
662 	  if (this->is_varargs_ && p + 1 == params->end())
663 	    {
664 	      // We can't use "..." here because the mangled name
665 	      // might start with 'u' or 'U', which would be ambiguous
666 	      // with the encoding of Unicode characters.
667 	      ret->append(",,,");
668 	    }
669 	  this->append_mangled_name(p->type(), gogo, ret);
670 	}
671     }
672   ret->push_back(')');
673 
674   ret->push_back('(');
675   const Typed_identifier_list* results = this->results();
676   if (results != NULL)
677     {
678       bool first = true;
679       for (Typed_identifier_list::const_iterator p = results->begin();
680 	   p != results->end();
681 	   ++p)
682 	{
683 	  if (first)
684 	    first = false;
685 	  else
686 	    ret->append(",");
687 	  this->append_mangled_name(p->type(), gogo, ret);
688 	}
689     }
690   ret->push_back(')');
691 }
692 
693 void
do_mangled_name(Gogo * gogo,std::string * ret) const694 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
695 {
696   ret->push_back('*');
697   this->append_mangled_name(this->to_type_, gogo, ret);
698 }
699 
700 void
do_mangled_name(Gogo *,std::string * ret) const701 Nil_type::do_mangled_name(Gogo*, std::string* ret) const
702 {
703   ret->append("{nil}");
704 }
705 
706 void
do_mangled_name(Gogo * gogo,std::string * ret) const707 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
708 {
709   ret->append("struct{");
710 
711   if (this->is_struct_incomparable_)
712     ret->append("{x}");
713 
714   const Struct_field_list* fields = this->fields_;
715   if (fields != NULL)
716     {
717       bool first = true;
718       for (Struct_field_list::const_iterator p = fields->begin();
719 	   p != fields->end();
720 	   ++p)
721 	{
722 	  if (first)
723 	    first = false;
724 	  else
725 	    ret->push_back(';');
726 
727 	  if (!p->is_anonymous())
728 	    {
729 	      ret->append(Gogo::mangle_possibly_hidden_name(p->field_name()));
730 	      ret->push_back(' ');
731 	    }
732 
733 	  // For an anonymous field with an alias type, the field name
734 	  // is the alias name.
735 	  if (p->is_anonymous()
736 	      && p->type()->named_type() != NULL
737 	      && p->type()->named_type()->is_alias())
738 	    p->type()->named_type()->append_mangled_type_name(gogo, true, ret);
739 	  else
740 	    this->append_mangled_name(p->type(), gogo, ret);
741 
742 	  if (p->has_tag())
743 	    {
744 	      // Use curly braces around a struct tag, since they are
745 	      // unambiguous here and we have no encoding for
746 	      // quotation marks.
747 	      ret->push_back('{');
748 	      ret->append(go_mangle_struct_tag(p->tag()));
749 	      ret->push_back('}');
750 	    }
751 	}
752     }
753 
754   ret->push_back('}');
755 }
756 
757 void
do_mangled_name(Gogo * gogo,std::string * ret) const758 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
759 {
760   ret->push_back('[');
761   if (this->length_ != NULL)
762     {
763       Numeric_constant nc;
764       if (!this->length_->numeric_constant_value(&nc))
765 	{
766 	  go_assert(saw_errors());
767 	  return;
768 	}
769       mpz_t val;
770       if (!nc.to_int(&val))
771 	{
772 	  go_assert(saw_errors());
773 	  return;
774 	}
775       char *s = mpz_get_str(NULL, 10, val);
776       ret->append(s);
777       free(s);
778       mpz_clear(val);
779       if (this->is_array_incomparable_)
780 	ret->append("x");
781     }
782   ret->push_back(']');
783   this->append_mangled_name(this->element_type_, gogo, ret);
784 }
785 
786 void
do_mangled_name(Gogo * gogo,std::string * ret) const787 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
788 {
789   ret->append("map[");
790   this->append_mangled_name(this->key_type_, gogo, ret);
791   ret->push_back(']');
792   this->append_mangled_name(this->val_type_, gogo, ret);
793 }
794 
795 void
do_mangled_name(Gogo * gogo,std::string * ret) const796 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
797 {
798   if (!this->may_send_)
799     ret->append("{}");
800   ret->append("chan");
801   if (!this->may_receive_)
802     ret->append("{}");
803   ret->push_back(' ');
804   this->append_mangled_name(this->element_type_, gogo, ret);
805 }
806 
807 void
do_mangled_name(Gogo * gogo,std::string * ret) const808 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
809 {
810   go_assert(this->methods_are_finalized_);
811 
812   ret->append("interface{");
813 
814   const Typed_identifier_list* methods = this->all_methods_;
815   if (methods != NULL && !this->seen_)
816     {
817       this->seen_ = true;
818       bool first = true;
819       for (Typed_identifier_list::const_iterator p = methods->begin();
820 	   p != methods->end();
821 	   ++p)
822 	{
823 	  if (first)
824 	    first = false;
825 	  else
826 	    ret->push_back(';');
827 
828 	  if (!p->name().empty())
829 	    {
830 	      ret->append(Gogo::mangle_possibly_hidden_name(p->name()));
831 	      ret->push_back(' ');
832 	    }
833 
834 	  this->append_mangled_name(p->type(), gogo, ret);
835 	}
836       this->seen_ = false;
837     }
838 
839   ret->push_back('}');
840 }
841 
842 void
do_mangled_name(Gogo * gogo,std::string * ret) const843 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
844 {
845   this->append_mangled_type_name(gogo, false, ret);
846 }
847 
848 void
do_mangled_name(Gogo * gogo,std::string * ret) const849 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
850 {
851   if (this->is_defined())
852     this->append_mangled_name(this->real_type(), gogo, ret);
853   else
854     {
855       const Named_object* no = this->named_object();
856       if (no->package() == NULL)
857 	ret->append(gogo->pkgpath_symbol());
858       else
859 	ret->append(no->package()->pkgpath_symbol());
860       ret->push_back('.');
861       ret->append(Gogo::unpack_hidden_name(no->name()));
862     }
863 }
864 
865 // Append the mangled name for a named type to RET.  For an alias we
866 // normally use the real name, but if USE_ALIAS is true we use the
867 // alias name itself.
868 
869 void
append_mangled_type_name(Gogo * gogo,bool use_alias,std::string * ret) const870 Named_type::append_mangled_type_name(Gogo* gogo, bool use_alias,
871 				     std::string* ret) const
872 {
873   if (this->is_error_)
874     return;
875   if (this->is_alias_ && !use_alias)
876     {
877       if (this->seen_alias_)
878 	return;
879       this->seen_alias_ = true;
880       this->append_mangled_name(this->type_, gogo, ret);
881       this->seen_alias_ = false;
882       return;
883     }
884   Named_object* no = this->named_object_;
885   std::string name;
886   if (this->is_builtin())
887     go_assert(this->in_function_ == NULL);
888   else
889     {
890       if (this->in_function_ != NULL)
891 	{
892 	  const Typed_identifier* rcvr =
893 	    this->in_function_->func_value()->type()->receiver();
894 	  if (rcvr != NULL)
895 	    ret->append(rcvr->type()->deref()->mangled_name(gogo));
896 	  else if (this->in_function_->package() == NULL)
897 	    ret->append(gogo->pkgpath_symbol());
898 	  else
899 	    ret->append(this->in_function_->package()->pkgpath_symbol());
900 	  ret->push_back('.');
901 	  ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
902 	}
903       else
904 	{
905 	  if (no->package() == NULL)
906 	    ret->append(gogo->pkgpath_symbol());
907 	  else
908 	    ret->append(no->package()->pkgpath_symbol());
909 	}
910       ret->push_back('.');
911     }
912 
913   ret->append(Gogo::unpack_hidden_name(no->name()));
914 
915   if (this->in_function_ != NULL && this->in_function_index_ > 0)
916     {
917       char buf[30];
918       snprintf(buf, sizeof buf, "..i%u", this->in_function_index_);
919       ret->append(buf);
920     }
921 }
922 
923 // Return the name for the type descriptor symbol for TYPE.  This can
924 // be a global, common, or local symbol, depending.  NT is not NULL if
925 // it is the name to use.
926 
927 std::string
type_descriptor_name(Type * type,Named_type * nt)928 Gogo::type_descriptor_name(Type* type, Named_type* nt)
929 {
930   // The type descriptor symbol for the unsafe.Pointer type is defined
931   // in libgo/runtime/go-unsafe-pointer.c, so just use a reference to
932   // that symbol for all unsafe pointer types.
933   if (type->is_unsafe_pointer_type())
934     return "unsafe.Pointer..d";
935 
936   if (nt == NULL)
937     return "type.." + type->mangled_name(this);
938 
939   std::string ret;
940   Named_object* no = nt->named_object();
941   unsigned int index;
942   const Named_object* in_function = nt->in_function(&index);
943   if (nt->is_builtin())
944     go_assert(in_function == NULL);
945   else
946     {
947       if (in_function != NULL)
948 	{
949 	  const Typed_identifier* rcvr =
950 	    in_function->func_value()->type()->receiver();
951 	  if (rcvr != NULL)
952 	    ret.append(rcvr->type()->deref()->mangled_name(this));
953 	  else if (in_function->package() == NULL)
954 	    ret.append(this->pkgpath_symbol());
955 	  else
956 	    ret.append(in_function->package()->pkgpath_symbol());
957 	  ret.push_back('.');
958 	  ret.append(Gogo::unpack_hidden_name(in_function->name()));
959 	  ret.push_back('.');
960 	}
961 
962       if (no->package() == NULL)
963 	ret.append(this->pkgpath_symbol());
964       else
965 	ret.append(no->package()->pkgpath_symbol());
966       ret.push_back('.');
967     }
968 
969   ret.append(Gogo::mangle_possibly_hidden_name(no->name()));
970 
971   if (in_function != NULL && index > 0)
972     {
973       char buf[30];
974       snprintf(buf, sizeof buf, "..i%u", index);
975       ret.append(buf);
976     }
977 
978   ret.append("..d");
979 
980   return ret;
981 }
982 
983 // Return the name for the GC symbol for a type.  This is used to
984 // initialize the gcdata field of a type descriptor.  This is a local
985 // name never referenced outside of this assembly file.  (Note that
986 // some type descriptors will initialize the gcdata field with a name
987 // generated by ptrmask_symbol_name rather than this method.)
988 
989 std::string
gc_symbol_name(Type * type)990 Gogo::gc_symbol_name(Type* type)
991 {
992   return this->type_descriptor_name(type, type->named_type()) + "..g";
993 }
994 
995 // Return the name for a ptrmask variable.  PTRMASK_SYM_NAME is a
996 // base32 string encoding the ptrmask (as returned by Ptrmask::symname
997 // in types.cc).  This name is used to intialize the gcdata field of a
998 // type descriptor.  These names are globally visible.  (Note that
999 // some type descriptors will initialize the gcdata field with a name
1000 // generated by gc_symbol_name rather than this method.)
1001 
1002 std::string
ptrmask_symbol_name(const std::string & ptrmask_sym_name)1003 Gogo::ptrmask_symbol_name(const std::string& ptrmask_sym_name)
1004 {
1005   return "gcbits.." + ptrmask_sym_name;
1006 }
1007 
1008 // Return the name to use for an interface method table used for the
1009 // ordinary type TYPE converted to the interface type ITYPE.
1010 // IS_POINTER is true if this is for the method set for a pointer
1011 // receiver.
1012 
1013 std::string
interface_method_table_name(Interface_type * itype,Type * type,bool is_pointer)1014 Gogo::interface_method_table_name(Interface_type* itype, Type* type,
1015 				  bool is_pointer)
1016 {
1017   return ((is_pointer ? "pimt.." : "imt..")
1018 	  + itype->mangled_name(this)
1019 	  + ".."
1020 	  + type->mangled_name(this));
1021 }
1022 
1023 // Return whether NAME is a special name that can not be passed to
1024 // unpack_hidden_name.  This is needed because various special names
1025 // use "..SUFFIX", but unpack_hidden_name just looks for '.'.
1026 
1027 bool
is_special_name(const std::string & name)1028 Gogo::is_special_name(const std::string& name)
1029 {
1030   return (name.find("..hash") != std::string::npos
1031 	  || name.find("..eq") != std::string::npos
1032 	  || name.find("..stub") != std::string::npos
1033 	  || name.find("..func") != std::string::npos
1034 	  || name.find("..r") != std::string::npos
1035 	  || name.find("..init") != std::string::npos
1036 	  || name.find("..thunk") != std::string::npos
1037 	  || name.find("..import") != std::string::npos);
1038 }
1039