1//
2// Copyright (c) 2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "GLSLANG/ShaderLang.h"
8#include "compiler/intermediate.h"
9
10class TInfoSinkBase;
11
12struct TLoopInfo {
13    struct TIndex {
14        int id;  // symbol id.
15    } index;
16};
17typedef TVector<TLoopInfo> TLoopStack;
18
19// Traverses intermediate tree to ensure that the shader does not exceed the
20// minimum functionality mandated in GLSL 1.0 spec, Appendix A.
21class ValidateLimitations : public TIntermTraverser {
22public:
23    ValidateLimitations(ShShaderType shaderType, TInfoSinkBase& sink);
24
25    int numErrors() const { return mNumErrors; }
26
27    virtual void visitSymbol(TIntermSymbol*);
28    virtual void visitConstantUnion(TIntermConstantUnion*);
29    virtual bool visitBinary(Visit, TIntermBinary*);
30    virtual bool visitUnary(Visit, TIntermUnary*);
31    virtual bool visitSelection(Visit, TIntermSelection*);
32    virtual bool visitAggregate(Visit, TIntermAggregate*);
33    virtual bool visitLoop(Visit, TIntermLoop*);
34    virtual bool visitBranch(Visit, TIntermBranch*);
35
36private:
37    void error(TSourceLoc loc, const char *reason, const char* token);
38
39    bool withinLoopBody() const;
40    bool isLoopIndex(const TIntermSymbol* symbol) const;
41    bool validateLoopType(TIntermLoop* node);
42    bool validateForLoopHeader(TIntermLoop* node, TLoopInfo* info);
43    bool validateForLoopInit(TIntermLoop* node, TLoopInfo* info);
44    bool validateForLoopCond(TIntermLoop* node, TLoopInfo* info);
45    bool validateForLoopExpr(TIntermLoop* node, TLoopInfo* info);
46    // Returns true if none of the loop indices is used as the argument to
47    // the given function out or inout parameter.
48    bool validateFunctionCall(TIntermAggregate* node);
49    bool validateOperation(TIntermOperator* node, TIntermNode* operand);
50
51    // Returns true if indexing does not exceed the minimum functionality
52    // mandated in GLSL 1.0 spec, Appendix A, Section 5.
53    bool isConstExpr(TIntermNode* node);
54    bool isConstIndexExpr(TIntermNode* node);
55    bool validateIndexing(TIntermBinary* node);
56
57    ShShaderType mShaderType;
58    TInfoSinkBase& mSink;
59    int mNumErrors;
60    TLoopStack mLoopStack;
61};
62
63