Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Public Attributes | List of all members
SkSL::SymbolTable Class Reference

#include <SkSLSymbolTable.h>

Public Member Functions

 SymbolTable (bool builtin)
 
 SymbolTable (SymbolTable *parent, bool builtin)
 
std::unique_ptr< SymbolTableinsertNewParent ()
 
const Symbolfind (std::string_view name) const
 
const SymbolfindBuiltinSymbol (std::string_view name) const
 
SymbolfindMutable (std::string_view name) const
 
std::unique_ptr< ExpressioninstantiateSymbolRef (const Context &context, std::string_view name, Position pos)
 
void renameSymbol (const Context &context, Symbol *symbol, std::string_view newName)
 
std::unique_ptr< SymbolremoveSymbol (const Symbol *symbol)
 
void moveSymbolTo (SymbolTable *otherTable, Symbol *sym, const Context &context)
 
bool isType (std::string_view name) const
 
bool isBuiltinType (std::string_view name) const
 
void addWithoutOwnershipOrDie (Symbol *symbol)
 
void addWithoutOwnership (const Context &context, Symbol *symbol)
 
template<typename T >
Tadd (const Context &context, std::unique_ptr< T > symbol)
 
template<typename T >
TaddOrDie (std::unique_ptr< T > symbol)
 
void injectWithoutOwnership (Symbol *symbol)
 
template<typename T >
Tinject (std::unique_ptr< T > symbol)
 
template<typename T >
TtakeOwnershipOfSymbol (std::unique_ptr< T > symbol)
 
const TypeaddArrayDimension (const Context &context, const Type *type, int arraySize)
 
template<typename Fn >
void foreach (Fn &&fn) const
 
bool wouldShadowSymbolsFrom (const SymbolTable *other) const
 
size_t count () const
 
bool isBuiltin () const
 
const std::string * takeOwnershipOfString (std::string n)
 
void markModuleBoundary ()
 

Public Attributes

SymbolTablefParent = nullptr
 
std::vector< std::unique_ptr< Symbol > > fOwnedSymbols
 

Detailed Description

Maps identifiers to symbols.

Definition at line 35 of file SkSLSymbolTable.h.

Constructor & Destructor Documentation

◆ SymbolTable() [1/2]

SkSL::SymbolTable::SymbolTable ( bool  builtin)
inlineexplicit

Definition at line 37 of file SkSLSymbolTable.h.

38 : fBuiltin(builtin) {}

◆ SymbolTable() [2/2]

SkSL::SymbolTable::SymbolTable ( SymbolTable parent,
bool  builtin 
)
inlineexplicit

Definition at line 40 of file SkSLSymbolTable.h.

41 : fParent(parent)
42 , fBuiltin(builtin) {}
SymbolTable * fParent

Member Function Documentation

◆ add()

template<typename T >
T * SkSL::SymbolTable::add ( const Context context,
std::unique_ptr< T symbol 
)
inline

Adds a symbol to this symbol table, conferring ownership. The symbol table will always be updated to reference the new symbol. If the symbol already exists, an error will be reported.

Definition at line 121 of file SkSLSymbolTable.h.

121 {
122 T* ptr = symbol.get();
123 this->addWithoutOwnership(context, this->takeOwnershipOfSymbol(std::move(symbol)));
124 return ptr;
125 }
void addWithoutOwnership(const Context &context, Symbol *symbol)
T * takeOwnershipOfSymbol(std::unique_ptr< T > symbol)
#define T
Definition: precompiler.cc:65

◆ addArrayDimension()

const Type * SkSL::SymbolTable::addArrayDimension ( const Context context,
const Type type,
int  arraySize 
)

Given type = float and arraySize = 5, creates the array type float[5] in the symbol table. The created array type is returned. If zero is passed, the base type is returned unchanged.

Definition at line 173 of file SkSLSymbolTable.cpp.

175 {
176 if (arraySize == 0) {
177 return type;
178 }
179 // If we are making an array of a builtin type, we add it as high as possible in the symbol
180 // table tree (at the module boundary), to enable additional reuse of the array-type.
181 if (fParent && !fAtModuleBoundary && !context.fConfig->fIsBuiltinCode && type->isBuiltin()) {
182 return fParent->addArrayDimension(context, type, arraySize);
183 }
184 // Reuse an existing array type with this name if one already exists in our symbol table.
185 std::string arrayName = type->getArrayName(arraySize);
186 if (const Symbol* existingType = this->find(arrayName)) {
187 return &existingType->as<Type>();
188 }
189 // Add a new array type to the symbol table.
190 const std::string* arrayNamePtr = this->takeOwnershipOfString(std::move(arrayName));
191 return this->add(context, Type::MakeArrayType(context, *arrayNamePtr, *type, arraySize));
192}
GLenum type
const Type * addArrayDimension(const Context &context, const Type *type, int arraySize)
const Symbol * find(std::string_view name) const
const std::string * takeOwnershipOfString(std::string n)
T * add(const Context &context, std::unique_ptr< T > symbol)
static std::unique_ptr< Type > MakeArrayType(const Context &context, std::string_view name, const Type &componentType, int columns)

◆ addOrDie()

template<typename T >
T * SkSL::SymbolTable::addOrDie ( std::unique_ptr< T symbol)
inline

Adds a symbol to this symbol table, conferring ownership. The symbol table will always be updated to reference the new symbol. If the symbol already exists, abort.

Definition at line 132 of file SkSLSymbolTable.h.

132 {
133 T* ptr = symbol.get();
134 this->addWithoutOwnershipOrDie(this->takeOwnershipOfSymbol(std::move(symbol)));
135 return ptr;
136 }
void addWithoutOwnershipOrDie(Symbol *symbol)

◆ addWithoutOwnership()

void SkSL::SymbolTable::addWithoutOwnership ( const Context context,
Symbol symbol 
)

Definition at line 123 of file SkSLSymbolTable.cpp.

123 {
124 if (!this->addWithoutOwnership(symbol)) {
125 context.fErrors->error(symbol->position(),
126 "symbol '" + std::string(symbol->name()) + "' was already defined");
127 }
128}

◆ addWithoutOwnershipOrDie()

void SkSL::SymbolTable::addWithoutOwnershipOrDie ( Symbol symbol)

Adds a symbol to this symbol table, without conferring ownership. The caller is responsible for keeping the Symbol alive throughout the lifetime of the program/module.

Definition at line 130 of file SkSLSymbolTable.cpp.

130 {
131 if (!this->addWithoutOwnership(symbol)) {
132 SK_ABORT("symbol '%.*s' was already defined",
133 (int)symbol->name().size(), symbol->name().data());
134 }
135}
#define SK_ABORT(message,...)
Definition: SkAssert.h:70

◆ count()

size_t SkSL::SymbolTable::count ( ) const
inline

Definition at line 183 of file SkSLSymbolTable.h.

183 {
184 return fSymbols.count();
185 }

◆ find()

const Symbol * SkSL::SymbolTable::find ( std::string_view  name) const
inline

Looks up the requested symbol and returns a const pointer.

Definition at line 54 of file SkSLSymbolTable.h.

54 {
55 return this->lookup(MakeSymbolKey(name));
56 }
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ findBuiltinSymbol()

const Symbol * SkSL::SymbolTable::findBuiltinSymbol ( std::string_view  name) const

Looks up the requested symbol, only searching the built-in symbol tables. Always const.

Definition at line 38 of file SkSLSymbolTable.cpp.

38 {
39 if (!this->isBuiltin()) {
40 return fParent ? fParent->findBuiltinSymbol(name) : nullptr;
41 }
42 return this->find(name);
43}
bool isBuiltin() const
const Symbol * findBuiltinSymbol(std::string_view name) const

◆ findMutable()

Symbol * SkSL::SymbolTable::findMutable ( std::string_view  name) const
inline

Looks up the requested symbol and returns a mutable pointer. Use caution–mutating a symbol will have program-wide impact, and built-in symbol tables must never be mutated.

Definition at line 67 of file SkSLSymbolTable.h.

67 {
68 return this->lookup(MakeSymbolKey(name));
69 }

◆ foreach()

template<typename Fn >
void SkSL::SymbolTable::foreach ( Fn &&  fn) const
inline

Definition at line 174 of file SkSLSymbolTable.h.

174 {
175 fSymbols.foreach(
176 [&fn](const SymbolKey& key, const Symbol* symbol) { fn(key.fName, symbol); });
177 }

◆ inject()

template<typename T >
T * SkSL::SymbolTable::inject ( std::unique_ptr< T symbol)
inline

Forces a symbol into this symbol table, conferring ownership. Replaces any existing symbol with the same name, if one exists.

Definition at line 149 of file SkSLSymbolTable.h.

149 {
150 T* ptr = symbol.get();
151 this->injectWithoutOwnership(this->takeOwnershipOfSymbol(std::move(symbol)));
152 return ptr;
153 }
void injectWithoutOwnership(Symbol *symbol)

◆ injectWithoutOwnership()

void SkSL::SymbolTable::injectWithoutOwnership ( Symbol symbol)

Forces a symbol into this symbol table, without conferring ownership. Replaces any existing symbol with the same name, if one exists.

Definition at line 168 of file SkSLSymbolTable.cpp.

168 {
169 auto key = MakeSymbolKey(symbol->name());
170 fSymbols[key] = symbol;
171}

◆ insertNewParent()

std::unique_ptr< SymbolTable > SkSL::SymbolTable::insertNewParent ( )

Creates a new, empty SymbolTable between this SymbolTable and its current parent. The new symbol table is returned, and is also accessible as this->fParent. The original parent is accessible as this->fParent->fParent.

Definition at line 20 of file SkSLSymbolTable.cpp.

20 {
21 auto newTable = std::make_unique<SymbolTable>(fParent, fBuiltin);
22 fParent = newTable.get();
23 return newTable;
24}

◆ instantiateSymbolRef()

std::unique_ptr< Expression > SkSL::SymbolTable::instantiateSymbolRef ( const Context context,
std::string_view  name,
Position  pos 
)

Looks up the requested symbol and instantiates an Expression reference to it; will return a VariableReference, TypeReference, FunctionReference, FieldAccess, or nullptr.

Definition at line 194 of file SkSLSymbolTable.cpp.

196 {
197 if (const Symbol* symbol = this->find(name)) {
198 return symbol->instantiate(context, pos);
199 }
200 context.fErrors->error(pos, "unknown identifier '" + std::string(name) + "'");
201 return nullptr;
202}
SkPoint pos

◆ isBuiltin()

bool SkSL::SymbolTable::isBuiltin ( ) const
inline

Returns true if this is a built-in SymbolTable.

Definition at line 188 of file SkSLSymbolTable.h.

188 {
189 return fBuiltin;
190 }

◆ isBuiltinType()

bool SkSL::SymbolTable::isBuiltinType ( std::string_view  name) const

Returns true if the name refers to a builtin type.

Definition at line 31 of file SkSLSymbolTable.cpp.

31 {
32 if (!this->isBuiltin()) {
34 }
35 return this->isType(name);
36}
bool isType(std::string_view name) const
bool isBuiltinType(std::string_view name) const

◆ isType()

bool SkSL::SymbolTable::isType ( std::string_view  name) const

Returns true if the name refers to a type (user or built-in) in the current symbol table.

Definition at line 26 of file SkSLSymbolTable.cpp.

26 {
27 const Symbol* symbol = this->find(name);
28 return symbol && symbol->is<Type>();
29}

◆ markModuleBoundary()

void SkSL::SymbolTable::markModuleBoundary ( )
inline

Indicates that this symbol table's parent is in a different module than this one.

Definition at line 197 of file SkSLSymbolTable.h.

197 {
198 fAtModuleBoundary = true;
199 }

◆ moveSymbolTo()

void SkSL::SymbolTable::moveSymbolTo ( SymbolTable otherTable,
Symbol sym,
const Context context 
)

Moves a symbol from this symbol table to another one. If this symbol table had ownership of the symbol, the ownership will be transferred as well. (If the symbol does not actually exist in this table at all, it will still be added to the other table.)

Definition at line 109 of file SkSLSymbolTable.cpp.

109 {
110 if (std::unique_ptr<Symbol> ownedSymbol = this->removeSymbol(sym)) {
111 otherTable->add(context, std::move(ownedSymbol));
112 } else {
113 otherTable->addWithoutOwnership(context, sym);
114 }
115}
std::unique_ptr< Symbol > removeSymbol(const Symbol *symbol)

◆ removeSymbol()

std::unique_ptr< Symbol > SkSL::SymbolTable::removeSymbol ( const Symbol symbol)

Removes a symbol from the symbol table. If this symbol table had ownership of the symbol, the symbol is returned (and can be deleted or reinserted as desired); if not, null is returned. In either event, the name will no longer correspond to the symbol.

Definition at line 93 of file SkSLSymbolTable.cpp.

93 {
94 // Remove the symbol from our symbol lookup table.
95 if (fSymbols.removeIfExists(MakeSymbolKey(symbol->name()))) {
96 // Transfer ownership of the symbol if we own it. (This will leave a nullptr behind in the
97 // `fOwnedSymbols` list, which should be harmless.)
98 for (std::unique_ptr<Symbol>& owned : fOwnedSymbols) {
99 if (symbol == owned.get()) {
100 return std::move(owned);
101 }
102 }
103 }
104
105 // We don't own the symbol after all.
106 return nullptr;
107}
std::vector< std::unique_ptr< Symbol > > fOwnedSymbols

◆ renameSymbol()

void SkSL::SymbolTable::renameSymbol ( const Context context,
Symbol symbol,
std::string_view  newName 
)

Assigns a new name to the passed-in symbol. The old name will continue to exist in the symbol table and point to the symbol.

Definition at line 78 of file SkSLSymbolTable.cpp.

78 {
79 if (symbol->is<FunctionDeclaration>()) {
80 // This is a function declaration, so we need to rename the entire overload set.
81 for (FunctionDeclaration* fn = &symbol->as<FunctionDeclaration>(); fn != nullptr;
82 fn = fn->mutableNextOverload()) {
83 fn->setName(newName);
84 }
85 } else {
86 // Other types of symbols don't allow multiple symbols with the same name.
87 symbol->setName(newName);
88 }
89
90 this->addWithoutOwnership(context, symbol);
91}

◆ takeOwnershipOfString()

const std::string * SkSL::SymbolTable::takeOwnershipOfString ( std::string  n)

Definition at line 117 of file SkSLSymbolTable.cpp.

117 {
118 fOwnedStrings.push_front(std::move(str));
119 // Because fOwnedStrings is a linked list, pointers to elements are stable.
120 return &fOwnedStrings.front();
121}

◆ takeOwnershipOfSymbol()

template<typename T >
T * SkSL::SymbolTable::takeOwnershipOfSymbol ( std::unique_ptr< T symbol)
inline

Confers ownership of a symbol without adding its name to the lookup table.

Definition at line 159 of file SkSLSymbolTable.h.

159 {
160 T* ptr = symbol.get();
161 fOwnedSymbols.push_back(std::move(symbol));
162 return ptr;
163 }

◆ wouldShadowSymbolsFrom()

bool SkSL::SymbolTable::wouldShadowSymbolsFrom ( const SymbolTable other) const

Definition at line 45 of file SkSLSymbolTable.cpp.

45 {
46 // We are checking two hash maps for overlap; we always iterate over the smaller one to minimize
47 // the total number of checks.
48 const SymbolTable* self = this;
49 if (self->count() > other->count()) {
50 std::swap(self, other);
51 }
52
53 bool foundShadow = false;
54
55 self->fSymbols.foreach([&](const SymbolKey& key, const Symbol* symbol) {
56 if (foundShadow) {
57 // We've already found a shadowed symbol; stop searching.
58 return;
59 }
60 if (other->fSymbols.find(key) != nullptr) {
61 foundShadow = true;
62 }
63 });
64
65 return foundShadow;
66}
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
SymbolTable(bool builtin)

Member Data Documentation

◆ fOwnedSymbols

std::vector<std::unique_ptr<Symbol> > SkSL::SymbolTable::fOwnedSymbols

Definition at line 203 of file SkSLSymbolTable.h.

◆ fParent

SymbolTable* SkSL::SymbolTable::fParent = nullptr

Definition at line 201 of file SkSLSymbolTable.h.


The documentation for this class was generated from the following files: