-#include <algorithm>
#include <cstring>
+#include <msp/core/algorithm.h>
#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/utils.h>
void ExpressionValidator::visit(VariableReference &var)
{
- if(var.declaration && constant_expression && !var.declaration->constant)
- error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
+ if(var.declaration && constant_expression)
+ {
+ if(!var.declaration->constant)
+ error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
+ else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
+ {
+ auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=var.declaration->layout->qualifiers.end())
+ error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
+ }
+ }
}
void ExpressionValidator::visit(InterfaceBlockReference &iface)
int flavour = -1;
for(unsigned i=0; i<swizzle.count; ++i)
{
- unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
+ unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
if(flavour==-1)
flavour = component_flavour;
else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
TraversingVisitor::visit(ternary);
}
+void ExpressionValidator::visit(StructDeclaration &strct)
+{
+ SetFlag set_struct(in_struct);
+ TraversingVisitor::visit(strct);
+}
+
void ExpressionValidator::visit(VariableDeclaration &var)
{
if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
var.layout->visit(*this);
if(var.init_expression)
{
- SetFlag set_const(constant_expression, var.constant);
+ ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT);
+ if(var.layout)
+ {
+ auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=var.layout->qualifiers.end())
+ const_kind = FIXED_CONSTANT;
+ }
+
+ SetForScope<ConstantKind> set_const(constant_expression, const_kind);
TraversingVisitor::visit(var.init_expression);
}
if(var.array_size)
{
- SetFlag set_const(constant_expression);
+ SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
TraversingVisitor::visit(var.array_size);
}
}
class ExpressionValidator: private Validator
{
private:
+ enum ConstantKind
+ {
+ NOT_CONSTANT,
+ FIXED_CONSTANT,
+ SPEC_CONSTANT
+ };
+
FunctionDeclaration *current_function = 0;
- bool constant_expression = false;
+ bool in_struct = false;
+ ConstantKind constant_expression = NOT_CONSTANT;
public:
void apply(Stage &s) { stage = &s; s.content.visit(*this); }
virtual void visit(BinaryExpression &);
virtual void visit(Assignment &);
virtual void visit(TernaryExpression &);
+ virtual void visit(StructDeclaration &);
virtual void visit(VariableDeclaration &);
virtual void visit(FunctionDeclaration &);
virtual void visit(Conditional &);
// Compile mode: module
-/* Expected output: vertex
-layout(location=0) in vec4 position;
-void main()
-{
- gl_Position = position;
-}
-*/
-
-/* Expected output: fragment
-layout(constant_id=953017667) const int max_colors = 2;
-struct ColorInfo
-{
- vec4 color;
-};
-layout(binding=23) uniform Colors
-{
- ColorInfo colors[max_colors];
-};
-layout(location=0) out vec4 frag_color;
-void main()
-{
- frag_color = colors[0].color;
-}
+/* Expected error:
+<test>:8: Reference to specialization constant 'max_colors' in a fixed constant expression
+<test>:8: Reference to specialization constant 'max_colors' in a fixed constant expression
*/