diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-05-19 15:37:40 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-19 15:37:40 -0400 |
| commit | c54c957d2e647d2f9bfdc0bf31561fca5a02c5df (patch) | |
| tree | 8b54c10f40a005fdac0d9837254c430a837a0a3f | |
| parent | 1de14312917a0427a7a0858615b65261da60f748 (diff) | |
Reduce the size of Token (#1349)
* Token size on 64 bits is 24 bytes (from 40). On 32 bits is 16 bytes from 24.
* Added hasContent method to Token.
Some other small improvements around Token.
| -rw-r--r-- | source/slang/slang-check-shader.cpp | 2 | ||||
| -rw-r--r-- | source/slang/slang-diagnostics.cpp | 2 | ||||
| -rw-r--r-- | source/slang/slang-lexer.cpp | 28 | ||||
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 10 | ||||
| -rw-r--r-- | source/slang/slang-parameter-binding.cpp | 10 | ||||
| -rw-r--r-- | source/slang/slang-parser.cpp | 52 | ||||
| -rw-r--r-- | source/slang/slang-preprocessor.cpp | 22 | ||||
| -rw-r--r-- | source/slang/slang-reflection.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-token.cpp | 15 | ||||
| -rw-r--r-- | source/slang/slang-token.h | 117 | ||||
| -rw-r--r-- | tools/slang-cpp-extractor/slang-cpp-extractor-main.cpp | 92 | ||||
| -rw-r--r-- | tools/slang-profile/slang-profile-main.cpp | 7 |
12 files changed, 216 insertions, 145 deletions
diff --git a/source/slang/slang-check-shader.cpp b/source/slang/slang-check-shader.cpp index b8a2a1d5e..ad5b1332e 100644 --- a/source/slang/slang-check-shader.cpp +++ b/source/slang/slang-check-shader.cpp @@ -396,7 +396,7 @@ namespace Slang { const auto& semanticToken = semantic->name; - String lowerName = String(semanticToken.Content).toLower(); + String lowerName = String(semanticToken.getContent()).toLower(); if(lowerName == "sv_dispatchthreadid") { diff --git a/source/slang/slang-diagnostics.cpp b/source/slang/slang-diagnostics.cpp index 615312661..6e0cfba4d 100644 --- a/source/slang/slang-diagnostics.cpp +++ b/source/slang/slang-diagnostics.cpp @@ -70,7 +70,7 @@ void printDiagnosticArg(StringBuilder& sb, TokenType tokenType) void printDiagnosticArg(StringBuilder& sb, Token const& token) { - sb << token.Content; + sb << token.getContent(); } SourceLoc const& getDiagnosticPos(Token const& token) diff --git a/source/slang/slang-lexer.cpp b/source/slang/slang-lexer.cpp index e091de735..fe268223d 100644 --- a/source/slang/slang-lexer.cpp +++ b/source/slang/slang-lexer.cpp @@ -545,8 +545,10 @@ namespace Slang { IntegerLiteralValue value = 0; - char const* cursor = token.Content.begin(); - char const* end = token.Content.end(); + const UnownedStringSlice content = token.getContent(); + + char const* cursor = content.begin(); + char const* end = content.end(); int base = _readOptionalBase(&cursor); @@ -571,8 +573,10 @@ namespace Slang { FloatingPointLiteralValue value = 0; - char const* cursor = token.Content.begin(); - char const* end = token.Content.end(); + const UnownedStringSlice content = token.getContent(); + + char const* cursor = content.begin(); + char const* end = content.end(); int radix = _readOptionalBase(&cursor); @@ -756,8 +760,10 @@ namespace Slang SLANG_ASSERT(token.type == TokenType::StringLiteral || token.type == TokenType::CharLiteral); - char const* cursor = token.Content.begin(); - char const* end = token.Content.end(); + const UnownedStringSlice content = token.getContent(); + + char const* cursor = content.begin(); + char const* end = content.end(); SLANG_UNREFERENCED_VARIABLE(end); auto quote = *cursor++; @@ -879,13 +885,15 @@ namespace Slang String getFileNameTokenValue(Token const& token) { + const UnownedStringSlice content = token.getContent(); + // A file name usually doesn't process escape sequences // (this is import on Windows, where `\\` is a valid // path separator character). // Just trim off the first and last characters to remove the quotes // (whether they were `""` or `<>`. - return String(token.Content.begin() + 1, token.Content.end() - 1); + return String(content.begin() + 1, content.end() - 1); } @@ -1298,11 +1306,11 @@ namespace Slang } *dst++ = c; } - token.Content = UnownedStringSlice(startDst, dst); + token.setContent(UnownedStringSlice(startDst, dst)); } else { - token.Content = UnownedStringSlice(textBegin, textEnd); + token.setContent(UnownedStringSlice(textBegin, textEnd)); } } @@ -1312,7 +1320,7 @@ namespace Slang if (tokenType == TokenType::Identifier) { - token.ptrValue = m_namePool->getName(token.Content); + token.setName(m_namePool->getName(token.getContent())); } return token; diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index aaa9d1a4c..46afc77b7 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -5625,7 +5625,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> } else if(definitionToken.type == TokenType::Identifier) { - definition = definitionToken.Content; + definition = definitionToken.getContent(); } else { @@ -5636,7 +5636,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> auto& targetToken = targetMod->targetToken; if( targetToken.type != TokenType::Unknown ) { - targetName = targetToken.Content; + targetName = targetToken.getContent(); } builder->addTargetIntrinsicDecoration(irInst, targetName, definition.getUnownedSlice()); @@ -5723,7 +5723,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> // if(targetMod->targetToken.type == TokenType::Unknown) return; - else if(targetMod->targetToken.Content.getLength() == 0) + else if(!targetMod->targetToken.hasContent()) return; } } @@ -6156,7 +6156,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> // a specialized definition of the particular function for the given // target, and we need to reflect that at the IR level. - getBuilder()->addTargetDecoration(irFunc, targetMod->targetToken.Content); + getBuilder()->addTargetDecoration(irFunc, targetMod->targetToken.getContent()); } // If this declaration was marked as having a target-specific lowering @@ -6173,7 +6173,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> // for(auto extensionMod : decl->GetModifiersOfType<RequiredGLSLExtensionModifier>()) { - getBuilder()->addRequireGLSLExtensionDecoration(irFunc, extensionMod->extensionNameToken.Content); + getBuilder()->addRequireGLSLExtensionDecoration(irFunc, extensionMod->extensionNameToken.getContent()); } for(auto versionMod : decl->GetModifiersOfType<RequiredGLSLVersionModifier>()) { diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp index 353888b69..2ce7f5f2a 100644 --- a/source/slang/slang-parameter-binding.cpp +++ b/source/slang/slang-parameter-binding.cpp @@ -490,7 +490,7 @@ LayoutSemanticInfo ExtractLayoutSemanticInfo( info.index = 0; info.kind = LayoutResourceKind::None; - UnownedStringSlice registerName = semantic->registerName.Content; + UnownedStringSlice registerName = semantic->registerName.getContent(); if (registerName.getLength() == 0) return info; @@ -533,7 +533,7 @@ LayoutSemanticInfo ExtractLayoutSemanticInfo( UInt space = 0; if( auto registerSemantic = as<HLSLRegisterSemantic>(semantic) ) { - auto const& spaceName = registerSemantic->spaceName.Content; + auto const& spaceName = registerSemantic->spaceName.getContent(); if(spaceName.getLength() != 0) { UnownedStringSlice spaceSpelling; @@ -564,7 +564,7 @@ LayoutSemanticInfo ExtractLayoutSemanticInfo( } // TODO: handle component mask part of things... - if( semantic->componentMask.Content.getLength() != 0 ) + if( semantic->componentMask.hasContent()) { getSink(context)->diagnose(semantic->componentMask, Diagnostics::componentMaskNotSupported); } @@ -589,7 +589,7 @@ static bool findLayoutArg( { if( modifier ) { - *outVal = (UInt) strtoull(String(modifier->valToken.Content).getBuffer(), nullptr, 10); + *outVal = (UInt) strtoull(String(modifier->valToken.getContent()).getBuffer(), nullptr, 10); return true; } } @@ -1336,7 +1336,7 @@ struct SimpleSemanticInfo SimpleSemanticInfo decomposeSimpleSemantic( HLSLSimpleSemantic* semantic) { - auto composedName = semantic->name.Content; + auto composedName = semantic->name.getContent(); // look for a trailing sequence of decimal digits // at the end of the composed name diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index cdd573a97..8d4ba36c0 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -315,7 +315,7 @@ namespace Slang Token Parser::ReadToken(const char* expected) { if (tokenReader.peekTokenType() == TokenType::Identifier - && tokenReader.peekToken().Content == expected) + && tokenReader.peekToken().getContent() == expected) { isRecovering = false; return tokenReader.advanceToken(); @@ -334,7 +334,7 @@ namespace Slang // The token we expected? // Then exit recovery mode and pretend like all is well. if (tokenReader.peekTokenType() == TokenType::Identifier - && tokenReader.peekToken().Content == expected) + && tokenReader.peekToken().getContent() == expected) { isRecovering = false; return tokenReader.advanceToken(); @@ -530,7 +530,7 @@ namespace Slang r.advanceToken(); return r.peekTokenType() == TokenType::Identifier - && r.peekToken().Content == string; + && r.peekToken().getContent() == string; } bool Parser::LookAheadToken(TokenType type, int offset) @@ -680,7 +680,7 @@ namespace Slang { scopedIdentifierBuilder.Append('_'); } - scopedIdentifierBuilder.Append(firstIdentifier.Content); + scopedIdentifierBuilder.Append(firstIdentifier.getContent()); while (parser->tokenReader.peekTokenType() == TokenType::Scope) { @@ -688,7 +688,7 @@ namespace Slang scopedIdentifierBuilder.Append('_'); const Token nextIdentifier(parser->ReadToken(TokenType::Identifier)); - scopedIdentifierBuilder.Append(nextIdentifier.Content); + scopedIdentifierBuilder.Append(nextIdentifier.getContent()); } // Make a 'token' @@ -700,7 +700,7 @@ namespace Slang auto namePool = parser->getNamePool(); // Since it's an Identifier have to set the name. - token.ptrValue = namePool->getName(token.Content); + token.setName(namePool->getName(token.getContent())); return token; } @@ -954,7 +954,7 @@ namespace Slang while (AdvanceIf(parser, TokenType::Dot)) { sb << "/"; - sb << parser->ReadToken(TokenType::Identifier).Content; + sb << parser->ReadToken(TokenType::Identifier).getContent(); } moduleNameAndLoc.name = getName(parser, sb.ProduceString()); @@ -1003,7 +1003,7 @@ namespace Slang if (AdvanceIf(parser, TokenType::Colon)) { // Concat : onto ? - nameToken.Content = UnownedStringSlice::fromLiteral("?:"); + nameToken.setContent(UnownedStringSlice::fromLiteral("?:")); break; } ; // fall-thru @@ -1013,7 +1013,7 @@ namespace Slang } return NameLoc( - getName(parser, nameToken.Content), + getName(parser, nameToken.getContent()), nameToken.loc); } else @@ -2301,8 +2301,8 @@ namespace Slang addModifier(bufferVarDecl, reflectionNameModifier); // Both the buffer variable and its type need to have names generated - bufferVarDecl->nameAndLoc.name = generateName(parser, "parameterGroup_" + String(reflectionNameToken.Content)); - bufferDataTypeDecl->nameAndLoc.name = generateName(parser, "ParameterGroup_" + String(reflectionNameToken.Content)); + bufferVarDecl->nameAndLoc.name = generateName(parser, "parameterGroup_" + String(reflectionNameToken.getContent())); + bufferDataTypeDecl->nameAndLoc.name = generateName(parser, "ParameterGroup_" + String(reflectionNameToken.getContent())); addModifier(bufferDataTypeDecl, new ImplicitParameterGroupElementTypeModifier()); addModifier(bufferVarDecl, new ImplicitParameterGroupVariableModifier()); @@ -3893,7 +3893,7 @@ namespace Slang { case TokenType::QuestionMark: opToken = parser->ReadToken(); - opToken.Content = UnownedStringSlice::fromLiteral("?:"); + opToken.setContent(UnownedStringSlice::fromLiteral("?:")); break; default: @@ -3902,7 +3902,7 @@ namespace Slang } auto opExpr = new VarExpr(); - opExpr->name = getName(parser, opToken.Content); + opExpr->name = getName(parser, opToken.getContent()); opExpr->scope = parser->currentScope; opExpr->loc = opToken.loc; @@ -4249,7 +4249,7 @@ namespace Slang if ((!(maskedValue == 0 || maskedValue == mask)) && sink && token) { // Output a warning that number has been altered - sink->diagnose(*token, Diagnostics::integerLiteralTruncated, token->Content, BaseTypeInfo::asText(baseType), truncatedValue); + sink->diagnose(*token, Diagnostics::integerLiteralTruncated, token->getContent(), BaseTypeInfo::asText(baseType), truncatedValue); } value = truncatedValue; @@ -4516,12 +4516,12 @@ namespace Slang } case FloatFixKind::Zeroed: { - parser->sink->diagnose(token, Diagnostics::floatLiteralTooSmall, BaseTypeInfo::asText(suffixBaseType), token.Content, fixedValue); + parser->sink->diagnose(token, Diagnostics::floatLiteralTooSmall, BaseTypeInfo::asText(suffixBaseType), token.getContent(), fixedValue); break; } case FloatFixKind::Unrepresentable: { - parser->sink->diagnose(token, Diagnostics::floatLiteralUnrepresentable, BaseTypeInfo::asText(suffixBaseType), token.Content, fixedValue); + parser->sink->diagnose(token, Diagnostics::floatLiteralUnrepresentable, BaseTypeInfo::asText(suffixBaseType), token.getContent(), fixedValue); break; } } @@ -4893,17 +4893,17 @@ namespace Slang { if (AdvanceIf(parser, TokenType::OpSub)) { - modifier->op = IROp(-StringToInt(parser->ReadToken().Content)); + modifier->op = IROp(-StringToInt(parser->ReadToken().getContent())); } else if (parser->LookAheadToken(TokenType::IntegerLiteral)) { - modifier->op = IROp(StringToInt(parser->ReadToken().Content)); + modifier->op = IROp(StringToInt(parser->ReadToken().getContent())); } else { modifier->opToken = parser->ReadToken(TokenType::Identifier); - modifier->op = findIROp(modifier->opToken.Content); + modifier->op = findIROp(modifier->opToken.getContent()); if (modifier->op == kIROp_Invalid) { @@ -4984,7 +4984,7 @@ namespace Slang outToken = parser->ReadToken(); parser->ReadToken(TokenType::RParent); - UnownedStringSlice content = outToken.Content; + UnownedStringSlice content = outToken.getContent(); // We allow specified as major.minor or as a string (in quotes) switch (outToken.type) { @@ -5162,7 +5162,7 @@ namespace Slang { RefPtr<BuiltinTypeModifier> modifier = new BuiltinTypeModifier(); parser->ReadToken(TokenType::LParent); - modifier->tag = BaseType(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).Content)); + modifier->tag = BaseType(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).getContent())); parser->ReadToken(TokenType::RParent); return modifier; @@ -5172,10 +5172,10 @@ namespace Slang { RefPtr<MagicTypeModifier> modifier = new MagicTypeModifier(); parser->ReadToken(TokenType::LParent); - modifier->name = parser->ReadToken(TokenType::Identifier).Content; + modifier->name = parser->ReadToken(TokenType::Identifier).getContent(); if (AdvanceIf(parser, TokenType::Comma)) { - modifier->tag = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).Content)); + modifier->tag = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).getContent())); } parser->ReadToken(TokenType::RParent); @@ -5186,10 +5186,10 @@ namespace Slang { RefPtr<IntrinsicTypeModifier> modifier = new IntrinsicTypeModifier(); parser->ReadToken(TokenType::LParent); - modifier->irOp = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).Content)); + modifier->irOp = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).getContent())); while( AdvanceIf(parser, TokenType::Comma) ) { - auto operand = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).Content)); + auto operand = uint32_t(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).getContent())); modifier->irOperands.add(operand); } parser->ReadToken(TokenType::RParent); @@ -5203,7 +5203,7 @@ namespace Slang ConversionCost cost = kConversionCost_Default; if( AdvanceIf(parser, TokenType::LParent) ) { - cost = ConversionCost(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).Content)); + cost = ConversionCost(StringToInt(parser->ReadToken(TokenType::IntegerLiteral).getContent())); parser->ReadToken(TokenType::RParent); } modifier->cost = cost; diff --git a/source/slang/slang-preprocessor.cpp b/source/slang/slang-preprocessor.cpp index 2f0ee69a2..60af58df0 100644 --- a/source/slang/slang-preprocessor.cpp +++ b/source/slang/slang-preprocessor.cpp @@ -999,7 +999,7 @@ top: // We are pasting tokens, which could get messy StringBuilder sb; - sb << token.Content; + sb << token.getContent(); while (PeekRawTokenType(preprocessor) == TokenType::PoundPound) { @@ -1012,7 +1012,7 @@ top: // Read the next raw token (now that expansion has been triggered) Token nextToken = AdvanceRawToken(preprocessor); - sb << nextToken.Content; + sb << nextToken.getContent(); } // Now re-lex the input @@ -1106,9 +1106,9 @@ inline Token const& GetDirective(PreprocessorDirectiveContext* context) } // Get the name of the directive being parsed. -inline UnownedStringSlice const& GetDirectiveName(PreprocessorDirectiveContext* context) +inline UnownedStringSlice GetDirectiveName(PreprocessorDirectiveContext* context) { - return context->directiveToken.Content; + return context->directiveToken.getContent(); } // Get the location of the directive being parsed. @@ -1360,12 +1360,12 @@ static PreprocessorExpressionValue ParseAndEvaluateUnaryExpression(PreprocessorD } case TokenType::IntegerLiteral: - return StringToInt(AdvanceToken(context).Content); + return StringToInt(AdvanceToken(context).getContent()); case TokenType::Identifier: { Token token = AdvanceToken(context); - if (token.Content == "defined") + if (token.getContent() == "defined") { // handle `defined(someName)` @@ -1994,7 +1994,7 @@ static void HandleWarningDirective(PreprocessorDirectiveContext* context) Expect(context, TokenType::DirectiveMessage, Diagnostics::expectedTokenInPreprocessorDirective, &messageToken); // Report the custom error. - GetSink(context)->diagnose(GetDirectiveLoc(context), Diagnostics::userDefinedWarning, messageToken.Content); + GetSink(context)->diagnose(GetDirectiveLoc(context), Diagnostics::userDefinedWarning, messageToken.getContent()); } // Handle a `#error` directive @@ -2008,7 +2008,7 @@ static void HandleErrorDirective(PreprocessorDirectiveContext* context) Expect(context, TokenType::DirectiveMessage, Diagnostics::expectedTokenInPreprocessorDirective, &messageToken); // Report the custom error. - GetSink(context)->diagnose(GetDirectiveLoc(context), Diagnostics::userDefinedError, messageToken.Content); + GetSink(context)->diagnose(GetDirectiveLoc(context), Diagnostics::userDefinedError, messageToken.getContent()); } // Handle a `#line` directive @@ -2023,14 +2023,14 @@ static void HandleLineDirective(PreprocessorDirectiveContext* context) // `#line <integer-literal> ...` if (PeekTokenType(context) == TokenType::IntegerLiteral) { - line = StringToInt(AdvanceToken(context).Content); + line = StringToInt(AdvanceToken(context).getContent()); } // `#line` // `#line default` else if ( PeekTokenType(context) == TokenType::EndOfDirective || (PeekTokenType(context) == TokenType::Identifier - && PeekToken(context).Content == "default")) + && PeekToken(context).getContent() == "default")) { AdvanceToken(context); @@ -2064,7 +2064,7 @@ static void HandleLineDirective(PreprocessorDirectiveContext* context) { // Note(tfoley): GLSL allows the "source string" to be indicated by an integer // TODO(tfoley): Figure out a better way to handle this, if it matters - file = AdvanceToken(context).Content; + file = AdvanceToken(context).getContent(); } else { diff --git a/source/slang/slang-reflection.cpp b/source/slang/slang-reflection.cpp index a3cb88892..c0e83ccaf 100644 --- a/source/slang/slang-reflection.cpp +++ b/source/slang/slang-reflection.cpp @@ -183,8 +183,8 @@ SLANG_API const char* spReflectionUserAttribute_GetArgumentValueString(SlangRefl if (auto cexpr = as<StringLiteralExpr>(userAttr->args[index])) { if (bufLen) - *bufLen = cexpr->token.Content.getLength(); - return cexpr->token.Content.begin(); + *bufLen = cexpr->token.getContentLength(); + return cexpr->token.getContent().begin(); } return nullptr; } diff --git a/source/slang/slang-token.cpp b/source/slang/slang-token.cpp index a8238eb6c..a7f6d7d62 100644 --- a/source/slang/slang-token.cpp +++ b/source/slang/slang-token.cpp @@ -6,22 +6,7 @@ namespace Slang { -Name* Token::getName() const -{ - return getNameOrNull(); -} - -Name* Token::getNameOrNull() const -{ - switch (type) - { - default: - return nullptr; - case TokenType::Identifier: - return (Name*) ptrValue; - } -} char const* TokenTypeToString(TokenType type) { diff --git a/source/slang/slang-token.h b/source/slang/slang-token.h index 193b128fa..9697a5c2d 100644 --- a/source/slang/slang-token.h +++ b/source/slang/slang-token.h @@ -5,12 +5,13 @@ #include "../core/slang-basic.h" #include "slang-source-loc.h" +#include "slang-name.h" namespace Slang { class Name; -enum class TokenType +enum class TokenType : uint8_t { #define TOKEN(NAME, DESC) NAME, #include "slang-token-defs.h" @@ -18,48 +19,120 @@ enum class TokenType char const* TokenTypeToString(TokenType type); -enum TokenFlag : unsigned int +typedef uint8_t TokenFlags; +struct TokenFlag { - AtStartOfLine = 1 << 0, - AfterWhitespace = 1 << 1, - SuppressMacroExpansion = 1 << 2, - ScrubbingNeeded = 1 << 3, + enum Enum : TokenFlags + { + AtStartOfLine = 1 << 0, + AfterWhitespace = 1 << 1, + SuppressMacroExpansion = 1 << 2, + ScrubbingNeeded = 1 << 3, + Name = 1 << 4, ///< Determines if 'name' is set or 'chars' in the charsNameUnion + }; }; -typedef unsigned int TokenFlags; class Token { public: + TokenType type = TokenType::Unknown; TokenFlags flags = 0; SourceLoc loc; - void* ptrValue; + uint32_t charsCount = 0; ///< Amount of characters. Is set if name or not. - UnownedStringSlice Content; + union CharsNameUnion + { + const char* chars; + Name* name; + }; - Token() = default; + CharsNameUnion charsNameUnion; - Token( - TokenType typeIn, - const UnownedStringSlice & contentIn, - SourceLoc locIn, - TokenFlags flagsIn = 0) - : flags(flagsIn) - { - type = typeIn; - Content = contentIn; - loc = locIn; - ptrValue = nullptr; - } + bool hasContent() const { return charsCount > 0; } + Index getContentLength() const { return charsCount; } + + UnownedStringSlice getContent() const; + /// Set content + void setContent(const UnownedStringSlice& content); Name* getName() const; Name* getNameOrNull() const; SourceLoc getLoc() const { return loc; } + + /// Set the name + SLANG_FORCE_INLINE void setName(Name* inName); + + Token() + { + charsNameUnion.chars = nullptr; + } + + Token( + TokenType inType, + const UnownedStringSlice& inContent, + SourceLoc inLoc, + TokenFlags inFlags = 0) + : flags(inFlags) + { + SLANG_ASSERT((inFlags & TokenFlag::Name) == 0); + type = inType; + charsNameUnion.chars = inContent.begin(); + charsCount = uint32_t(inContent.getLength()); + loc = inLoc; + } + Token( + TokenType inType, + Name* name, + SourceLoc inLoc, + TokenFlags inFlags = 0) + { + SLANG_ASSERT(name); + type = inType; + flags = inFlags | TokenFlag::Name; + charsNameUnion.name = name; + charsCount = uint32_t(name->text.getLength()); + loc = inLoc; + } }; +// --------------------------------------------------------------------------- +SLANG_FORCE_INLINE UnownedStringSlice Token::getContent() const +{ + return (flags & TokenFlag::Name) ? charsNameUnion.name->text.getUnownedSlice() : UnownedStringSlice(charsNameUnion.chars, charsCount); +} + +// --------------------------------------------------------------------------- +SLANG_FORCE_INLINE Name* Token::getName() const +{ + return getNameOrNull(); +} + +// --------------------------------------------------------------------------- +SLANG_FORCE_INLINE Name* Token::getNameOrNull() const +{ + return (flags & TokenFlag::Name) ? charsNameUnion.name : nullptr; +} + +// --------------------------------------------------------------------------- +SLANG_FORCE_INLINE void Token::setContent(const UnownedStringSlice& content) +{ + flags &= ~TokenFlag::Name; + charsNameUnion.chars = content.begin(); + charsCount = uint32_t(content.getLength()); +} + +// --------------------------------------------------------------------------- +SLANG_FORCE_INLINE void Token::setName(Name* inName) +{ + SLANG_ASSERT(inName); + flags |= TokenFlag::Name; + charsNameUnion.name = inName; + charsCount = uint32_t(inName->text.getLength()); +} } // namespace Slang diff --git a/tools/slang-cpp-extractor/slang-cpp-extractor-main.cpp b/tools/slang-cpp-extractor/slang-cpp-extractor-main.cpp index 814e7ac0e..5118a3a9b 100644 --- a/tools/slang-cpp-extractor/slang-cpp-extractor-main.cpp +++ b/tools/slang-cpp-extractor/slang-cpp-extractor-main.cpp @@ -393,9 +393,11 @@ void Node::addChild(Node* child) child->m_parentScope = this; m_children.add(child); - if (child->m_name.Content.getLength()) + UnownedStringSlice content = child->m_name.getContent(); + + if (content.getLength()) { - m_childMap.Add(child->m_name.Content, child); + m_childMap.Add(content, child); } } @@ -441,10 +443,10 @@ static void _indent(Index indentCount, StringBuilder& out) void Node::dumpDerived(int indentCount, StringBuilder& out) { - if (isClassLike() && m_isReflected && m_name.Content.getLength() > 0) + if (isClassLike() && m_isReflected && m_name.hasContent()) { _indent(indentCount, out); - out << m_name.Content << "\n"; + out << m_name.getContent() << "\n"; } for (Node* derivedType : m_derivedTypes) @@ -465,9 +467,9 @@ void Node::dump(int indentCount, StringBuilder& out) } case Type::Namespace: { - if (m_name.Content.getLength()) + if (m_name.hasContent()) { - out << "namespace " << m_name.Content << " {\n"; + out << "namespace " << m_name.getContent() << " {\n"; } else { @@ -486,15 +488,15 @@ void Node::dump(int indentCount, StringBuilder& out) { out << " ("; } - out << m_name.Content; + out << m_name.getContent(); if (!m_isReflected) { out << ") "; } - if (m_super.Content.getLength()) + if (m_super.hasContent()) { - out << " : " << m_super.Content; + out << " : " << m_super.getContent(); } out << " {\n"; @@ -510,7 +512,7 @@ void Node::dump(int indentCount, StringBuilder& out) for (const Field& field : m_fields) { _indent(indentCount + 1, out); - out << field.type << " " << field.name.Content << "\n"; + out << field.type << " " << field.name.getContent() << "\n"; } _indent(indentCount, out); @@ -521,11 +523,11 @@ void Node::calcAbsoluteName(StringBuilder& outName) const { if (m_parentScope == nullptr) { - if (m_name.Content.getLength() == 0) + if (!m_name.hasContent()) { return; } - outName << m_name.Content; + outName << m_name.getContent(); } else { @@ -536,7 +538,7 @@ void Node::calcAbsoluteName(StringBuilder& outName) const } else { - outName << m_name.Content; + outName << m_name.getContent(); } } } @@ -850,7 +852,7 @@ bool CPPExtractor::advanceIfToken(TokenType type, Token* outToken) bool CPPExtractor::advanceIfMarker(Token* outToken) { const Token peekToken = m_reader.peekToken(); - if (peekToken.type == TokenType::Identifier && _isMarker(peekToken.Content)) + if (peekToken.type == TokenType::Identifier && _isMarker(peekToken.getContent())) { m_reader.advanceToken(); if (outToken) @@ -866,7 +868,7 @@ bool CPPExtractor::advanceIfStyle(IdentifierStyle style, Token* outToken) { if (m_reader.peekTokenType() == TokenType::Identifier) { - IdentifierStyle readStyle = m_identifierLookup->get(m_reader.peekToken().Content); + IdentifierStyle readStyle = m_identifierLookup->get(m_reader.peekToken().getContent()); if (readStyle == style) { Token token = m_reader.advanceToken(); @@ -900,17 +902,17 @@ SlangResult CPPExtractor::pushNode(Node* node) m_origin->addNode(node); } - if (node->m_name.Content.getLength()) + if (node->m_name.hasContent()) { // For anonymous namespace, we should look if we already have one and just reopen that. Doing so will mean will // find anonymous namespace clashes - if (Node* foundNode = m_currentNode->findChild(node->m_name.Content)) + if (Node* foundNode = m_currentNode->findChild(node->m_name.getContent())) { if (node->isClassLike()) { - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::typeAlreadyDeclared, node->m_name.Content); - m_sink->diagnose(foundNode->m_name, CPPDiagnostics::seeDeclarationOf, node->m_name.Content); + m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::typeAlreadyDeclared, node->m_name.getContent()); + m_sink->diagnose(foundNode->m_name, CPPDiagnostics::seeDeclarationOf, node->m_name.getContent()); return SLANG_FAIL; } @@ -1126,9 +1128,9 @@ SlangResult CPPExtractor::_maybeParseNode(Node::Type type) SLANG_RETURN_ON_FAIL(expect(TokenType::Identifier, &typeNameToken)); SLANG_RETURN_ON_FAIL(expect(TokenType::RParent)); - if (typeNameToken.Content != node->m_name.Content) + if (typeNameToken.getContent() != node->m_name.getContent()) { - m_sink->diagnose(typeNameToken, CPPDiagnostics::typeNameDoesntMatch, node->m_name.Content); + m_sink->diagnose(typeNameToken, CPPDiagnostics::typeNameDoesntMatch, node->m_name.getContent()); return SLANG_FAIL; } @@ -1256,7 +1258,7 @@ UnownedStringSlice CPPExtractor::_concatTokens(TokenReader::ParsingCursor start) while (!m_reader.isAtCursor(endCursor)) { const Token token = m_reader.advanceToken(); - buf << token.Content; + buf << token.getContent(); } return m_typePool->getSlice(m_typePool->add(buf)); @@ -1277,7 +1279,7 @@ SlangResult CPPExtractor::_maybeParseType(UnownedStringSlice& outType, Index& io return SLANG_FAIL; } - const IdentifierStyle style = m_identifierLookup->get(identifierToken.Content); + const IdentifierStyle style = m_identifierLookup->get(identifierToken.getContent()); if (hasFlag(style, IdentifierFlag::Keyword)) { return SLANG_FAIL; @@ -1536,7 +1538,7 @@ SlangResult CPPExtractor::parse(SourceFile* sourceFile, const Options* options) { case TokenType::Identifier: { - IdentifierStyle style = m_identifierLookup->get(m_reader.peekToken().Content); + IdentifierStyle style = m_identifierLookup->get(m_reader.peekToken().getContent()); switch (style) { @@ -1646,7 +1648,7 @@ SlangResult CPPExtractor::_calcDerivedTypesRec(Node* node) { if (node->isClassLike() && node->m_baseType == Node::BaseType::None) { - if (node->m_super.Content.getLength()) + if (node->m_super.hasContent()) { Node* parentScope = node->m_parentScope; if (parentScope == nullptr) @@ -1655,12 +1657,12 @@ SlangResult CPPExtractor::_calcDerivedTypesRec(Node* node) return SLANG_FAIL; } - Node* superType = parentScope->findChild(node->m_super.Content); + Node* superType = parentScope->findChild(node->m_super.getContent()); if (!superType) { if (node->m_isReflected) { - m_sink->diagnose(node->m_name, CPPDiagnostics::superTypeNotFound, node->m_name.Content); + m_sink->diagnose(node->m_name, CPPDiagnostics::superTypeNotFound, node->m_name.getContent()); return SLANG_FAIL; } } @@ -1668,7 +1670,7 @@ SlangResult CPPExtractor::_calcDerivedTypesRec(Node* node) { if (!superType->isClassLike()) { - m_sink->diagnose(node->m_name, CPPDiagnostics::superTypeNotAType, node->m_name.Content); + m_sink->diagnose(node->m_name, CPPDiagnostics::superTypeNotAType, node->m_name.getContent()); return SLANG_FAIL; } @@ -1864,12 +1866,12 @@ SlangResult CPPExtractorApp::calcDef(CPPExtractor& extractor, SourceOrigin* orig { if (node->isClassLike() && node->m_isReflected) { - if (node->m_marker.Content.indexOf(UnownedStringSlice::fromLiteral("ABSTRACT")) >= 0) + if (node->m_marker.getContent().indexOf(UnownedStringSlice::fromLiteral("ABSTRACT")) >= 0) { out << "ABSTRACT_"; } - out << "SYNTAX_CLASS(" << node->m_name.Content << ", " << node->m_super.Content << ")\n"; + out << "SYNTAX_CLASS(" << node->m_name.getContent() << ", " << node->m_super.getContent() << ")\n"; out << "END_SYNTAX_CLASS()\n\n"; } } @@ -1909,7 +1911,7 @@ SlangResult CPPExtractorApp::calcChildrenHeader(CPPExtractor& extractor, StringB node->getReflectedDerivedTypes(derivedTypes); // Define the derived types - out << "#define " << m_options.m_prefixMark << "CHILDREN_" << reflectTypeName << "_" << node->m_name.Content << "(x, param)"; + out << "#define " << m_options.m_prefixMark << "CHILDREN_" << reflectTypeName << "_" << node->m_name.getContent() << "(x, param)"; if (derivedTypes.getCount()) { @@ -1918,7 +1920,7 @@ SlangResult CPPExtractorApp::calcChildrenHeader(CPPExtractor& extractor, StringB { Node* derivedType = derivedTypes[j]; _indent(1, out); - out << m_options.m_prefixMark << "ALL_" << reflectTypeName << "_" << derivedType->m_name.Content << "(x, param)"; + out << m_options.m_prefixMark << "ALL_" << reflectTypeName << "_" << derivedType->m_name.getContent() << "(x, param)"; if (j < derivedTypes.getCount() - 1) { out << "\\\n"; @@ -1933,16 +1935,16 @@ SlangResult CPPExtractorApp::calcChildrenHeader(CPPExtractor& extractor, StringB for (Node* node : nodes) { // Define the derived types - out << "#define " << m_options.m_prefixMark << "ALL_" << reflectTypeName << "_" << node->m_name.Content << "(x, param) \\\n"; + out << "#define " << m_options.m_prefixMark << "ALL_" << reflectTypeName << "_" << node->m_name.getContent() << "(x, param) \\\n"; _indent(1, out); - out << m_options.m_prefixMark << reflectTypeName << "_" << node->m_name.Content << "(x, param)"; + out << m_options.m_prefixMark << reflectTypeName << "_" << node->m_name.getContent() << "(x, param)"; // If has derived types output them if (node->hasReflectedDerivedType()) { out << " \\\n"; _indent(1, out); - out << m_options.m_prefixMark << "CHILDREN_" << reflectTypeName << "_" << node->m_name.Content << "(x, param)"; + out << m_options.m_prefixMark << "CHILDREN_" << reflectTypeName << "_" << node->m_name.getContent() << "(x, param)"; } out << "\n\n"; } @@ -1979,7 +1981,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& for (Node* scopeNode : baseScopePath) { SLANG_ASSERT(scopeNode->m_type == Node::Type::Namespace); - out << "namespace " << scopeNode->m_name.Content << " {\n"; + out << "namespace " << scopeNode->m_name.getContent() << " {\n"; } List<Node*> nodes; @@ -1998,7 +2000,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& // Okay first we are going to output the enum values const Index depth = node->calcDerivedDepth() - 1; _indent(depth, out); - out << node->m_name.Content << " = " << typeIndex << ",\n"; + out << node->m_name.getContent() << " = " << typeIndex << ",\n"; typeIndex++; } @@ -2045,7 +2047,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& if (node->m_isReflected) { const char* type = (node->m_type == Node::Type::ClassType) ? "class" : "struct"; - out << type << " " << node->m_name.Content << ";\n"; + out << type << " " << node->m_name.getContent() << ";\n"; } } } @@ -2084,15 +2086,15 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& // Output all of the definitions for each type for (Node* node : nodes) { - out << "#define " << m_options.m_prefixMark << reflectTypeName << "_" << node->m_name.Content << "(x, param) "; + out << "#define " << m_options.m_prefixMark << reflectTypeName << "_" << node->m_name.getContent() << "(x, param) "; // Output the X macro part _indent(1, out); - out << "x(" << node->m_name.Content << ", "; + out << "x(" << node->m_name.getContent() << ", "; if (node->m_superNode) { - out << node->m_superNode->m_name.Content << ", "; + out << node->m_superNode->m_name.getContent() << ", "; } else { @@ -2107,7 +2109,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& Node* lastDerived = node->findLastDerived(); if (lastDerived) { - out << lastDerived->m_name.Content << ", "; + out << lastDerived->m_name.getContent() << ", "; } else { @@ -2115,7 +2117,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& } // Output any specifics of the markup - UnownedStringSlice marker = node->m_marker.Content; + UnownedStringSlice marker = node->m_marker.getContent(); // Need to extract the name if (marker.getLength() > m_options.m_prefixMark.getLength() + m_options.m_postfixMark.getLength()) { @@ -2148,7 +2150,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& for (Index j = baseScopePath.getCount() - 1; j >= 0; j--) { Node* scopeNode = baseScopePath[j]; - out << "} // namespace " << scopeNode->m_name.Content << "\n"; + out << "} // namespace " << scopeNode->m_name.getContent() << "\n"; } } @@ -2168,7 +2170,7 @@ SlangResult CPPExtractorApp::calcHeader(CPPExtractor& extractor, StringBuilder& } _indent(1, out); - out << "x(" << node->m_name.Content << ", param) \\\n"; + out << "x(" << node->m_name.getContent() << ", param) \\\n"; } out << "/* */\n\n"; } diff --git a/tools/slang-profile/slang-profile-main.cpp b/tools/slang-profile/slang-profile-main.cpp index 19fb36604..2bc97548f 100644 --- a/tools/slang-profile/slang-profile-main.cpp +++ b/tools/slang-profile/slang-profile-main.cpp @@ -19,8 +19,11 @@ SlangResult innerMain(int argc, char** argv) { const auto startTick = ProcessUtil::getClockTick(); - ComPtr<slang::IGlobalSession> slangSession; - slangSession.attach(spCreateSession(nullptr)); + for (Int i = 0; i < 32; ++i) + { + ComPtr<slang::IGlobalSession> slangSession; + slangSession.attach(spCreateSession(nullptr)); + } const auto endTick = ProcessUtil::getClockTick(); |
