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

#include <SkSLVariable.h>

Inheritance diagram for SkSL::Variable:
SkSL::Symbol SkSL::IRNode SkSL::Poolable SkSL::ExtendedVariable

Classes

struct  ScratchVariable
 

Public Types

using Storage = VariableStorage
 
- Public Types inherited from SkSL::Symbol
using Kind = SymbolKind
 

Public Member Functions

 Variable (Position pos, Position modifiersPosition, ModifierFlags modifierFlags, std::string_view name, const Type *type, bool builtin, Storage storage)
 
 ~Variable () override
 
ModifierFlags modifierFlags () const
 
virtual const Layoutlayout () const
 
Position modifiersPosition () const
 
bool isBuiltin () const
 
Storage storage () const
 
const ExpressioninitialValue () const
 
VarDeclarationvarDeclaration () const
 
void setVarDeclaration (VarDeclaration *declaration)
 
GlobalVarDeclarationglobalVarDeclaration () const
 
void setGlobalVarDeclaration (GlobalVarDeclaration *global)
 
void detachDeadVarDeclaration ()
 
virtual InterfaceBlockinterfaceBlock () const
 
virtual void setInterfaceBlock (InterfaceBlock *)
 
virtual void detachDeadInterfaceBlock ()
 
virtual std::string_view mangledName () const
 
std::string description () const override
 
- Public Member Functions inherited from SkSL::Symbol
 Symbol (Position pos, Kind kind, std::string_view name, const Type *type=nullptr)
 
 ~Symbol () override
 
std::unique_ptr< Expressioninstantiate (const Context &context, Position pos) const
 
const Typetype () const
 
Kind kind () const
 
std::string_view name () const
 
void setName (std::string_view newName)
 
- Public Member Functions inherited from SkSL::IRNode
virtual ~IRNode ()
 
virtual std::string description () const =0
 
 IRNode (const IRNode &)=delete
 
IRNodeoperator= (const IRNode &)=delete
 
Position position () const
 
void setPosition (Position p)
 
template<typename T >
bool is () const
 
template<typename T >
const Tas () const
 
template<typename T >
Tas ()
 

Static Public Member Functions

static std::unique_ptr< VariableConvert (const Context &context, Position pos, Position modifiersPos, const Layout &layout, ModifierFlags flags, const Type *type, Position namePos, std::string_view name, Storage storage)
 
static std::unique_ptr< VariableMake (Position pos, Position modifiersPosition, const Layout &layout, ModifierFlags flags, const Type *type, std::string_view name, std::string mangledName, bool builtin, Storage storage)
 
static ScratchVariable MakeScratchVariable (const Context &context, Mangler &mangler, std::string_view baseName, const Type *type, SymbolTable *symbolTable, std::unique_ptr< Expression > initialValue)
 
- Static Public Member Functions inherited from SkSL::Poolable
static void * operator new (const size_t size)
 
static void operator delete (void *ptr)
 

Static Public Attributes

static constexpr Kind kIRNodeKind = Kind::kVariable
 

Additional Inherited Members

- Public Attributes inherited from SkSL::IRNode
Position fPosition
 
- Protected Member Functions inherited from SkSL::IRNode
 IRNode (Position position, int kind)
 
- Protected Attributes inherited from SkSL::IRNode
int fKind
 

Detailed Description

Represents a variable, whether local, global, or a function parameter. This represents the variable itself (the storage location), which is shared between all VariableReferences which read or write that storage location.

Definition at line 48 of file SkSLVariable.h.

Member Typedef Documentation

◆ Storage

Definition at line 50 of file SkSLVariable.h.

Constructor & Destructor Documentation

◆ Variable()

SkSL::Variable::Variable ( Position  pos,
Position  modifiersPosition,
ModifierFlags  modifierFlags,
std::string_view  name,
const Type type,
bool  builtin,
Storage  storage 
)
inline

Definition at line 54 of file SkSLVariable.h.

56 : INHERITED(pos, kIRNodeKind, name, type)
57 , fModifierFlags(modifierFlags)
58 , fModifiersPosition(modifiersPosition)
59 , fStorage(storage)
60 , fBuiltin(builtin) {}
SkPoint pos
std::string_view name() const
Definition: SkSLSymbol.h:51
const Type & type() const
Definition: SkSLSymbol.h:42
Storage storage() const
Definition: SkSLVariable.h:103
static constexpr Kind kIRNodeKind
Definition: SkSLVariable.h:52
Position modifiersPosition() const
Definition: SkSLVariable.h:95
ModifierFlags modifierFlags() const
Definition: SkSLVariable.h:89

◆ ~Variable()

SkSL::Variable::~Variable ( )
override

Definition at line 31 of file SkSLVariable.cpp.

31 {
32 // Unhook this Variable from its associated VarDeclaration, since we're being deleted.
33 if (VarDeclaration* declaration = this->varDeclaration()) {
34 declaration->detachDeadVariable();
35 }
36}
VarDeclaration * varDeclaration() const

Member Function Documentation

◆ Convert()

std::unique_ptr< Variable > SkSL::Variable::Convert ( const Context context,
Position  pos,
Position  modifiersPos,
const Layout layout,
ModifierFlags  flags,
const Type type,
Position  namePos,
std::string_view  name,
Storage  storage 
)
static

Definition at line 92 of file SkSLVariable.cpp.

100 {
101 if (layout.fLocation == 0 &&
102 layout.fIndex == 0 &&
104 ProgramConfig::IsFragment(context.fConfig->fKind) &&
106 context.fErrors->error(modifiersPos,
107 "out location=0, index=0 is reserved for sk_FragColor");
108 }
109 if (type->isUnsizedArray() && storage != Variable::Storage::kInterfaceBlock) {
110 context.fErrors->error(pos, "unsized arrays are not permitted here");
111 }
112 if (ProgramConfig::IsCompute(context.fConfig->fKind) && layout.fBuiltin == -1) {
114 if (flags & ModifierFlag::kIn) {
115 context.fErrors->error(pos, "pipeline inputs not permitted in compute shaders");
116 } else if (flags & ModifierFlag::kOut) {
117 context.fErrors->error(pos, "pipeline outputs not permitted in compute shaders");
118 }
119 }
120 }
121 if (storage == Variable::Storage::kParameter) {
122 // The `in` modifier on function parameters is implicit, so we can replace `in float x` with
123 // `float x`. This prevents any ambiguity when matching a function by its param types.
126 }
127 }
128
129 // Invent a mangled name for the variable, if it needs one.
130 std::string mangledName;
131 if (skstd::starts_with(name, '$')) {
132 // The $ prefix will fail to compile in GLSL, so replace it with `sk_Priv`.
133 mangledName = "sk_Priv" + std::string(name.substr(1));
134 } else if (FindIntrinsicKind(name) != kNotIntrinsic) {
135 // Having a variable name overlap an intrinsic name will prevent us from calling the
136 // intrinsic, but it's not illegal for user names to shadow a global symbol.
137 // Mangle the name to avoid a possible collision.
138 mangledName = Mangler{}.uniqueName(name, context.fSymbolTable);
139 }
140
141 return Make(pos, modifiersPos, layout, flags, type, name, std::move(mangledName),
142 context.fConfig->fIsBuiltinCode, storage);
143}
static constexpr const char FRAGCOLOR_NAME[]
Definition: SkSLCompiler.h:71
virtual bool isUnsizedArray() const
Definition: SkSLType.h:536
virtual std::string_view mangledName() const
Definition: SkSLVariable.h:132
static std::unique_ptr< Variable > Make(Position pos, Position modifiersPosition, const Layout &layout, ModifierFlags flags, const Type *type, std::string_view name, std::string mangledName, bool builtin, Storage storage)
virtual const Layout & layout() const
FlutterSemanticsFlag flags
IntrinsicKind FindIntrinsicKind(std::string_view functionName)
constexpr bool starts_with(std::string_view str, std::string_view prefix)
Definition: SkStringView.h:17
static bool IsFragment(ProgramKind kind)
static bool IsCompute(ProgramKind kind)

◆ description()

std::string SkSL::Variable::description ( ) const
inlineoverridevirtual

Implements SkSL::IRNode.

Definition at line 134 of file SkSLVariable.h.

134 {
135 return this->layout().paddedDescription() + this->modifierFlags().paddedDescription() +
136 this->type().displayName() + " " + std::string(this->name());
137 }
std::string paddedDescription() const
std::string displayName() const
Definition: SkSLType.h:234
std::string paddedDescription() const

◆ detachDeadInterfaceBlock()

virtual void SkSL::Variable::detachDeadInterfaceBlock ( )
inlinevirtual

Reimplemented in SkSL::ExtendedVariable.

Definition at line 129 of file SkSLVariable.h.

129{}

◆ detachDeadVarDeclaration()

void SkSL::Variable::detachDeadVarDeclaration ( )
inline

Definition at line 117 of file SkSLVariable.h.

117 {
118 // The VarDeclaration is being deleted, so our reference to it has become stale.
119 fDeclaringElement = nullptr;
120 }

◆ globalVarDeclaration()

GlobalVarDeclaration * SkSL::Variable::globalVarDeclaration ( ) const

Definition at line 61 of file SkSLVariable.cpp.

61 {
62 if (!fDeclaringElement) {
63 return nullptr;
64 }
65 SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
66 fDeclaringElement->is<GlobalVarDeclaration>());
67 return fDeclaringElement->is<GlobalVarDeclaration>()
68 ? &fDeclaringElement->as<GlobalVarDeclaration>()
69 : nullptr;
70}
#define SkASSERT(cond)
Definition: SkAssert.h:116
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133

◆ initialValue()

const Expression * SkSL::Variable::initialValue ( ) const

Definition at line 45 of file SkSLVariable.cpp.

45 {
46 VarDeclaration* declaration = this->varDeclaration();
47 return declaration ? declaration->value().get() : nullptr;
48}

◆ interfaceBlock()

virtual InterfaceBlock * SkSL::Variable::interfaceBlock ( ) const
inlinevirtual

Reimplemented in SkSL::ExtendedVariable.

Definition at line 125 of file SkSLVariable.h.

125{ return nullptr; }

◆ isBuiltin()

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

Definition at line 99 of file SkSLVariable.h.

99 {
100 return fBuiltin;
101 }

◆ layout()

const Layout & SkSL::Variable::layout ( ) const
virtual

Reimplemented in SkSL::ExtendedVariable.

Definition at line 84 of file SkSLVariable.cpp.

84 {
85 return kDefaultLayout;
86}
static constexpr Layout kDefaultLayout

◆ Make()

std::unique_ptr< Variable > SkSL::Variable::Make ( Position  pos,
Position  modifiersPosition,
const Layout layout,
ModifierFlags  flags,
const Type type,
std::string_view  name,
std::string  mangledName,
bool  builtin,
Variable::Storage  storage 
)
static

Definition at line 145 of file SkSLVariable.cpp.

153 {
154 // the `in` modifier on function parameters is implicit and should have been removed
155 SkASSERT(!(storage == Variable::Storage::kParameter &&
157
158 if (type->componentType().isInterfaceBlock() || !mangledName.empty() ||
160 return std::make_unique<ExtendedVariable>(pos,
162 layout,
163 flags,
164 name,
165 type,
166 builtin,
167 storage,
168 std::move(mangledName));
169 } else {
170 return std::make_unique<Variable>(pos,
172 flags,
173 name,
174 type,
175 builtin,
176 storage);
177 }
178}
virtual const Type & componentType() const
Definition: SkSLType.h:404
virtual bool isInterfaceBlock() const
Definition: SkSLType.h:544

◆ MakeScratchVariable()

Variable::ScratchVariable SkSL::Variable::MakeScratchVariable ( const Context context,
Mangler mangler,
std::string_view  baseName,
const Type type,
SymbolTable symbolTable,
std::unique_ptr< Expression initialValue 
)
static

Definition at line 180 of file SkSLVariable.cpp.

185 {
186 // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
187 // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
188 // somewhere during compilation.
189 if (type->isLiteral()) {
190 SkDEBUGFAIL("found a $literal type in MakeScratchVariable");
192 }
193
194 // Provide our new variable with a unique name, and add it to our symbol table.
195 const std::string* name =
196 symbolTable->takeOwnershipOfString(mangler.uniqueName(baseName, symbolTable));
197
198 // Create our new variable and add it to the symbol table.
199 ScratchVariable result;
200 auto var = std::make_unique<Variable>(initialValue ? initialValue->fPosition : Position(),
201 /*modifiersPosition=*/Position(),
203 name->c_str(),
204 type,
205 symbolTable->isBuiltin(),
207
208 // If we are creating an array type, reduce it to base type plus array-size.
209 int arraySize = 0;
210 if (type->isArray()) {
211 arraySize = type->columns();
212 type = &type->componentType();
213 }
214 // Create our variable declaration.
215 result.fVarDecl = VarDeclaration::Make(context, var.get(), type, arraySize,
216 std::move(initialValue));
217 result.fVarSymbol = symbolTable->add(context, std::move(var));
218 return result;
219}
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118
Position fPosition
Definition: SkSLIRNode.h:109
virtual bool isArray() const
Definition: SkSLType.h:532
virtual bool isLiteral() const
Definition: SkSLType.h:516
virtual int columns() const
Definition: SkSLType.h:429
virtual bool isBuiltin() const
Definition: SkSLType.h:230
virtual const Type & scalarTypeForLiteral() const
Definition: SkSLType.h:520
static std::unique_ptr< VarDeclaration > Make(const Context &context, Variable *var, const Type *baseType, int arraySize, std::unique_ptr< Expression > value)
const Expression * initialValue() const
GAsyncResult * result

◆ mangledName()

virtual std::string_view SkSL::Variable::mangledName ( ) const
inlinevirtual

Reimplemented in SkSL::ExtendedVariable.

Definition at line 132 of file SkSLVariable.h.

132{ return this->name(); }

◆ modifierFlags()

ModifierFlags SkSL::Variable::modifierFlags ( ) const
inline

Definition at line 89 of file SkSLVariable.h.

89 {
90 return fModifierFlags;
91 }

◆ modifiersPosition()

Position SkSL::Variable::modifiersPosition ( ) const
inline

Definition at line 95 of file SkSLVariable.h.

95 {
96 return fModifiersPosition;
97 }

◆ setGlobalVarDeclaration()

void SkSL::Variable::setGlobalVarDeclaration ( GlobalVarDeclaration global)

Definition at line 79 of file SkSLVariable.cpp.

79 {
80 SkASSERT(!fDeclaringElement || this == global->varDeclaration().var());
81 fDeclaringElement = global;
82}

◆ setInterfaceBlock()

virtual void SkSL::Variable::setInterfaceBlock ( InterfaceBlock )
inlinevirtual

Reimplemented in SkSL::ExtendedVariable.

Definition at line 127 of file SkSLVariable.h.

127{ SkUNREACHABLE; }
#define SkUNREACHABLE
Definition: SkAssert.h:135

◆ setVarDeclaration()

void SkSL::Variable::setVarDeclaration ( VarDeclaration declaration)

Definition at line 72 of file SkSLVariable.cpp.

72 {
73 SkASSERT(!fDeclaringElement || this == declaration->var());
74 if (!fDeclaringElement) {
75 fDeclaringElement = declaration;
76 }
77}

◆ storage()

Storage SkSL::Variable::storage ( ) const
inline

Definition at line 103 of file SkSLVariable.h.

103 {
104 return fStorage;
105 }

◆ varDeclaration()

VarDeclaration * SkSL::Variable::varDeclaration ( ) const

Definition at line 50 of file SkSLVariable.cpp.

50 {
51 if (!fDeclaringElement) {
52 return nullptr;
53 }
54 SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
55 fDeclaringElement->is<GlobalVarDeclaration>());
56 return fDeclaringElement->is<GlobalVarDeclaration>()
57 ? &fDeclaringElement->as<GlobalVarDeclaration>().varDeclaration()
58 : &fDeclaringElement->as<VarDeclaration>();
59}

Member Data Documentation

◆ kIRNodeKind

constexpr Kind SkSL::Variable::kIRNodeKind = Kind::kVariable
inlinestaticconstexpr

Definition at line 52 of file SkSLVariable.h.


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