MaterialX 1.39.2
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
73 virtual void registerStructTypeDescSyntax();
74
76 const StringSet& getReservedWords() const { return _reservedWords; }
77
79 const StringMap& getInvalidTokens() const { return _invalidTokens; }
80
83 const TypeSyntax& getTypeSyntax(TypeDesc type) const;
84 [[deprecated]] const TypeSyntax& getTypeSyntax(const TypeDesc* type) const { return getTypeSyntax(*type); }
85
87 const vector<TypeSyntaxPtr>& getTypeSyntaxes() const { return _typeSyntaxes; }
88
90 const string& getTypeName(TypeDesc type) const;
91 [[deprecated]] const string& getTypeName(const TypeDesc* type) const { return getTypeName(*type); }
92
94 virtual string getOutputTypeName(TypeDesc type) const;
95 [[deprecated]] string getOutputTypeName(const TypeDesc* type) const { return getOutputTypeName(*type); }
96
99 const string& getTypeAlias(TypeDesc type) const;
100 [[deprecated]] const string& getTypeAlias(const TypeDesc* type) const { return getTypeAlias(*type); }
101
104 const string& getTypeDefinition(TypeDesc type) const;
105 [[deprecated]] const string& getTypeDefinition(const TypeDesc* type) const { return getTypeDefinition(*type); }
106
108 const string& getDefaultValue(TypeDesc type, bool uniform = false) const;
109 [[deprecated]] const string& getDefaultValue(const TypeDesc* type, bool uniform = false) const { return getDefaultValue(*type, uniform); }
110
112 virtual string getValue(TypeDesc type, const Value& value, bool uniform = false) const;
113 [[deprecated]] string getValue(const TypeDesc* type, const Value& value, bool uniform = false) const { return getValue(*type, value, uniform); }
114
116 virtual string getValue(const ShaderPort* port, bool uniform = false) const;
117
121 virtual const string& getInputQualifier() const { return EMPTY_STRING; };
122
126 virtual const string& getOutputQualifier() const { return EMPTY_STRING; };
127
130 virtual const string& getConstantQualifier() const = 0;
131
135 virtual const string& getUniformQualifier() const { return EMPTY_STRING; };
136
138 virtual const string& getNewline() const { return NEWLINE; };
139
141 virtual const string& getIndentation() const { return INDENTATION; };
142
144 virtual const string& getStringQuote() const { return STRING_QUOTE; };
145
147 virtual const string& getIncludeStatement() const { return INCLUDE_STATEMENT; };
148
150 virtual const string& getSingleLineComment() const { return SINGLE_LINE_COMMENT; };
151
153 virtual const string& getBeginMultiLineComment() const { return BEGIN_MULTI_LINE_COMMENT; };
154
156 virtual const string& getEndMultiLineComment() const { return END_MULTI_LINE_COMMENT; };
157
159 virtual const string& getSourceFileExtension() const = 0;
160
162 virtual string getArrayTypeSuffix(TypeDesc, const Value&) const { return EMPTY_STRING; };
163 [[deprecated]] string getArrayTypeSuffix(const TypeDesc* type, const Value& value) const { return getArrayTypeSuffix(*type, value); }
164
166 virtual string getArrayVariableSuffix(TypeDesc type, const Value& value) const;
167 [[deprecated]] string getArrayVariableSuffix(const TypeDesc* type, const Value& value) const { return getArrayVariableSuffix(*type, value); }
168
171 [[deprecated]] virtual bool typeSupported(const TypeDesc* type) const;
172
174 virtual void makeValidName(string& name) const;
175
178 virtual void makeIdentifier(string& name, IdentifierMap& identifiers) const;
179
185 virtual string getVariableName(const string& name, TypeDesc type, IdentifierMap& identifiers) const;
186 [[deprecated]] string getVariableName(const string& name, const TypeDesc* type, IdentifierMap& identifiers) const { return getVariableName(name, *type, identifiers); }
187
195 virtual bool remapEnumeration(const string& value, TypeDesc type, const string& enumNames,
196 std::pair<TypeDesc, ValuePtr>& result) const;
197
199 static const string NEWLINE;
200 static const string SEMICOLON;
201 static const string COMMA;
202
203 protected:
206
207 virtual StructTypeSyntaxPtr createStructSyntax(const string& structTypeName, const string& defaultValue,
208 const string& uniformDefaultValue, const string& typeAlias,
209 const string& typeDefinition) const
210 {
211 return std::make_shared<StructTypeSyntax>(
212 this,
213 structTypeName,
214 defaultValue,
215 uniformDefaultValue,
216 typeAlias,
217 typeDefinition);
218 }
219
220 vector<TypeSyntaxPtr> _typeSyntaxes;
221 std::unordered_map<TypeDesc, size_t, TypeDesc::Hasher> _typeSyntaxIndexByType;
222
223 StringSet _reservedWords;
224 StringMap _invalidTokens;
225
226 static const string INDENTATION;
227 static const string STRING_QUOTE;
228 static const string INCLUDE_STATEMENT;
229 static const string SINGLE_LINE_COMMENT;
230 static const string BEGIN_MULTI_LINE_COMMENT;
231 static const string END_MULTI_LINE_COMMENT;
232
233 static const std::unordered_map<char, size_t> CHANNELS_MAPPING;
234};
235
238class MX_GENSHADER_API TypeSyntax
239{
240 public:
241 virtual ~TypeSyntax() { }
242
244 const string& getName() const { return _name; }
245
247 const string& getTypeAlias() const { return _typeAlias; }
248
250 const string& getTypeDefinition() const { return _typeDefinition; }
251
253 const string& getDefaultValue(bool uniform) const { return uniform ? _uniformDefaultValue : _defaultValue; }
254
257 const StringVec& getMembers() const { return _members; }
258
261 virtual string getValue(const ShaderPort* port, bool uniform) const;
262
265 virtual string getValue(const Value& value, bool uniform) const = 0;
266
267 protected:
269 TypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
270 const string& typeAlias, const string& typeDefinition, const StringVec& members);
271
272 string _name; // type name
273 string _defaultValue; // default value syntax
274 string _uniformDefaultValue; // default value syntax when assigned to uniforms
275 string _typeAlias; // type alias if needed in source code
276 string _typeDefinition; // custom type definition if needed in source code
277 StringVec _members; // syntax for member access
278
279 static const StringVec EMPTY_MEMBERS;
280};
281
283class MX_GENSHADER_API ScalarTypeSyntax : public TypeSyntax
284{
285 public:
286 ScalarTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
287 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
288
289 string getValue(const Value& value, bool uniform) const override;
290};
291
293class MX_GENSHADER_API StringTypeSyntax : public ScalarTypeSyntax
294{
295 public:
296 StringTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
297 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
298
299 string getValue(const Value& value, bool uniform) const override;
300};
301
303class MX_GENSHADER_API AggregateTypeSyntax : public TypeSyntax
304{
305 public:
306 AggregateTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
307 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
308 const StringVec& members = EMPTY_MEMBERS);
309
310 string getValue(const Value& value, bool uniform) const override;
311};
312
314class MX_GENSHADER_API StructTypeSyntax : public TypeSyntax
315{
316 public:
317 StructTypeSyntax(const Syntax* parentSyntax, const string& name, const string& defaultValue, const string& uniformDefaultValue,
318 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
319 const StringVec& members = EMPTY_MEMBERS);
320
321 string getValue(const Value& value, bool uniform) const override;
322
323 protected:
324 const Syntax* _parentSyntax;
325};
326
327MATERIALX_NAMESPACE_END
328
329#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 aggregate types.
Definition Syntax.h:315
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:162
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:153
virtual const string & getIndentation() const
Return the characters used for a single indentation level.
Definition Syntax.h:141
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:87
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:79
virtual const string & getStringQuote() const
Return the characters used to begin/end a string definition.
Definition Syntax.h:144
virtual const string & getNewline() const
Return the characters used for a newline.
Definition Syntax.h:138
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...
Syntax()
Protected constructor.
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:126
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:147
virtual string getOutputTypeName(TypeDesc type) const
Returns the type name in an output context.
Punctuation
Punctuation types.
Definition Syntax.h:48
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:156
virtual const string & getSingleLineComment() const
Return the characters used for single line comment.
Definition Syntax.h:150
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:199
virtual const string & getUniformQualifier() const
Get the qualifier used when declaring uniform variables.
Definition Syntax.h:135
virtual const string & getInputQualifier() const
Returns a type qualifier to be used when declaring types for input variables.
Definition Syntax.h:121
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:76
A type descriptor for MaterialX data types.
Definition TypeDesc.h:36
Base class for syntax handling of types.
Definition Syntax.h:239
const string & getDefaultValue(bool uniform) const
Returns the default value for this type.
Definition Syntax.h:253
virtual string getValue(const Value &value, bool uniform) const =0
Returns a value formatted according to this type syntax.
const string & getName() const
Returns the type name.
Definition Syntax.h:244
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:247
TypeSyntax(const string &name, const string &defaultValue, const string &uniformDefaultValue, const string &typeAlias, const string &typeDefinition, const StringVec &members)
Protected constructor.
const StringVec & getMembers() const
Returns the syntax for accessing type members if the type can be swizzled.
Definition Syntax.h:257
const string & getTypeDefinition() const
Returns a type definition if needed to define the type in the target language.
Definition Syntax.h:250
A generic, discriminated value, whose type may be queried dynamically.
Definition Value.h:46