aboutsummaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/go-encode-id.cc
diff options
context:
space:
mode:
authorThan McIntosh <thanm@google.com>2016-11-22 22:28:05 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2016-11-22 22:28:05 +0000
commitf3878205ddc2970e384ffaed3a95362763eee9fe (patch)
tree3f3e5efdd02fec9f6bb7a55785fb00da33b93a4a /gcc/go/gofrontend/go-encode-id.cc
parent7e98cccbd85a0bc5ec6b7edf0017d98c2e96aef5 (diff)
downloadgcc-f3878205ddc2970e384ffaed3a95362763eee9fe.zip
gcc-f3878205ddc2970e384ffaed3a95362763eee9fe.tar.gz
gcc-f3878205ddc2970e384ffaed3a95362763eee9fe.tar.bz2
compiler: relocate ID encoding utilities to gofrontend
Relocate the code that encodes/sanitizes identifiers to make them assembler-friendly, moving it from the back end to the front end; the decisions about when to encode an identifier and the calls to the encoding helpers now take place entirely in gofrontend. Reviewed-on: https://go-review.googlesource.com/33424 * go-gcc.cc (char_needs_encoding): Remove. (needs_encoding, fetch_utf8_char, encode_id): Remove. (Gcc_backend::global_variable): Add asm_name parameter. Don't compute asm_name here. (Gcc_backend::implicit_variable): Likewise. (Gcc_backend::implicit_variable_reference): Likewise. (Gcc_backend::immutable_struct): Likewise. (Gcc_backend::immutable_struct_reference): Likewise. * Make-lang.in (GO_OBJS): Add go/go-encode-id.o. From-SVN: r242726
Diffstat (limited to 'gcc/go/gofrontend/go-encode-id.cc')
-rw-r--r--gcc/go/gofrontend/go-encode-id.cc113
1 files changed, 113 insertions, 0 deletions
diff --git a/gcc/go/gofrontend/go-encode-id.cc b/gcc/go/gofrontend/go-encode-id.cc
new file mode 100644
index 0000000..978f208
--- /dev/null
+++ b/gcc/go/gofrontend/go-encode-id.cc
@@ -0,0 +1,113 @@
+// go-encode-id.cc -- Go identifier encoding 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-location.h"
+#include "go-linemap.h"
+#include "go-encode-id.h"
+
+// Return whether the character c is OK to use in the assembler.
+
+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':
+ case '_': case '.': case '$': case '/':
+ 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;
+}
+
+// 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 ASCII characters.
+
+std::string
+go_encode_id(const std::string &id)
+{
+ std::string ret;
+ const char* p = id.c_str();
+ const char* pend = p + id.length();
+ while (p < pend)
+ {
+ unsigned int c;
+ size_t len = fetch_utf8_char(p, &c);
+ if (len == 1 && !char_needs_encoding(c))
+ ret += c;
+ else
+ {
+ ret += "$U";
+ char buf[30];
+ snprintf(buf, sizeof buf, "%x", c);
+ ret += buf;
+ ret += "$";
+ }
+ p += len;
+ }
+ return ret;
+}
+
+std::string
+go_selectively_encode_id(const std::string &id)
+{
+ if (go_id_needs_encoding(id))
+ return go_encode_id(id);
+ return std::string();
+}