Added DSLSymbols

These are new APIs to manage symbols and symbol tables from DSL code,
needed for the upcoming DSLParser.

Change-Id: Ic8d86aebfbcdeb84bd872d8727cdfafbb8db2fd6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/414907
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2021-06-10 12:07:34 -04:00 committed by Skia Commit-Bot
parent d2e0960696
commit 2919bbdab8
3 changed files with 122 additions and 0 deletions

View File

@ -29,6 +29,7 @@ skia_sksl_sources = [
"$_include/sksl/DSLModifiers.h",
"$_include/sksl/DSLRuntimeEffects.h",
"$_include/sksl/DSLStatement.h",
"$_include/sksl/DSLSymbols.h",
"$_include/sksl/DSLType.h",
"$_include/sksl/DSLVar.h",
"$_src/sksl/SkSLASTFile.h",
@ -86,6 +87,7 @@ skia_sksl_sources = [
"$_src/sksl/dsl/DSLLayout.cpp",
"$_src/sksl/dsl/DSLRuntimeEffects.cpp",
"$_src/sksl/dsl/DSLStatement.cpp",
"$_src/sksl/dsl/DSLSymbols.cpp",
"$_src/sksl/dsl/DSLType.cpp",
"$_src/sksl/dsl/DSLVar.cpp",
"$_src/sksl/dsl/priv/DSLFPs.cpp",

72
include/sksl/DSLSymbols.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKSL_DSL_SYMBOLS
#define SKSL_DSL_SYMBOLS
#include "include/core/SkStringView.h"
#include "include/private/SkSLString.h"
#include "include/sksl/DSLExpression.h"
#include <memory>
namespace SkSL {
class SymbolTable;
namespace dsl {
class DSLVar;
// This header provides methods for manually managing symbol tables in DSL code. They should not be
// used by normal hand-written DSL code, where we rely on C++ to manage symbols, but are instead
// needed when DSL objects are being constructed programmatically (as in DSLParser).
/**
* Pushes a new symbol table onto the symbol table stack.
*/
void PushSymbolTable();
/**
* Pops the top symbol table from the stack. As symbol tables are shared pointers, this will only
* destroy the symbol table if it was never attached to anything (e.g. passed into a Block
* constructor).
*/
void PopSymbolTable();
/**
* Returns the current symbol table. Outside of SkSL itself, this is an opaque pointer, used only
* for passing it to DSL methods that require it.
*/
std::shared_ptr<SymbolTable> CurrentSymbolTable();
/**
* Returns an expression referring to the named symbol.
*/
DSLExpression Symbol(skstd::string_view name);
/**
* Returns true if the name refers to a type.
*/
bool IsType(skstd::string_view name);
/**
* Adds a variable to the current symbol table.
*/
void AddToSymbolTable(DSLVar& var);
/**
* Attaches a String to the current symbol table. The String will persist as long as the symbol
* table remains in memory.
*/
const String* Retain(String s);
} // namespace dsl
} // namespace SkSL
#endif

View File

@ -0,0 +1,48 @@
/*
* Copyright 2021 Google LLC.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/sksl/DSLSymbols.h"
#include "src/sksl/SkSLIRGenerator.h"
#include "src/sksl/dsl/priv/DSLWriter.h"
namespace SkSL {
namespace dsl {
void PushSymbolTable() {
DSLWriter::IRGenerator().pushSymbolTable();
}
void PopSymbolTable() {
DSLWriter::IRGenerator().popSymbolTable();
}
std::shared_ptr<SymbolTable> CurrentSymbolTable() {
return DSLWriter::IRGenerator().symbolTable();
}
DSLExpression Symbol(skstd::string_view name) {
return DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, name);
}
bool IsType(skstd::string_view name) {
const SkSL::Symbol* s = (*CurrentSymbolTable())[name];
return s && s->is<Type>();
}
void AddToSymbolTable(DSLVar& var) {
CurrentSymbolTable()->addWithoutOwnership(&DSLWriter::Var(var));
}
const String* Retain(String string) {
return CurrentSymbolTable()->takeOwnershipOfString(std::move(string));
}
} // namespace dsl
} // namespace SkSL