HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Base class for syntax handling for shader generators
11 
14 
16 #include <MaterialXCore/Library.h>
17 #include <MaterialXCore/Value.h>
18 
20 
21 class Syntax;
22 class TypeSyntax;
23 class StructTypeSyntax;
24 class TypeDesc;
25 class ShaderPort;
26 
27 /// Shared pointer to a Syntax
28 using SyntaxPtr = shared_ptr<Syntax>;
29 /// Shared pointer to a constant Syntax
30 using ConstSyntaxPtr = shared_ptr<const Syntax>;
31 /// Shared pointer to a TypeSyntax
32 using TypeSyntaxPtr = shared_ptr<TypeSyntax>;
33 /// Shared pointer to a StructTypeSyntax
34 using StructTypeSyntaxPtr = shared_ptr<StructTypeSyntax>;
35 
36 /// Map holding identifier names and a counter for
37 /// creating unique names from them.
38 using IdentifierMap = std::unordered_map<string, size_t>;
39 
40 /// @class Syntax
41 /// Base class for syntax objects used by shader generators
42 /// to emit code with correct syntax for each language.
44 {
45  public:
46  /// Punctuation types
48  {
52  DOUBLE_SQUARE_BRACKETS
53  };
54 
55  public:
56  virtual ~Syntax() { }
57 
58  /// Register syntax handling for a data type.
59  /// Required to be set for all supported data types.
60  void registerTypeSyntax(TypeDesc type, TypeSyntaxPtr syntax);
61  [[deprecated]] void registerTypeSyntax(const TypeDesc* type, TypeSyntaxPtr syntax) { registerTypeSyntax(*type, syntax); }
62 
63  /// Register names that are reserved words not to be used by a code generator when naming
64  /// variables and functions. Keywords, types, built-in functions etc. should be
65  /// added to this set. Multiple calls will add to the internal set of names.
66  void registerReservedWords(const StringSet& names);
67 
68  /// Register a set string replacements for disallowed tokens
69  /// for a code generator when naming variables and functions.
70  /// Multiple calls will add to the internal set of tokens.
71  void registerInvalidTokens(const StringMap& tokens);
72 
73  /// Returns a set of names that are reserved words for this language syntax.
74  const StringSet& getReservedWords() const { return _reservedWords; }
75 
76  /// Returns a mapping from disallowed tokens to replacement strings for this language syntax.
77  const StringMap& getInvalidTokens() const { return _invalidTokens; }
78 
79  /// Returns the type syntax object for a named type.
80  /// Throws an exception if a type syntax is not defined for the given type.
81  const TypeSyntax& getTypeSyntax(TypeDesc type) const;
82  [[deprecated]] const TypeSyntax& getTypeSyntax(const TypeDesc* type) const { return getTypeSyntax(*type); }
83 
84  /// Returns an array of all registered type syntax objects
85  const vector<TypeSyntaxPtr>& getTypeSyntaxes() const { return _typeSyntaxes; }
86 
87  /// Return a type description for the given type name.
88  TypeDesc getType(const string& name) const { return _typeSystem->getType(name); }
89 
90  /// Returns the name syntax of the given type
91  const string& getTypeName(TypeDesc type) const;
92  [[deprecated]] const string& getTypeName(const TypeDesc* type) const { return getTypeName(*type); }
93 
94  /// Returns the type name in an output context
95  virtual string getOutputTypeName(TypeDesc type) const;
96  [[deprecated]] string getOutputTypeName(const TypeDesc* type) const { return getOutputTypeName(*type); }
97 
98  /// Returns a type alias for the given data type.
99  /// If not used returns an empty string.
100  const string& getTypeAlias(TypeDesc type) const;
101  [[deprecated]] const string& getTypeAlias(const TypeDesc* type) const { return getTypeAlias(*type); }
102 
103  /// Returns a custom type definition if needed for the given data type.
104  /// If not used returns an empty string.
105  const string& getTypeDefinition(TypeDesc type) const;
106  [[deprecated]] const string& getTypeDefinition(const TypeDesc* type) const { return getTypeDefinition(*type); }
107 
108  /// Returns the default value string for the given type
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 
112  /// Returns the value string for a given type and value object
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 
116  /// Returns the value string for a given shader port object
117  virtual string getValue(const ShaderPort* port, bool uniform = false) const;
118 
119  /// Returns a type qualifier to be used when declaring types for input variables.
120  /// Default implementation returns empty string and derived syntax classes should
121  /// override this method.
122  virtual const string& getInputQualifier() const { return EMPTY_STRING; };
123 
124  /// Returns a type qualifier to be used when declaring types for output variables.
125  /// Default implementation returns empty string and derived syntax classes should
126  /// override this method.
127  virtual const string& getOutputQualifier() const { return EMPTY_STRING; };
128 
129  /// Get the qualifier used when declaring constant variables.
130  /// Derived classes must define this method.
131  virtual const string& getConstantQualifier() const = 0;
132 
133  /// Get the qualifier used when declaring uniform variables.
134  /// Default implementation returns empty string and derived syntax classes should
135  /// override this method.
136  virtual const string& getUniformQualifier() const { return EMPTY_STRING; };
137 
138  /// Return the characters used for a newline.
139  virtual const string& getNewline() const { return NEWLINE; };
140 
141  /// Return the characters used for a single indentation level.
142  virtual const string& getIndentation() const { return INDENTATION; };
143 
144  /// Return the characters used to begin/end a string definition.
145  virtual const string& getStringQuote() const { return STRING_QUOTE; };
146 
147  /// Return the string pattern used for a file include statement.
148  virtual const string& getIncludeStatement() const { return INCLUDE_STATEMENT; };
149 
150  /// Return the characters used for single line comment.
151  virtual const string& getSingleLineComment() const { return SINGLE_LINE_COMMENT; };
152 
153  /// Return the characters used to begin a multi line comments block.
154  virtual const string& getBeginMultiLineComment() const { return BEGIN_MULTI_LINE_COMMENT; };
155 
156  /// Return the characters used to end a multi line comments block.
157  virtual const string& getEndMultiLineComment() const { return END_MULTI_LINE_COMMENT; };
158 
159  /// Return the file extension used for source code files in this language.
160  virtual const string& getSourceFileExtension() const = 0;
161 
162  /// Return the array suffix to use for declaring an array type.
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 
166  /// Return the array suffix to use for declaring an array variable.
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 
170  /// Query if given type is supported in the syntax.
171  /// By default all types are assumed to be supported.
172  [[deprecated]] virtual bool typeSupported(const TypeDesc* type) const;
173 
174  /// Modify the given name string to remove any invalid characters or tokens.
175  virtual void makeValidName(string& name) const;
176 
177  /// Make sure the given name is a unique identifier,
178  /// updating it if needed to make it unique.
179  virtual void makeIdentifier(string& name, IdentifierMap& identifiers) const;
180 
181  /// Create a unique identifier for the given variable name and type.
182  /// The method is used for naming variables (inputs and outputs) in generated code.
183  /// Derived classes can override this method to have a custom naming strategy.
184  /// Default implementation adds a number suffix, or increases an existing number suffix,
185  /// on the name string if there is a name collision.
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 
189  /// Given an input specification attempt to remap this to an enumeration which is accepted by
190  /// the shader generator. The enumeration may be converted to a different type than the input.
191  /// @param value The value string to remap.
192  /// @param type The type of the value to remap,
193  /// @param enumNames Type enumeration names
194  /// @param result Enumeration type and value (returned).
195  /// @return Return true if the remapping was successful.
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 
204  /// Constants with commonly used strings.
205  static const string NEWLINE;
206  static const string SEMICOLON;
207  static const string COMMA;
208 
209  protected:
210  /// Protected constructor
211  Syntax(TypeSystemPtr typeSystem);
212 
214  vector<TypeSyntaxPtr> _typeSyntaxes;
215  std::unordered_map<TypeDesc, size_t, TypeDesc::Hasher> _typeSyntaxIndexByType;
216 
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 
230 /// @class TypeSyntax
231 /// Base class for syntax handling of types.
233 {
234  public:
235  virtual ~TypeSyntax() { }
236 
237  /// Returns the type name.
238  const string& getName() const { return _name; }
239 
240  /// Returns a type alias if needed to define the type in the target language.
241  const string& getTypeAlias() const { return _typeAlias; }
242 
243  /// Returns a type definition if needed to define the type in the target language.
244  const string& getTypeDefinition() const { return _typeDefinition; }
245 
246  /// Returns the default value for this type.
247  const string& getDefaultValue(bool uniform) const { return uniform ? _uniformDefaultValue : _defaultValue; }
248 
249  /// Returns the syntax for accessing type members if the type
250  /// can be swizzled.
251  const StringVec& getMembers() const { return _members; }
252 
253  /// Returns a value formatted according to this type syntax.
254  /// The value is constructed from the given shader port object.
255  virtual string getValue(const ShaderPort* port, bool uniform) const;
256 
257  /// Returns a value formatted according to this type syntax.
258  /// The value is constructed from the given value object.
259  virtual string getValue(const Value& value, bool uniform) const = 0;
260 
261  protected:
262  /// Protected constructor
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 
277 /// Specialization of TypeSyntax for scalar types.
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 
287 /// Specialization of TypeSyntax for string types.
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 
297 /// Specialization of TypeSyntax for aggregate types.
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 
308 /// Specialization of TypeSyntax for struct types.
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 
320 
321 #endif
static const std::unordered_map< char, size_t > CHANNELS_MAPPING
Definition: Syntax.h:227
const string & getTypeDefinition() const
Returns a type definition if needed to define the type in the target language.
Definition: Syntax.h:244
string _typeDefinition
Definition: Syntax.h:271
const string & getTypeName(const TypeDesc *type) const
Definition: Syntax.h:92
const StringVec & getMembers() const
Definition: Syntax.h:251
const StringSet & getReservedWords() const
Returns a set of names that are reserved words for this language syntax.
Definition: Syntax.h:74
string _name
Definition: Syntax.h:267
shared_ptr< StructTypeSyntax > StructTypeSyntaxPtr
Shared pointer to a StructTypeSyntax.
Definition: Syntax.h:34
TypeDesc getType(const string &name) const
Return a type description for the given type name.
Definition: Syntax.h:88
virtual const string & getEndMultiLineComment() const
Return the characters used to end a multi line comments block.
Definition: Syntax.h:157
Specialization of TypeSyntax for scalar types.
Definition: Syntax.h:278
string getArrayTypeSuffix(const TypeDesc *type, const Value &value) const
Definition: Syntax.h:164
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
string _defaultValue
Definition: Syntax.h:268
vector< string > StringVec
A vector of strings.
Definition: Library.h:60
virtual string getArrayTypeSuffix(TypeDesc, const Value &) const
Return the array suffix to use for declaring an array type.
Definition: Syntax.h:163
const string & getName() const
Returns the type name.
Definition: Syntax.h:238
virtual const string & getUniformQualifier() const
Definition: Syntax.h:136
StringMap _invalidTokens
Definition: Syntax.h:218
GLsizei const GLfloat * value
Definition: glcorearb.h:824
string getVariableName(const string &name, const TypeDesc *type, IdentifierMap &identifiers) const
Definition: Syntax.h:187
string getOutputTypeName(const TypeDesc *type) const
Definition: Syntax.h:96
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
shared_ptr< TypeSyntax > TypeSyntaxPtr
Shared pointer to a TypeSyntax.
Definition: Syntax.h:32
const string & getTypeAlias() const
Returns a type alias if needed to define the type in the target language.
Definition: Syntax.h:241
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition: Syntax.h:28
const vector< TypeSyntaxPtr > & getTypeSyntaxes() const
Returns an array of all registered type syntax objects.
Definition: Syntax.h:85
const string & getTypeAlias(const TypeDesc *type) const
Definition: Syntax.h:101
shared_ptr< const Syntax > ConstSyntaxPtr
Shared pointer to a constant Syntax.
Definition: Syntax.h:30
static const string INDENTATION
Definition: Syntax.h:220
const string & getDefaultValue(bool uniform) const
Returns the default value for this type.
Definition: Syntax.h:247
virtual const string & getOutputQualifier() const
Definition: Syntax.h:127
virtual const string & getSingleLineComment() const
Return the characters used for single line comment.
Definition: Syntax.h:151
const string & getDefaultValue(const TypeDesc *type, bool uniform=false) const
Definition: Syntax.h:110
#define MX_GENSHADER_API
Definition: Export.h:18
**But if you need a result
Definition: thread.h:622
const Syntax * _parent
Definition: Syntax.h:266
string _typeAlias
Definition: Syntax.h:270
string getValue(const Value &value, bool uniform) const override
shared_ptr< class TypeSystem > TypeSystemPtr
Definition: TypeDesc.h:219
__hostdev__ float getValue(uint32_t i) const
Definition: NanoVDB.h:5578
void registerTypeSyntax(const TypeDesc *type, TypeSyntaxPtr syntax)
Definition: Syntax.h:61
virtual string getValue(const ShaderPort *port, bool uniform) const
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
virtual ~Syntax()
Definition: Syntax.h:56
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:45
static const string END_MULTI_LINE_COMMENT
Definition: Syntax.h:225
string _uniformDefaultValue
Definition: Syntax.h:269
virtual const string & getIndentation() const
Return the characters used for a single indentation level.
Definition: Syntax.h:142
virtual const string & getStringQuote() const
Return the characters used to begin/end a string definition.
Definition: Syntax.h:145
static const string NEWLINE
Constants with commonly used strings.
Definition: Syntax.h:205
GLuint const GLchar * name
Definition: glcorearb.h:786
const string & getTypeDefinition(const TypeDesc *type) const
Definition: Syntax.h:106
virtual const string & getNewline() const
Return the characters used for a newline.
Definition: Syntax.h:139
Specialization of TypeSyntax for aggregate types.
Definition: Syntax.h:298
vector< TypeSyntaxPtr > _typeSyntaxes
Definition: Syntax.h:214
Specialization of TypeSyntax for string types.
Definition: Syntax.h:288
static const string INCLUDE_STATEMENT
Definition: Syntax.h:222
const TypeSyntax & getTypeSyntax(const TypeDesc *type) const
Definition: Syntax.h:82
StringVec _members
Definition: Syntax.h:272
static const StringVec EMPTY_MEMBERS
Definition: Syntax.h:274
TypeSystemPtr _typeSystem
Definition: Syntax.h:213
virtual ~TypeSyntax()
Definition: Syntax.h:235
Punctuation
Punctuation types.
Definition: Syntax.h:47
std::unordered_map< string, size_t > IdentifierMap
Definition: Syntax.h:38
StringSet _reservedWords
Definition: Syntax.h:217
string getArrayVariableSuffix(const TypeDesc *type, const Value &value) const
Definition: Syntax.h:168
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:62
Specialization of TypeSyntax for struct types.
Definition: Syntax.h:309
std::set< string > StringSet
A set of strings.
Definition: Library.h:64
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
static const string COMMA
Definition: Syntax.h:207
static const string STRING_QUOTE
Definition: Syntax.h:221
virtual const string & getBeginMultiLineComment() const
Return the characters used to begin a multi line comments block.
Definition: Syntax.h:154
Definition: Syntax.h:43
virtual const string & getIncludeStatement() const
Return the string pattern used for a file include statement.
Definition: Syntax.h:148
std::unordered_map< TypeDesc, size_t, TypeDesc::Hasher > _typeSyntaxIndexByType
Definition: Syntax.h:215
virtual const string & getInputQualifier() const
Definition: Syntax.h:122
string getValue(const TypeDesc *type, const Value &value, bool uniform=false) const
Definition: Syntax.h:114
static const string SINGLE_LINE_COMMENT
Definition: Syntax.h:223
const StringMap & getInvalidTokens() const
Returns a mapping from disallowed tokens to replacement strings for this language syntax...
Definition: Syntax.h:77
static const string BEGIN_MULTI_LINE_COMMENT
Definition: Syntax.h:224
static const string SEMICOLON
Definition: Syntax.h:206