From b81ff3ef968d1cc4e954b31a1812b3c391d17b02 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 31 May 2019 13:17:34 -0400 Subject: 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. --- source/slang/slang-mangled-lexer.cpp | 184 +++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 source/slang/slang-mangled-lexer.cpp (limited to 'source/slang/slang-mangled-lexer.cpp') diff --git a/source/slang/slang-mangled-lexer.cpp b/source/slang/slang-mangled-lexer.cpp new file mode 100644 index 000000000..f1f5ec903 --- /dev/null +++ b/source/slang/slang-mangled-lexer.cpp @@ -0,0 +1,184 @@ +// slang-mangled-lexer.cpp +#include "slang-mangled-lexer.h" + +#include + +namespace Slang { + +UInt MangledLexer::readCount() +{ + int c = _peek(); + if (!_isDigit((char)c)) + { + SLANG_UNEXPECTED("bad name mangling"); + UNREACHABLE_RETURN(0); + } + _next(); + + if (c == '0') + return 0; + + UInt count = 0; + for (;;) + { + count = count * 10 + c - '0'; + c = _peek(); + if (!_isDigit((char)c)) + return count; + + _next(); + } +} + +void MangledLexer::readGenericParam() +{ + switch (_peek()) + { + case 'T': + case 'C': + _next(); + break; + + case 'v': + _next(); + readType(); + break; + + default: + SLANG_UNEXPECTED("bad name mangling"); + break; + } +} + +void MangledLexer::readGenericParams() +{ + _expect("g"); + UInt paramCount = readCount(); + for (UInt pp = 0; pp < paramCount; pp++) + { + readGenericParam(); + } +} + +void MangledLexer::readType() +{ + int c = _peek(); + switch (c) + { + case 'V': + case 'b': + case 'i': + case 'u': + case 'U': + case 'h': + case 'f': + case 'd': + _next(); + break; + + case 'v': + _next(); + readSimpleIntVal(); + readType(); + break; + + default: + readNamedType(); + break; + } +} + +void MangledLexer::readVal() +{ + switch (_peek()) + { + case 'k': + _next(); + readCount(); + break; + + case 'K': + _next(); + readRawStringSegment(); + break; + + default: + readType(); + break; + } + +} + +void MangledLexer::readGenericArgs() +{ + _expect("G"); + UInt argCount = readCount(); + for (UInt aa = 0; aa < argCount; aa++) + { + readGenericArg(); + } +} + +UnownedStringSlice MangledLexer::readSimpleName() +{ + UnownedStringSlice result; + for (;;) + { + int c = _peek(); + + if (c == 'g') + { + readGenericParams(); + continue; + } + else if (c == 'G') + { + readGenericArgs(); + continue; + } + else if (c == 'X') + { + readExtensionSpec(); + continue; + } + + if (!_isDigit((char)c)) + return result; + + // Read the length part + UInt count = readCount(); + if (count > UInt(m_end - m_cursor)) + { + SLANG_UNEXPECTED("bad name mangling"); + UNREACHABLE_RETURN(result); + } + + result = UnownedStringSlice(m_cursor, m_cursor + count); + m_cursor += count; + } +} + +UnownedStringSlice MangledLexer::readRawStringSegment() +{ + // Read the length part + UInt count = readCount(); + if (count > UInt(m_end - m_cursor)) + { + SLANG_UNEXPECTED("bad name mangling"); + UNREACHABLE_RETURN(UnownedStringSlice()); + } + + auto result = UnownedStringSlice(m_cursor, m_cursor + count); + m_cursor += count; + return result; +} + +UInt MangledLexer::readParamCount() +{ + _expect("p"); + UInt count = readCount(); + _expect("p"); + return count; +} + +} // namespace Slang -- cgit v1.2.3