MaterialX 1.39.3
Loading...
Searching...
No Matches
Syntax.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_SYNTAX_H
7#define MATERIALX_SYNTAX_H
8
11
14
17#include <MaterialXCore/Value.h>
18
19MATERIALX_NAMESPACE_BEGIN
20
21class Syntax;
22class TypeSyntax;
24class TypeDesc;
25class ShaderPort;
26
28using SyntaxPtr = shared_ptr<Syntax>;
30using ConstSyntaxPtr = shared_ptr<const Syntax>;
32using TypeSyntaxPtr = shared_ptr<TypeSyntax>;
34using StructTypeSyntaxPtr = shared_ptr<StructTypeSyntax>;
35
38using IdentifierMap = std::unordered_map<string, size_t>;
39
43class MX_GENSHADER_API Syntax
44{
45 public:
48 {
49 PARENTHESES,
50 CURLY_BRACKETS,
51 SQUARE_BRACKETS,
52 DOUBLE_SQUARE_BRACKETS
53 };
54
55 public:
56 virtual ~Syntax() { }
57
61 [[deprecated]] void registerTypeSyntax(const TypeDesc* type, TypeSyntaxPtr syntax) { registerTypeSyntax(*type, syntax); }
62
66 void registerReservedWords(const StringSet& names);
67
71 void registerInvalidTokens(const StringMap& tokens);
72
74 const StringSet& getReservedWords() const { return _reservedWords; }
75
77 const StringMap& getInvalidTokens() const { return _invalidTokens; }
78
81 const TypeSyntax& getTypeSyntax(TypeDesc type) const;
82 [[deprecated]] const TypeSyntax& getTypeSyntax(const TypeDesc* type) const { return getTypeSyntax(*type); }
83
85 const vector<TypeSyntaxPtr>& getTypeSyntaxes() const { return _typeSyntaxes; }
86
88 TypeDesc getType(const string& name) const { return _typeSystem->getType(name); }
89
91 const string& getTypeName(TypeDesc type) const;
92 [[deprecated]] const string& getTypeName(const TypeDesc* type) const { return getTypeName(*type); }
93
95 virtual string getOutputTypeName(TypeDesc type) const;
96 [[deprecated]] string getOutputTypeName(const TypeDesc* type) const { return getOutputTypeName(*type); }
97
100 const string& getTypeAlias(TypeDesc type) const;
101 [[deprecated]] const string& getTypeAlias(const TypeDesc* type) const { return getTypeAlias(*type); }
102
105 const string& getTypeDefinition(TypeDesc type) const;
106 [[deprecated]] const string& getTypeDefinition(const TypeDesc* type) const { return getTypeDefinition(*type); }
107
109 const string& getDefaultValue(TypeDesc type, bool uniform = false) const;
110 [[deprecated]] const string& getDefaultValue(const TypeDesc* type, bool uniform = false) const { return getDefaultValue(*type, uniform); }
111
113 virtual string getValue(TypeDesc type, const Value& value, bool uniform = false) const;
114 [[deprecated]] string getValue(const TypeDesc* type, const Value& value, bool uniform = false) const { return getValue(*type, value, uniform); }
115
117 virtual string getValue(const ShaderPort* port, bool uniform = false) const;
118
122 virtual const string& getInputQualifier() const { return EMPTY_STRING; };
123
127 virtual const string& getOutputQualifier() const { return EMPTY_STRING; };
128
131 virtual const string& getConstantQualifier() const = 0;
132
136 virtual const string& getUniformQualifier() const { return EMPTY_STRING; };
137
139 virtual const string& getNewline() const { return NEWLINE; };
140
142 virtual const string& getIndentation() const { return INDENTATION; };
143
145 virtual const string& getStringQuote() const { return STRING_QUOTE; };
146
148 virtual const string& getIncludeStatement() const { return INCLUDE_STATEMENT; };
149
151 virtual const string& getSingleLineComment() const { return SINGLE_LINE_COMMENT; };
152
154 virtual const string& getBeginMultiLineComment() const { return BEGIN_MULTI_LINE_COMMENT; };
155
157 virtual const string& getEndMultiLineComment() const { return END_MULTI_LINE_COMMENT; };
158
160 virtual const string& getSourceFileExtension() const = 0;
161
163 virtual string getArrayTypeSuffix(TypeDesc, const Value&) const { return EMPTY_STRING; };
164 [[deprecated]] string getArrayTypeSuffix(const TypeDesc* type, const Value& value) const { return getArrayTypeSuffix(*type, value); }
165
167 virtual string getArrayVariableSuffix(TypeDesc type, const Value& value) const;
168 [[deprecated]] string getArrayVariableSuffix(const TypeDesc* type, const Value& value) const { return getArrayVariableSuffix(*type, value); }
169
172 [[deprecated]] virtual bool typeSupported(const TypeDesc* type) const;
173
175 virtual void makeValidName(string& name) const;
176
179 virtual void makeIdentifier(string& name, IdentifierMap& identifiers) const;
180
186 virtual string getVariableName(const string& name, TypeDesc type, IdentifierMap& identifiers) const;
187 [[deprecated]] string getVariableName(const string& name, const TypeDesc* type, IdentifierMap& identifiers) const { return getVariableName(name, *type, identifiers); }
188
196 virtual bool remapEnumeration(const string& value, TypeDesc type, const string& enumNames,
197 std::pair<TypeDesc, ValuePtr>& result) const;
198
199 // Create and return a type syntax for a struct type.
200 virtual StructTypeSyntaxPtr createStructSyntax(const string& structTypeName, const string& defaultValue,
201 const string& uniformDefaultValue, const string& typeAlias,
202 const string& typeDefinition) const;
203
205 static const string NEWLINE;
206 static const string SEMICOLON;
207 static const string COMMA;
208
209 protected:
211 Syntax(TypeSystemPtr typeSystem);
212
213 TypeSystemPtr _typeSystem;
214 vector<TypeSyntaxPtr> _typeSyntaxes;
215 std::unordered_map<TypeDesc, size_t, TypeDesc::Hasher> _typeSyntaxIndexByType;
216
217 StringSet _reservedWords;
218 StringMap _invalidTokens;
219
220 static const string INDENTATION;
221 static const string STRING_QUOTE;
222 static const string INCLUDE_STATEMENT;
223 static const string SINGLE_LINE_COMMENT;
224 static const string BEGIN_MULTI_LINE_COMMENT;
225 static const string END_MULTI_LINE_COMMENT;
226
227 static const std::unordered_map<char, size_t> CHANNELS_MAPPING;
228};
229
232class MX_GENSHADER_API TypeSyntax
233{
234 public:
235 virtual ~TypeSyntax() { }
236
238 const string& getName() const { return _name; }
239
241 const string& getTypeAlias() const { return _typeAlias; }
242
244 const string& getTypeDefinition() const { return _typeDefinition; }
245
247 const string& getDefaultValue(bool uniform) const { return uniform ? _uniformDefaultValue : _defaultValue; }
248
251 const StringVec& getMembers() const { return _members; }
252
255 virtual string getValue(const ShaderPort* port, bool uniform) const;
256
259 virtual string getValue(const Value& value, bool uniform) const = 0;
260
261 protected:
263 TypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
264 const string& typeAlias, const string& typeDefinition, const StringVec& members);
265
266 const Syntax* _parent; // parent syntax class
267 string _name; // type name
268 string _defaultValue; // default value syntax
269 string _uniformDefaultValue; // default value syntax when assigned to uniforms
270 string _typeAlias; // type alias if needed in source code
271 string _typeDefinition; // custom type definition if needed in source code
272 StringVec _members; // syntax for member access
273
274 static const StringVec EMPTY_MEMBERS;
275};
276
278class MX_GENSHADER_API ScalarTypeSyntax : public TypeSyntax
279{
280 public:
281 ScalarTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
282 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
283
284 string getValue(const Value& value, bool uniform) const override;
285};
286
288class MX_GENSHADER_API StringTypeSyntax : public ScalarTypeSyntax
289{
290 public:
291 StringTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
292 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
293
294 string getValue(const Value& value, bool uniform) const override;
295};
296
298class MX_GENSHADER_API AggregateTypeSyntax : public TypeSyntax
299{
300 public:
301 AggregateTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
302 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
303 const StringVec& members = EMPTY_MEMBERS);
304
305 string getValue(const Value& value, bool uniform) const override;
306};
307
309class MX_GENSHADER_API StructTypeSyntax : public TypeSyntax
310{
311 public:
312 StructTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
313 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
314 const StringVec& members = EMPTY_MEMBERS);
315
316 string getValue(const Value& value, bool uniform) const override;
317};
318
319MATERIALX_NAMESPACE_END
320
321#endif
Definition element subclasses.
Library-wide includes and types.
std::set< string > StringSet
A set of strings.
Definition Library.h:64
vector< string > StringVec
A vector of strings.
Definition Library.h:60
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition Library.h:62
Macros for declaring imported and exported symbols.
shared_ptr< TypeSyntax > TypeSyntaxPtr
Shared pointer to a TypeSyntax.
Definition Syntax.h:32
shared_ptr< const Syntax > ConstSyntaxPtr
Shared pointer to a constant Syntax.
Definition Syntax.h:30
shared_ptr< StructTypeSyntax > StructTypeSyntaxPtr
Shared pointer to a StructTypeSyntax.
Definition Syntax.h:34
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition Syntax.h:28
std::unordered_map< string, size_t > IdentifierMap
Map holding identifier names and a counter for creating unique names from them.
Definition Syntax.h:38
Type descriptor for a MaterialX data type.
Generic value classes.
string getValue(const Value &value, bool uniform) const override
Returns a value formatted according to this type syntax.
string getValue(const Value &value, bool uniform) const override
Returns a value formatted according to this type syntax.
An input or output port on a ShaderNode.
Definition ShaderNode.h:123
string getValue(const Value &value, bool uniform) const override
Returns a value formatted according to this type syntax.
Specialization of TypeSyntax for struct types.
Definition Syntax.h:310
string getValue(const Value &value, bool uniform) const override
Returns a value formatted according to this type syntax.
Base class for syntax objects used by shader generators to emit code with correct syntax for each lan...
Definition Syntax.h:44
virtual string getArrayVariableSuffix(TypeDesc type, const Value &value) const
Return the array suffix to use for declaring an array variable.
virtual string getArrayTypeSuffix(TypeDesc, const Value &) const
Return the array suffix to use for declaring an array type.
Definition Syntax.h:163
virtual string getValue(const ShaderPort *port, bool uniform=false) const
Returns the value string for a given shader port object.
virtual const string & getBeginMultiLineComment() const
Return the characters used to begin a multi line comments block.
Definition Syntax.h:154
virtual const string & getIndentation() const
Return the characters used for a single indentation level.
Definition Syntax.h:142
virtual const string & getSourceFileExtension() const =0
Return the file extension used for source code files in this language.
const string & getDefaultValue(TypeDesc type, bool uniform=false) const
Returns the default value string for the given type.
const vector< TypeSyntaxPtr > & getTypeSyntaxes() const
Returns an array of all registered type syntax objects.
Definition Syntax.h:85
virtual void makeValidName(string &name) const
Modify the given name string to remove any invalid characters or tokens.
const StringMap & getInvalidTokens() const
Returns a mapping from disallowed tokens to replacement strings for this language syntax.
Definition Syntax.h:77
virtual const string & getStringQuote() const
Return the characters used to begin/end a string definition.
Definition Syntax.h:145
virtual const string & getNewline() const
Return the characters used for a newline.
Definition Syntax.h:139
const string & getTypeAlias(TypeDesc type) const
Returns a type alias for the given data type.
virtual bool typeSupported(const TypeDesc *type) const
Query if given type is supported in the syntax.
const string & getTypeDefinition(TypeDesc type) const
Returns a custom type definition if needed for the given data type.
void registerReservedWords(const StringSet &names)
Register names that are reserved words not to be used by a code generator when naming variables and f...
virtual string getVariableName(const string &name, TypeDesc type, IdentifierMap &identifiers) const
Create a unique identifier for the given variable name and type.
virtual const string & getOutputQualifier() const
Returns a type qualifier to be used when declaring types for output variables.
Definition Syntax.h:127
void registerTypeSyntax(TypeDesc type, TypeSyntaxPtr syntax)
Register syntax handling for a data type.
virtual const string & getConstantQualifier() const =0
Get the qualifier used when declaring constant variables.
virtual const string & getIncludeStatement() const
Return the string pattern used for a file include statement.
Definition Syntax.h:148
virtual string getOutputTypeName(TypeDesc type) const
Returns the type name in an output context.
Punctuation
Punctuation types.
Definition Syntax.h:48
Syntax(TypeSystemPtr typeSystem)
Protected constructor.
void registerInvalidTokens(const StringMap &tokens)
Register a set string replacements for disallowed tokens for a code generator when naming variables a...
virtual const string & getEndMultiLineComment() const
Return the characters used to end a multi line comments block.
Definition Syntax.h:157
virtual const string & getSingleLineComment() const
Return the characters used for single line comment.
Definition Syntax.h:151
TypeDesc getType(const string &name) const
Return a type description for the given type name.
Definition Syntax.h:88
const string & getTypeName(TypeDesc type) const
Returns the name syntax of the given type.
const TypeSyntax & getTypeSyntax(TypeDesc type) const
Returns the type syntax object for a named type.
static const string NEWLINE
Constants with commonly used strings.
Definition Syntax.h:205
virtual const string & getUniformQualifier() const
Get the qualifier used when declaring uniform variables.
Definition Syntax.h:136
virtual const string & getInputQualifier() const
Returns a type qualifier to be used when declaring types for input variables.
Definition Syntax.h:122
virtual void makeIdentifier(string &name, IdentifierMap &identifiers) const
Make sure the given name is a unique identifier, updating it if needed to make it unique.
virtual string getValue(TypeDesc type, const Value &value, bool uniform=false) const
Returns the value string for a given type and value object.
virtual bool remapEnumeration(const string &value, TypeDesc type, const string &enumNames, std::pair< TypeDesc, ValuePtr > &result) const
Given an input specification attempt to remap this to an enumeration which is accepted by the shader ...
const StringSet & getReservedWords() const
Returns a set of names that are reserved words for this language syntax.
Definition Syntax.h:74
A type descriptor for MaterialX data types.
Definition TypeDesc.h:40
Base class for syntax handling of types.
Definition Syntax.h:233
const string & getDefaultValue(bool uniform) const
Returns the default value for this type.
Definition Syntax.h:247
virtual string getValue(const Value &value, bool uniform) const =0
Returns a value formatted according to this type syntax.
TypeSyntax(const Syntax *parent, const string &name, const string &defaultValue, const string &uniformDefaultValue, const string &typeAlias, const string &typeDefinition, const StringVec &members)
Protected constructor.
const string & getName() const
Returns the type name.
Definition Syntax.h:238
virtual string getValue(const ShaderPort *port, bool uniform) const
Returns a value formatted according to this type syntax.
const string & getTypeAlias() const
Returns a type alias if needed to define the type in the target language.
Definition Syntax.h:241
const StringVec & getMembers() const
Returns the syntax for accessing type members if the type can be swizzled.
Definition Syntax.h:251
const string & getTypeDefinition() const
Returns a type definition if needed to define the type in the target language.
Definition Syntax.h:244
A generic, discriminated value, whose type may be queried dynamically.
Definition Value.h:46