skia2/include/sksl/DSLSymbols.h
Ethan Nicholas a2d22b2e08 Broke DSLVar into separate subclasses
Previously, DSLVar represented local, global, and parameter variables.
This splits it into three separate subclasses.

In addition to just being a cleaner API in general, this also addresses
an issue we ran into with the upcoming DSLParser: previously, a global
DSLVar's storage was not set correctly until DeclareGlobal was called,
so an AddToSymbolTable call prior to DeclareGlobal would create the
SkSL variable with the wrong storage, causing spurious errors on
global-only modifiers. But holding off on the AddToSymbolTable tends to
break constructs like "int x = 0, y = x", so improving the API seemed
like the best way to address it.

Now that we have greater type safety around variables, we can
potentially avoid having to call AddToSymbolTable for DSLVar and
DSLGlobalVar altogether, since we know they are both supposed to end up
in the symbol table, but that isn't something I want to change in this
CL.

Change-Id: I5f390a7384ce0af6a2131d84f97fc5e5b318063f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/428576
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
2021-07-15 15:40:06 +00:00

73 lines
1.7 KiB
C++

/*
* 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.
*/
DSLPossibleExpression 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(DSLVarBase& 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