* auto-profile.c (afdo_annotate_cfg): Use update_max_bb_count.
[official-gcc.git] / gcc / go / gofrontend / go-encode-id.cc
blob978f20823d69ed80da285768a2c149181b08f63f
1 // go-encode-id.cc -- Go identifier encoding hooks
3 // Copyright 2016 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.
7 #include "go-location.h"
8 #include "go-linemap.h"
9 #include "go-encode-id.h"
11 // Return whether the character c is OK to use in the assembler.
13 static bool
14 char_needs_encoding(char c)
16 switch (c)
18 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
19 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
20 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
21 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
22 case 'Y': case 'Z':
23 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
24 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
25 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
26 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
27 case 'y': case 'z':
28 case '0': case '1': case '2': case '3': case '4':
29 case '5': case '6': case '7': case '8': case '9':
30 case '_': case '.': case '$': case '/':
31 return false;
32 default:
33 return true;
37 // Return whether the identifier needs to be translated because it
38 // contains non-ASCII characters.
40 bool
41 go_id_needs_encoding(const std::string& str)
43 for (std::string::const_iterator p = str.begin();
44 p != str.end();
45 ++p)
46 if (char_needs_encoding(*p))
47 return true;
48 return false;
51 // Pull the next UTF-8 character out of P and store it in *PC. Return
52 // the number of bytes read.
54 static size_t
55 fetch_utf8_char(const char* p, unsigned int* pc)
57 unsigned char c = *p;
58 if ((c & 0x80) == 0)
60 *pc = c;
61 return 1;
63 size_t len = 0;
64 while ((c & 0x80) != 0)
66 ++len;
67 c <<= 1;
69 unsigned int rc = *p & ((1 << (7 - len)) - 1);
70 for (size_t i = 1; i < len; i++)
72 unsigned int u = p[i];
73 rc <<= 6;
74 rc |= u & 0x3f;
76 *pc = rc;
77 return len;
80 // Encode an identifier using ASCII characters.
82 std::string
83 go_encode_id(const std::string &id)
85 std::string ret;
86 const char* p = id.c_str();
87 const char* pend = p + id.length();
88 while (p < pend)
90 unsigned int c;
91 size_t len = fetch_utf8_char(p, &c);
92 if (len == 1 && !char_needs_encoding(c))
93 ret += c;
94 else
96 ret += "$U";
97 char buf[30];
98 snprintf(buf, sizeof buf, "%x", c);
99 ret += buf;
100 ret += "$";
102 p += len;
104 return ret;
107 std::string
108 go_selectively_encode_id(const std::string &id)
110 if (go_id_needs_encoding(id))
111 return go_encode_id(id);
112 return std::string();