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

#include <SkSLLiteral.h>

Inheritance diagram for SkSL::Literal:
SkSL::Expression SkSL::IRNode SkSL::Poolable

Public Member Functions

 Literal (Position pos, double value, const Type *type)
 
float floatValue () const
 
SKSL_INT intValue () const
 
SKSL_INT boolValue () const
 
double value () const
 
std::string description (OperatorPrecedence) const override
 
ComparisonResult compareConstant (const Expression &other) const override
 
std::unique_ptr< Expressionclone (Position pos) const override
 
bool supportsConstantValues () const override
 
std::optional< double > getConstantValue (int n) const override
 
- Public Member Functions inherited from SkSL::Expression
 Expression (Position pos, Kind kind, const Type *type)
 
Kind kind () const
 
const Typetype () const
 
bool isAnyConstructor () const
 
bool isIntLiteral () const
 
bool isFloatLiteral () const
 
bool isBoolLiteral () const
 
AnyConstructorasAnyConstructor ()
 
const AnyConstructorasAnyConstructor () const
 
bool isIncomplete (const Context &context) const
 
virtual ComparisonResult compareConstant (const Expression &other) const
 
CoercionCost coercionCost (const Type &target) const
 
virtual bool supportsConstantValues () const
 
virtual std::optional< double > getConstantValue (int n) const
 
virtual std::unique_ptr< Expressionclone (Position pos) const =0
 
std::unique_ptr< Expressionclone () const
 
std::string description () const final
 
virtual std::string description (OperatorPrecedence parentPrecedence) const =0
 
- 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< LiteralMakeFloat (const Context &context, Position pos, float value)
 
static std::unique_ptr< LiteralMakeFloat (Position pos, float value, const Type *type)
 
static std::unique_ptr< LiteralMakeInt (const Context &context, Position pos, SKSL_INT value)
 
static std::unique_ptr< LiteralMakeInt (Position pos, SKSL_INT value, const Type *type)
 
static std::unique_ptr< LiteralMakeBool (const Context &context, Position pos, bool value)
 
static std::unique_ptr< LiteralMakeBool (Position pos, bool value, const Type *type)
 
static std::unique_ptr< LiteralMake (Position pos, double value, const Type *type)
 
- 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::kLiteral
 

Additional Inherited Members

- Public Types inherited from SkSL::Expression
enum class  ComparisonResult { kUnknown = -1 , kNotEqual , kEqual }
 
using Kind = ExpressionKind
 
- 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

A literal value. These can contain ints, floats, or booleans.

Definition at line 34 of file SkSLLiteral.h.

Constructor & Destructor Documentation

◆ Literal()

SkSL::Literal::Literal ( Position  pos,
double  value,
const Type type 
)
inline

Definition at line 38 of file SkSLLiteral.h.

39 : INHERITED(pos, kIRNodeKind, type)
40 , fValue(value) {}
SkPoint pos
const Type & type() const
double value() const
Definition: SkSLLiteral.h:107
static constexpr Kind kIRNodeKind
Definition: SkSLLiteral.h:36

Member Function Documentation

◆ boolValue()

SKSL_INT SkSL::Literal::boolValue ( ) const
inline

Definition at line 102 of file SkSLLiteral.h.

102 {
103 SkASSERT(this->type().isBoolean());
104 return (bool)fValue;
105 }
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ clone()

std::unique_ptr< Expression > SkSL::Literal::clone ( Position  pos) const
inlineoverridevirtual

Implements SkSL::Expression.

Definition at line 122 of file SkSLLiteral.h.

122 {
123 return std::make_unique<Literal>(pos, this->value(), &this->type());
124 }

◆ compareConstant()

ComparisonResult SkSL::Literal::compareConstant ( const Expression other) const
inlineoverridevirtual

Reimplemented from SkSL::Expression.

Definition at line 113 of file SkSLLiteral.h.

113 {
114 if (!other.is<Literal>() || this->type().numberKind() != other.type().numberKind()) {
116 }
117 return this->value() == other.as<Literal>().value()
120 }
Literal(Position pos, double value, const Type *type)
Definition: SkSLLiteral.h:38

◆ description()

std::string SkSL::Literal::description ( OperatorPrecedence  ) const
overridevirtual

Implements SkSL::Expression.

Definition at line 13 of file SkSLLiteral.cpp.

13 {
14 if (this->type().isBoolean()) {
15 return fValue ? "true" : "false";
16 }
17 if (this->type().isInteger()) {
18 return std::to_string(this->intValue());
19 }
20 return skstd::to_string(this->floatValue());
21}
float floatValue() const
Definition: SkSLLiteral.h:92
SKSL_INT intValue() const
Definition: SkSLLiteral.h:97
std::string to_string(float value)
Definition: SkSLString.cpp:50
static SkString to_string(int n)
Definition: nanobench.cpp:119

◆ floatValue()

float SkSL::Literal::floatValue ( ) const
inline

Definition at line 92 of file SkSLLiteral.h.

92 {
93 SkASSERT(this->type().isFloat());
94 return (SKSL_FLOAT)fValue;
95 }
float SKSL_FLOAT
Definition: SkSLDefines.h:17

◆ getConstantValue()

std::optional< double > SkSL::Literal::getConstantValue ( int  n) const
inlineoverridevirtual

Returns the n'th compile-time constant value within a literal or constructor. Use Type::slotCount to determine the number of slots within an expression. Slots which do not contain compile-time constant values will return nullopt. vec4(1, vec2(2), 3) contains four compile-time constants: (1, 2, 2, 3) mat2(f) contains four slots, and two are constant: (nullopt, 0, 0, nullopt) All classes which override this function must also implement supportsConstantValues.

Reimplemented from SkSL::Expression.

Definition at line 130 of file SkSLLiteral.h.

130 {
131 SkASSERT(n == 0);
132 return fValue;
133 }

◆ intValue()

SKSL_INT SkSL::Literal::intValue ( ) const
inline

Definition at line 97 of file SkSLLiteral.h.

97 {
98 SkASSERT(this->type().isInteger());
99 return (SKSL_INT)fValue;
100 }
int64_t SKSL_INT
Definition: SkSLDefines.h:16

◆ Make()

static std::unique_ptr< Literal > SkSL::Literal::Make ( Position  pos,
double  value,
const Type type 
)
inlinestatic

Definition at line 81 of file SkSLLiteral.h.

81 {
82 if (type->isFloat()) {
83 return MakeFloat(pos, value, type);
84 }
85 if (type->isInteger()) {
86 return MakeInt(pos, value, type);
87 }
89 return MakeBool(pos, value, type);
90 }
static std::unique_ptr< Literal > MakeInt(const Context &context, Position pos, SKSL_INT value)
Definition: SkSLLiteral.h:54
static std::unique_ptr< Literal > MakeBool(const Context &context, Position pos, bool value)
Definition: SkSLLiteral.h:69
static std::unique_ptr< Literal > MakeFloat(const Context &context, Position pos, float value)
Definition: SkSLLiteral.h:43
bool isBoolean() const
Definition: SkSLType.h:297
bool isFloat() const
Definition: SkSLType.h:318
bool isInteger() const
Definition: SkSLType.h:339

◆ MakeBool() [1/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeBool ( const Context context,
Position  pos,
bool  value 
)
inlinestatic

Definition at line 69 of file SkSLLiteral.h.

69 {
70 return std::make_unique<Literal>(pos, value, context.fTypes.fBool.get());
71 }

◆ MakeBool() [2/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeBool ( Position  pos,
bool  value,
const Type type 
)
inlinestatic

Definition at line 75 of file SkSLLiteral.h.

75 {
77 return std::make_unique<Literal>(pos, value, type);
78 }

◆ MakeFloat() [1/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeFloat ( const Context context,
Position  pos,
float  value 
)
inlinestatic

Definition at line 43 of file SkSLLiteral.h.

43 {
44 return std::make_unique<Literal>(pos, value, context.fTypes.fFloatLiteral.get());
45 }

◆ MakeFloat() [2/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeFloat ( Position  pos,
float  value,
const Type type 
)
inlinestatic

Definition at line 48 of file SkSLLiteral.h.

48 {
50 return std::make_unique<Literal>(pos, value, type);
51 }

◆ MakeInt() [1/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeInt ( const Context context,
Position  pos,
SKSL_INT  value 
)
inlinestatic

Definition at line 54 of file SkSLLiteral.h.

54 {
55 return std::make_unique<Literal>(pos, value, context.fTypes.fIntLiteral.get());
56 }

◆ MakeInt() [2/2]

static std::unique_ptr< Literal > SkSL::Literal::MakeInt ( Position  pos,
SKSL_INT  value,
const Type type 
)
inlinestatic

Definition at line 59 of file SkSLLiteral.h.

59 {
61 SkASSERTF(value >= type->minimumValue(), "Value %" PRId64 " does not fit in type %s",
62 value, type->description().c_str());
63 SkASSERTF(value <= type->maximumValue(), "Value %" PRId64 " does not fit in type %s",
64 value, type->description().c_str());
65 return std::make_unique<Literal>(pos, value, type);
66 }
#define SkASSERTF(cond, fmt,...)
Definition: SkAssert.h:117
std::string description() const override
Definition: SkSLType.h:238
virtual double minimumValue() const
Definition: SkSLType.h:444

◆ supportsConstantValues()

bool SkSL::Literal::supportsConstantValues ( ) const
inlineoverridevirtual

Returns true if this expression type supports getConstantValue. (This particular expression may or may not actually contain a constant value.) It's harmless to call getConstantValue on expressions which don't support constant values or don't contain any constant values, but if supportsConstantValues returns false, you can assume that getConstantValue will return nullopt for every slot of this expression. This allows for early-out opportunities in some cases. (Some expressions have tons of slots but never hold a constant value; e.g. a variable holding a very large array.)

Reimplemented from SkSL::Expression.

Definition at line 126 of file SkSLLiteral.h.

126 {
127 return true;
128 }

◆ value()

double SkSL::Literal::value ( ) const
inline

Definition at line 107 of file SkSLLiteral.h.

107 {
108 return fValue;
109 }

Member Data Documentation

◆ kIRNodeKind

constexpr Kind SkSL::Literal::kIRNodeKind = Kind::kLiteral
inlinestaticconstexpr

Definition at line 36 of file SkSLLiteral.h.


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