summaryrefslogtreecommitdiff
path: root/source/slang/slang-mangled-lexer.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-05-31 13:17:34 -0400
committerTim Foley <tfoleyNV@users.noreply.github.com>2019-05-31 10:17:34 -0700
commitb81ff3ef968d1cc4e954b31a1812b3c391d17b02 (patch)
treed9669f736c3be30c569b1c0dbd0abfaca6e85a0c /source/slang/slang-mangled-lexer.h
parentd4924f5fc67f56b60d11381bf77d21bc01eb8763 (diff)
WIP: Support for other source target language (#971)
* WIP: Setting up C/Cpp source compilation targets. * WIP: Emitting C/CPP. * WIP: Split out SourceSink, and use it for source output on emit. * SourceSink -> SourceStream * * Made SourceStream use m_ prefixing of members. * Make all methods use lower camel * Removed methods from SourceStream interface that are not used externally (use _ prefixing) * Improvements to documentation * EmitContext is now effectively empty, so just use SharedEmitContext as EmitContext. * SharedEmitContext -> EmitContext * Methods to LowerCamel in emit.cpp * Split out EmitContext and ExtensionUsageTracker into separate files. * Split out EmitVisitor into slang-c-like-source-emitter files. * EmitVisitor -> CLikeSourceEmitter * Tidy up around CLikeSourceEmitter - simplify header. * Small tidy up - removing repeated comments that are in header. * Remove EmitContext paramter threading. * Small tidy up. Use prefixed macros for slang-c-like-source-emitter.h * Small tidy up in slang-c-like-source-emitter.cpp * First pass at splitting out UnmangleContext. * MangledNameParser -> MangledLexer. * WIP making EmitOp (EOp) enum available outside of cpp * Generating EmitOpInfo from macro. * Split out emit precedence handling. Don't use kOp_ style anymore, just use an array indexed by EmitOp. * Disable C simple test for now. * Keep g++/clang happy with token pasting. * Fix win32 narrowing warning.
Diffstat (limited to 'source/slang/slang-mangled-lexer.h')
-rw-r--r--source/slang/slang-mangled-lexer.h125
1 files changed, 125 insertions, 0 deletions
diff --git a/source/slang/slang-mangled-lexer.h b/source/slang/slang-mangled-lexer.h
new file mode 100644
index 000000000..4890ae80f
--- /dev/null
+++ b/source/slang/slang-mangled-lexer.h
@@ -0,0 +1,125 @@
+// slang-mangled-lexer.h
+#ifndef SLANG_MANGLED_LEXER_H_INCLUDED
+#define SLANG_MANGLED_LEXER_H_INCLUDED
+
+#include "../core/basic.h"
+
+#include "compiler.h"
+
+namespace Slang
+{
+
+/* A lexer like utility class used for decoding mangled names.
+Expects names to be correctly constructed - any errors will cause asserts/failures */
+class MangledLexer
+{
+public:
+ /// Reads a count at current position
+ UInt readCount();
+
+ void readGenericParam();
+
+ void readGenericParams();
+
+ SLANG_INLINE void readSimpleIntVal();
+
+ UnownedStringSlice readRawStringSegment();
+
+ void readNamedType();
+
+ void readType();
+
+ void readVal();
+
+ void readGenericArg() { readVal(); }
+
+ void readGenericArgs();
+
+ SLANG_INLINE void readExtensionSpec();
+
+ UnownedStringSlice readSimpleName();
+
+ UInt readParamCount();
+
+ /// Ctor
+ SLANG_FORCE_INLINE MangledLexer(String const& str);
+
+private:
+
+ // Call at the beginning of a mangled name,
+ // to strip off the main prefix
+ void _start() { _expect("_S"); }
+
+ static bool _isDigit(char c) { return (c >= '0') && (c <= '9'); }
+
+ /// Returns the character at the current position
+ char _peek() { return *m_cursor; }
+ // Returns the current character and moves to next character.
+ char _next() { return *m_cursor++; }
+
+ SLANG_INLINE void _expect(char c);
+
+ void _expect(char const* str)
+ {
+ while (char c = *str++)
+ _expect(c);
+ }
+
+ char const* m_cursor = nullptr;
+ char const* m_begin = nullptr;
+ char const* m_end = nullptr;
+};
+
+// -------------------------------------------------------------------------- -
+SLANG_FORCE_INLINE MangledLexer::MangledLexer(String const& str)
+ : m_cursor(str.begin())
+ , m_begin(str.begin())
+ , m_end(str.end())
+{
+ _start();
+}
+
+// ---------------------------------------------------------------------------
+SLANG_INLINE void MangledLexer::readSimpleIntVal()
+{
+ int c = _peek();
+ if (_isDigit((char)c))
+ {
+ _next();
+ }
+ else
+ {
+ readVal();
+ }
+}
+
+// ---------------------------------------------------------------------------
+SLANG_INLINE void MangledLexer::readNamedType()
+{
+ // TODO: handle types with more complicated names
+ readRawStringSegment();
+}
+
+// ---------------------------------------------------------------------------
+SLANG_INLINE void MangledLexer::readExtensionSpec()
+{
+ _expect("X");
+ readType();
+}
+
+// ---------------------------------------------------------------------------
+SLANG_INLINE void MangledLexer::_expect(char c)
+{
+ if (_peek() == c)
+ {
+ _next();
+ }
+ else
+ {
+ // ERROR!
+ SLANG_UNEXPECTED("mangled name error");
+ }
+}
+
+}
+#endif