MaterialX 1.38.2
Syntax.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_SYNTAX_H
7#define MATERIALX_SYNTAX_H
8
11
13
16#include <MaterialXCore/Value.h>
17
18namespace MaterialX
19{
20
21class Syntax;
22class TypeSyntax;
23class TypeDesc;
24
26using SyntaxPtr = shared_ptr<Syntax>;
28using ConstSyntaxPtr = shared_ptr<const Syntax>;
30using TypeSyntaxPtr = shared_ptr<TypeSyntax>;
31
34using IdentifierMap = std::unordered_map<string, size_t>;
35
39class MX_GENSHADER_API Syntax
40{
41 public:
44 {
45 PARENTHESES,
46 CURLY_BRACKETS,
47 SQUARE_BRACKETS,
48 DOUBLE_SQUARE_BRACKETS
49 };
50
51 public:
52 virtual ~Syntax() { }
53
56 void registerTypeSyntax(const TypeDesc* type, TypeSyntaxPtr syntax);
57
61 void registerReservedWords(const StringSet& names);
62
66 void registerInvalidTokens(const StringMap& tokens);
67
69 const StringSet& getReservedWords() const { return _reservedWords; }
70
72 const StringMap& getInvalidTokens() const { return _invalidTokens; }
73
76 const TypeSyntax& getTypeSyntax(const TypeDesc* type) const;
77
79 const vector<TypeSyntaxPtr>& getTypeSyntaxes() const { return _typeSyntaxes; }
80
83 const TypeDesc* getTypeDescription(const TypeSyntaxPtr& typeSyntax) const;
84
86 const string& getTypeName(const TypeDesc* type) const;
87
89 string getOutputTypeName(const TypeDesc* type) const;
90
93 const string& getTypeAlias(const TypeDesc* type) const;
94
97 const string& getTypeDefinition(const TypeDesc* type) const;
98
100 const string& getDefaultValue(const TypeDesc* type, bool uniform = false) const;
101
103 virtual string getValue(const TypeDesc* type, const Value& value, bool uniform = false) const;
104
106 virtual string getSwizzledVariable(const string& srcName, const TypeDesc* srcType, const string& channels, const TypeDesc* dstType) const;
107
109 virtual ValuePtr getSwizzledValue(ValuePtr value, const TypeDesc* srcType, const string& channels, const TypeDesc* dstType) const;
110
114 virtual const string& getInputQualifier() const { return EMPTY_STRING; };
115
119 virtual const string& getOutputQualifier() const { return EMPTY_STRING; };
120
123 virtual const string& getConstantQualifier() const = 0;
124
128 virtual const string& getUniformQualifier() const { return EMPTY_STRING; };
129
131 virtual const string& getNewline() const { return NEWLINE; };
132
134 virtual const string& getIndentation() const { return INDENTATION; };
135
137 virtual const string& getStringQuote() const { return STRING_QUOTE; };
138
140 virtual const string& getIncludeStatement() const { return INCLUDE_STATEMENT; };
141
143 virtual const string& getSingleLineComment() const { return SINGLE_LINE_COMMENT; };
144
146 virtual const string& getBeginMultiLineComment() const { return BEGIN_MULTI_LINE_COMMENT; };
147
149 virtual const string& getEndMultiLineComment() const { return END_MULTI_LINE_COMMENT; };
150
152 virtual const string& getSourceFileExtension() const = 0;
153
155 virtual string getArrayTypeSuffix(const TypeDesc*, const Value&) const { return EMPTY_STRING; };
156
158 virtual string getArrayVariableSuffix(const TypeDesc* type, const Value& value) const;
159
162 virtual bool typeSupported(const TypeDesc* type) const;
163
165 virtual void makeValidName(string& name) const;
166
169 virtual void makeIdentifier(string& name, IdentifierMap& identifiers) const;
170
176 virtual string getVariableName(const string& name, const TypeDesc* type, IdentifierMap& identifiers) const;
177
185 virtual bool remapEnumeration(const string& value, const TypeDesc* type, const string& enumNames,
186 std::pair<const TypeDesc*, ValuePtr>& result) const;
187
189 static const string NEWLINE;
190 static const string SEMICOLON;
191 static const string COMMA;
192
193 protected:
195 Syntax();
196
197 vector<TypeSyntaxPtr> _typeSyntaxes;
198 std::unordered_map<const TypeDesc*, size_t> _typeSyntaxByType;
199
200 StringSet _reservedWords;
201 StringMap _invalidTokens;
202
203 static const string INDENTATION;
204 static const string STRING_QUOTE;
205 static const string INCLUDE_STATEMENT;
206 static const string SINGLE_LINE_COMMENT;
207 static const string BEGIN_MULTI_LINE_COMMENT;
208 static const string END_MULTI_LINE_COMMENT;
209
210 static const std::unordered_map<char, size_t> CHANNELS_MAPPING;
211};
212
215class MX_GENSHADER_API TypeSyntax
216{
217 public:
218 virtual ~TypeSyntax() { }
219
221 const string& getName() const { return _name; }
222
224 const string& getTypeAlias() const { return _typeAlias; }
225
227 const string& getTypeDefinition() const { return _typeDefinition; }
228
230 const string& getDefaultValue(bool uniform) const { return uniform ? _uniformDefaultValue : _defaultValue; }
231
234 const StringVec& getMembers() const { return _members; }
235
238 virtual string getValue(const Value& value, bool uniform) const = 0;
239
243 virtual string getValue(const StringVec& values, bool uniform) const = 0;
244
245 protected:
247 TypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
248 const string& typeAlias, const string& typeDefinition, const StringVec& members);
249
250 string _name; // type name
251 string _defaultValue; // default value syntax
252 string _uniformDefaultValue; // default value syntax when assigned to uniforms
253 string _typeAlias; // type alias if needed in source code
254 string _typeDefinition; // custom type definition if needed in source code
255 StringVec _members; // syntax for member access
256
257 static const StringVec EMPTY_MEMBERS;
258};
259
261class MX_GENSHADER_API ScalarTypeSyntax : public TypeSyntax
262{
263 public:
264 ScalarTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
265 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
266
267 string getValue(const Value& value, bool uniform) const override;
268 string getValue(const StringVec& values, bool uniform) const override;
269};
270
272class MX_GENSHADER_API StringTypeSyntax : public ScalarTypeSyntax
273{
274 public:
275 StringTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
276 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING);
277
278 string getValue(const Value& value, bool uniform) const override;
279};
280
282class MX_GENSHADER_API AggregateTypeSyntax : public TypeSyntax
283{
284 public:
285 AggregateTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
286 const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
287 const StringVec& members = EMPTY_MEMBERS);
288
289 string getValue(const Value& value, bool uniform) const override;
290 string getValue(const StringVec& values, bool uniform) const override;
291};
292
293} // namespace MaterialX
294
295#endif
Definition element subclasses.
Library-wide includes and types.
std::set< string > StringSet
A set of strings.
Definition: Library.h:60
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:58
Macros for declaring imported and exported symbols.
shared_ptr< TypeSyntax > TypeSyntaxPtr
Shared pointer to a TypeSyntax.
Definition: Syntax.h:30
shared_ptr< const Syntax > ConstSyntaxPtr
Shared pointer to a constant Syntax.
Definition: Syntax.h:28
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition: Syntax.h:26
std::unordered_map< string, size_t > IdentifierMap
Map holding identifier names and a counter for creating unique names from them.
Definition: Syntax.h:34
Generic value classes.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
Specialization of TypeSyntax for aggregate types.
Definition: Syntax.h:283
Specialization of TypeSyntax for scalar types.
Definition: Syntax.h:262
Specialization of TypeSyntax for string types.
Definition: Syntax.h:273
Base class for syntax objects used by shader generators to emit code with correct syntax for each lan...
Definition: Syntax.h:40
virtual const string & getBeginMultiLineComment() const
Return the characters used to begin a multi line comments block.
Definition: Syntax.h:146
virtual const string & getIndentation() const
Return the characters used for a single indentation level.
Definition: Syntax.h:134
virtual const string & getSourceFileExtension() const =0
Return the file extension used for source code files in this language.
const vector< TypeSyntaxPtr > & getTypeSyntaxes() const
Returns an array of all registered type syntax objects.
Definition: Syntax.h:79
const StringMap & getInvalidTokens() const
Returns a mapping from disallowed tokens to replacement strings for this language syntax.
Definition: Syntax.h:72
virtual const string & getStringQuote() const
Return the characters used to begin/end a string definition.
Definition: Syntax.h:137
virtual const string & getNewline() const
Return the characters used for a newline.
Definition: Syntax.h:131
virtual const string & getOutputQualifier() const
Returns a type qualifier to be used when declaring types for output variables.
Definition: Syntax.h:119
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:140
Punctuation
Punctuation types.
Definition: Syntax.h:44
virtual const string & getEndMultiLineComment() const
Return the characters used to end a multi line comments block.
Definition: Syntax.h:149
virtual const string & getSingleLineComment() const
Return the characters used for single line comment.
Definition: Syntax.h:143
static const string NEWLINE
Constants with commonly used strings.
Definition: Syntax.h:189
virtual string getArrayTypeSuffix(const TypeDesc *, const Value &) const
Return the array suffix to use for declaring an array type.
Definition: Syntax.h:155
virtual const string & getUniformQualifier() const
Get the qualifier used when declaring uniform variables.
Definition: Syntax.h:128
virtual const string & getInputQualifier() const
Returns a type qualifier to be used when declaring types for input variables.
Definition: Syntax.h:114
const StringSet & getReservedWords() const
Returns a set of names that are reserved words for this language syntax.
Definition: Syntax.h:69
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:29
Base class for syntax handling of types.
Definition: Syntax.h:216
const string & getDefaultValue(bool uniform) const
Returns the default value for this type.
Definition: Syntax.h:230
virtual string getValue(const StringVec &values, bool uniform) const =0
Returns a value formatted according to this type syntax.
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:221
const string & getTypeAlias() const
Returns a type alias if needed to define the type in the target language.
Definition: Syntax.h:224
const StringVec & getMembers() const
Returns the syntax for accessing type members if the type can be swizzled.
Definition: Syntax.h:234
const string & getTypeDefinition() const
Returns a type definition if needed to define the type in the target language.
Definition: Syntax.h:227
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:46