diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-08-14 14:48:37 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-08-14 14:48:37 -0700 |
| commit | 9885c972a6bfa6f856e505cdd90d9b71fdbdadaf (patch) | |
| tree | 7314b26e21ded966b6a4fe2430f0421c0c0970bd /source/slang/slang.cpp | |
| parent | 7f57ea4ad86c2a3eb5a14fef458e711845c1f87e (diff) | |
Add an explicit `Name` type
Fixes #23
Up to this point, the compiler has used the ordinary `String` type to represent declaration names, which means a bunch of lookup structures throughout the compiler were string-to-whatever maps, which can reduce efficiency.
It also means that things like the `Token` type end up carying a `String` by value and paying for things like reference-counting.
This change adds a `Name` type that is used to represent names of variables, types, macros, etc.
Names are cached and unique'd globally for a session, and the string-to-name mapping gets done during lexing.
From that point on, most mapping is from pointers, which should make all the various table lookups faster.
More importantly (possibly), this brings us one step closer to being able to pool-allocate the AST nodes.
Diffstat (limited to 'source/slang/slang.cpp')
| -rw-r--r-- | source/slang/slang.cpp | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 2bacb28ca..f6e9bd70b 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -20,9 +20,13 @@ namespace Slang { Session::Session() { + // Initialize name pool + getNamePool()->setRootNamePool(getRootNamePool()); + // Initialize the lookup table of syntax classes: - #define SYNTAX_CLASS(NAME, BASE) mapNameToSyntaxClass.Add(#NAME, getClass<NAME>()); + #define SYNTAX_CLASS(NAME, BASE) \ + mapNameToSyntaxClass.Add(getNamePool()->getName(#NAME), getClass<NAME>()); #include "object-meta-begin.h" #include "syntax-base-defs.h" @@ -110,6 +114,8 @@ struct IncludeHandlerImpl : IncludeHandler CompileRequest::CompileRequest(Session* session) : mSession(session) { + getNamePool()->setRootNamePool(session->getRootNamePool()); + setSourceManager(&sourceManagerStorage); sourceManager->initialize(session->getBuiltinSourceManager()); @@ -378,7 +384,7 @@ int CompileRequest::addEntryPoint( { RefPtr<EntryPointRequest> entryPoint = new EntryPointRequest(); entryPoint->compileRequest = this; - entryPoint->name = name; + entryPoint->name = getNamePool()->getName(name); entryPoint->profile = entryPointProfile; entryPoint->translationUnitIndex = translationUnitIndex; @@ -391,7 +397,7 @@ int CompileRequest::addEntryPoint( } RefPtr<ModuleDecl> CompileRequest::loadModule( - String const& name, + Name* name, String const& path, String const& source, SourceLoc const&) @@ -462,15 +468,20 @@ void CompileRequest::handlePoundImport( // as the "name" when registering things, but this saves // us the trouble of trying to special-case things when // checking an `import` down the road. - mapNameToLoadedModules.Add(path, moduleDecl); + // + // Ideally we'd construct a suitable name by effectively + // running the name->path logic in reverse (e.g., replacing + // `-` with `_` and `/` with `.`). + Name* name = getNamePool()->getName(path); + mapNameToLoadedModules.Add(name, moduleDecl); mapPathToLoadedModule.Add(path, moduleDecl); loadedModulesList.Add(moduleDecl); } RefPtr<ModuleDecl> CompileRequest::findOrImportModule( - String const& name, - SourceLoc const& loc) + Name* name, + SourceLoc const& loc) { // Have we already loaded a module matching this name? // If so, return it. @@ -485,7 +496,7 @@ RefPtr<ModuleDecl> CompileRequest::findOrImportModule( // For example, `foo_bar` becomes `foo-bar.slang`. StringBuilder sb; - for (auto c : name) + for (auto c : getText(name)) { if (c == '_') c = '-'; @@ -541,8 +552,8 @@ RefPtr<ModuleDecl> CompileRequest::findOrImportModule( RefPtr<ModuleDecl> findOrImportModule( CompileRequest* request, - String const& name, - SourceLoc const& loc) + Name* name, + SourceLoc const& loc) { return request->findOrImportModule(name, loc); } |
