Add ImmutableString to encapsulate some compiler strings
The new ImmutableString class is intended to be used instead of plain
const char pointers to pool-allocated or static memory.
It has the following advantages over using plain const char pointers:
1. It makes it clear when a string is guaranteed to be safe to pass
around inside the compiler.
2. It can be compared with a comparison operator rather than using
strcmp, which is easier to read.
3. It records the length of the stored string, which enables faster
copies and comparisons in some cases.
4. ImmutableStrings could be implicitly converted from std::strings
when a pool-allocated string is required. This is robust and
convenient.
C++17 has a similar class std::string_view, but our code style doesn't
allow it yet. We also couldn't use it as is if we require properties
1 and 4 from above, but would rather need to inherit or wrap it in a
custom class.
Eventually all current usage of TString could be replaced with
ImmutableString. For now, use it for unmangled built-in names.
TEST=angle_unittests
BUG=angleproject:2267
Change-Id: Id60c7b544032e06460e1b99837e429bc84dc4367
Reviewed-on: https://chromium-review.googlesource.com/881020
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/compiler/translator/SymbolTable.cpp b/src/compiler/translator/SymbolTable.cpp
index 53ba840..7548880 100644
--- a/src/compiler/translator/SymbolTable.cpp
+++ b/src/compiler/translator/SymbolTable.cpp
@@ -13,6 +13,7 @@
#include "compiler/translator/SymbolTable.h"
+#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/IntermNode.h"
#include <stdio.h>
@@ -54,11 +55,7 @@
std::set<std::string> mInvariantVaryings;
bool mGlobalInvariant;
- struct CharArrayComparator
- {
- bool operator()(const char *a, const char *b) const { return strcmp(a, b) < 0; }
- };
- std::set<const char *, CharArrayComparator> mUnmangledBuiltInNames;
+ std::set<ImmutableString> mUnmangledBuiltInNames;
};
bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
@@ -88,12 +85,12 @@
void TSymbolTable::TSymbolTableLevel::insertUnmangledBuiltInName(const char *name)
{
- mUnmangledBuiltInNames.insert(name);
+ mUnmangledBuiltInNames.insert(ImmutableString(name));
}
bool TSymbolTable::TSymbolTableLevel::hasUnmangledBuiltIn(const char *name) const
{
- return mUnmangledBuiltInNames.count(name) > 0;
+ return mUnmangledBuiltInNames.count(ImmutableString(name)) > 0;
}
void TSymbolTable::push()