aboutsummaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/go-encode-id.cc
blob: 7ab65f513b394642c7448f6e21ff91719b1474fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// go-encode-id.cc -- Go identifier and packagepath encoding/decoding hooks

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "go-system.h"

#include "gogo.h"
#include "go-location.h"
#include "go-linemap.h"
#include "go-encode-id.h"
#include "lex.h"

// Return whether the character c can appear in a name that we are
// encoding.  We only permit ASCII alphanumeric characters.

static bool
char_needs_encoding(char c)
{
  switch (c)
    {
    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
    case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
    case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
    case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
    case 'Y': case 'Z':
    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
    case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
    case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
    case 's': case 't': case 'u': case 'v': case 'w': case 'x':
    case 'y': case 'z':
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      return false;
    default:
      return true;
    }
}

// Return whether the identifier needs to be translated because it
// contains non-ASCII characters.

bool
go_id_needs_encoding(const std::string& str)
{
  for (std::string::const_iterator p = str.begin();
       p != str.end();
       ++p)
    if (char_needs_encoding(*p))
      return true;
  return false;
}

// Map from characters to the underscore encoding for them.

class Special_char_code
{
 public:
  Special_char_code();

  // Return the simple underscore encoding for C, or 0 if none.
  char
  code_for(unsigned int c) const
  {
    if (c <= 127)
      return this->codes_[c];
    return 0;
  }

 private:
  // Encodings for characters.
  char codes_[128];
};

// Construct the underscore encoding map.

Special_char_code::Special_char_code()
{
  memset(this->codes_, 0, sizeof this->codes_);
  this->codes_['_'] = '_';
  this->codes_['.'] = '0';
  this->codes_['/'] = '1';
  this->codes_['*'] = '2';
  this->codes_[','] = '3';
  this->codes_['{'] = '4';
  this->codes_['}'] = '5';
  this->codes_['['] = '6';
  this->codes_[']'] = '7';
  this->codes_['('] = '8';
  this->codes_[')'] = '9';
  this->codes_['"'] = 'a';
  this->codes_[' '] = 'b';
  this->codes_[';'] = 'c';
}

// The singleton Special_char_code.

static const Special_char_code special_char_code;

// Pull the next UTF-8 character out of P and store it in *PC.  Return
// the number of bytes read.

static size_t
fetch_utf8_char(const char* p, unsigned int* pc)
{
  unsigned char c = *p;
  if ((c & 0x80) == 0)
    {
      *pc = c;
      return 1;
    }
  size_t len = 0;
  while ((c & 0x80) != 0)
    {
      ++len;
      c <<= 1;
    }
  unsigned int rc = *p & ((1 << (7 - len)) - 1);
  for (size_t i = 1; i < len; i++)
    {
      unsigned int u = p[i];
      rc <<= 6;
      rc |= u & 0x3f;
    }
  *pc = rc;
  return len;
}

// Encode an identifier using assembler-friendly characters.  The
// encoding is described in detail near the end of the long comment at
// the start of names.cc.

std::string
go_encode_id(const std::string &id)
{
  if (Lex::is_invalid_identifier(id))
    {
      go_assert(saw_errors());
      return id;
    }

  std::string ret;
  const char* p = id.c_str();
  const char* pend = p + id.length();

  // We encode a leading digit, to ensure that no identifier starts
  // with a digit.
  if (pend > p && p[0] >= '0' && p[0] <= '9')
    {
      char buf[8];
      snprintf(buf, sizeof buf, "_x%02x", p[0]);
      ret.append(buf);
      ++p;
    }

  while (p < pend)
    {
      unsigned int c;
      size_t len = fetch_utf8_char(p, &c);
      if (len == 1)
	{
	  if (!char_needs_encoding(c))
	    ret.push_back(c);
	  else
	    {
	      char code = special_char_code.code_for(c);
	      if (code != 0)
		{
		  ret.push_back('_');
		  ret.push_back(code);
		}
	      else
		{
		  char buf[8];
		  snprintf(buf, sizeof buf, "_x%02x", c);
		  ret.append(buf);
		}
	    }
	}
      else
	{
	  char buf[16];
	  if (c < 0x10000)
	    snprintf(buf, sizeof buf, "_u%04x", c);
	  else
	    snprintf(buf, sizeof buf, "_U%08x", c);
	  ret.append(buf);
	}

      p += len;
    }

  return ret;
}

// Convert a hex digit string to a unicode codepoint. No checking
// to insure that the hex digit is meaningful.

static unsigned
hex_digits_to_unicode_codepoint(const char *digits, unsigned ndig)
{
  unsigned result = 0;
  for (unsigned i = 0; i < ndig; ++i) {
    result <<= 4;
    result |= Lex::hex_val(digits[i]);
  }
  return result;
}

// Decode/demangle a mangled string produced by go_encode_id(). Returns
// empty string if demangling process fails in some way.  At the moment
// this routine is unused; there is an equivalent routine in the runtime
// used for demangling symbols appearing in stack traces.

std::string
go_decode_id(const std::string &encoded)
{
  std::string ret;
  const char* p = encoded.c_str();
  const char* pend = p + encoded.length();
  const Location loc = Linemap::predeclared_location();

  while (p < pend)
    {
      if (*p != '_' || p + 1 == pend)
	{
	  ret.push_back(*p);
	  p++;
	  continue;
	}

      switch (p[1])
	{
	case '_':
	  ret.push_back('_');
	  p += 2;
	  break;
	case '0':
	  ret.push_back('.');
	  p += 2;
	  break;
	case '1':
	  ret.push_back('/');
	  p += 2;
	  break;
	case '2':
	  ret.push_back('*');
	  p += 2;
	  break;
	case '3':
	  ret.push_back(',');
	  p += 2;
	  break;
	case '4':
	  ret.push_back('{');
	  p += 2;
	  break;
	case '5':
	  ret.push_back('}');
	  p += 2;
	  break;
	case '6':
	  ret.push_back('[');
	  p += 2;
	  break;
	case '7':
	  ret.push_back(']');
	  p += 2;
	  break;
	case '8':
	  ret.push_back('(');
	  p += 2;
	  break;
	case '9':
	  ret.push_back(')');
	  p += 2;
	  break;
	case 'a':
	  ret.push_back('"');
	  p += 2;
	  break;
	case 'b':
	  ret.push_back(' ');
	  p += 2;
	  break;
	case 'c':
	  ret.push_back(';');
	  p += 2;
	  break;
        case 'x':
	  {
	    const char* digits = p + 2;
	    if (strlen(digits) < 2)
	      return "";
	    unsigned int rune = hex_digits_to_unicode_codepoint(digits, 2);
	    Lex::append_char(rune, true, &ret, loc);
	    p += 4;
	  }
	  break;
	case 'u':
	  {
	    const char* digits = p + 2;
	    if (strlen(digits) < 4)
	      return "";
	    unsigned int rune = hex_digits_to_unicode_codepoint(digits, 4);
	    Lex::append_char(rune, true, &ret, loc);
	    p += 6;
	  }
	  break;
	case 'U':
	  {
	    const char* digits = p + 2;
	    if (strlen(digits) < 8)
	      return "";
	    unsigned int rune = hex_digits_to_unicode_codepoint(digits, 8);
	    Lex::append_char(rune, true, &ret, loc);
	    p += 10;
	  }
	  break;
	default:
	  return "";
	}
    }

  return ret;
}

// Encode a struct field tag.  This is only used when we need to
// create a type descriptor for an anonymous struct type with field
// tags.  Underscore encoding will be applied to the returned string.
// The tag will appear between curly braces, so that is all we have to
// avoid.

std::string
go_mangle_struct_tag(const std::string& tag)
{
  std::string ret;
  const char* p = tag.c_str();
  const char* pend = p + tag.length();
  while (p < pend)
    {
      unsigned int c;
      size_t len = fetch_utf8_char(p, &c);
      if (len > 1)
	ret.append(p, len);
      else if (c != '{' && c != '}' && c != '\\')
	ret.push_back(c);
      else
	{
	  ret.push_back('\\');
	  ret.push_back(c);
	}
      p += len;
    }
  return ret;
}