gfx/angle/src/compiler/DirectiveHandler.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/compiler/DirectiveHandler.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +//
     1.5 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
     1.6 +// Use of this source code is governed by a BSD-style license that can be
     1.7 +// found in the LICENSE file.
     1.8 +//
     1.9 +
    1.10 +#include "compiler/DirectiveHandler.h"
    1.11 +
    1.12 +#include <sstream>
    1.13 +
    1.14 +#include "compiler/compiler_debug.h"
    1.15 +#include "compiler/Diagnostics.h"
    1.16 +
    1.17 +static TBehavior getBehavior(const std::string& str)
    1.18 +{
    1.19 +    static const std::string kRequire("require");
    1.20 +    static const std::string kEnable("enable");
    1.21 +    static const std::string kDisable("disable");
    1.22 +    static const std::string kWarn("warn");
    1.23 +
    1.24 +    if (str == kRequire) return EBhRequire;
    1.25 +    else if (str == kEnable) return EBhEnable;
    1.26 +    else if (str == kDisable) return EBhDisable;
    1.27 +    else if (str == kWarn) return EBhWarn;
    1.28 +    return EBhUndefined;
    1.29 +}
    1.30 +
    1.31 +TDirectiveHandler::TDirectiveHandler(TExtensionBehavior& extBehavior,
    1.32 +                                     TDiagnostics& diagnostics)
    1.33 +    : mExtensionBehavior(extBehavior),
    1.34 +      mDiagnostics(diagnostics)
    1.35 +{
    1.36 +}
    1.37 +
    1.38 +TDirectiveHandler::~TDirectiveHandler()
    1.39 +{
    1.40 +}
    1.41 +
    1.42 +void TDirectiveHandler::handleError(const pp::SourceLocation& loc,
    1.43 +                                    const std::string& msg)
    1.44 +{
    1.45 +    mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc, msg, "", "");
    1.46 +}
    1.47 +
    1.48 +void TDirectiveHandler::handlePragma(const pp::SourceLocation& loc,
    1.49 +                                     const std::string& name,
    1.50 +                                     const std::string& value)
    1.51 +{
    1.52 +    static const std::string kSTDGL("STDGL");
    1.53 +    static const std::string kOptimize("optimize");
    1.54 +    static const std::string kDebug("debug");
    1.55 +    static const std::string kOn("on");
    1.56 +    static const std::string kOff("off");
    1.57 +
    1.58 +    bool invalidValue = false;
    1.59 +    if (name == kSTDGL)
    1.60 +    {
    1.61 +        // The STDGL pragma is used to reserve pragmas for use by future
    1.62 +        // revisions of GLSL. Ignore it.
    1.63 +        return;
    1.64 +    }
    1.65 +    else if (name == kOptimize)
    1.66 +    {
    1.67 +        if (value == kOn) mPragma.optimize = true;
    1.68 +        else if (value == kOff) mPragma.optimize = false;
    1.69 +        else invalidValue = true;
    1.70 +    }
    1.71 +    else if (name == kDebug)
    1.72 +    {
    1.73 +        if (value == kOn) mPragma.debug = true;
    1.74 +        else if (value == kOff) mPragma.debug = false;
    1.75 +        else invalidValue = true;
    1.76 +    }
    1.77 +    else
    1.78 +    {
    1.79 +        mDiagnostics.report(pp::Diagnostics::UNRECOGNIZED_PRAGMA, loc, name);
    1.80 +        return;
    1.81 +    }
    1.82 +
    1.83 +    if (invalidValue)
    1.84 +      mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc,
    1.85 +                             "invalid pragma value", value,
    1.86 +                             "'on' or 'off' expected");
    1.87 +}
    1.88 +
    1.89 +void TDirectiveHandler::handleExtension(const pp::SourceLocation& loc,
    1.90 +                                        const std::string& name,
    1.91 +                                        const std::string& behavior)
    1.92 +{
    1.93 +    static const std::string kExtAll("all");
    1.94 +
    1.95 +    TBehavior behaviorVal = getBehavior(behavior);
    1.96 +    if (behaviorVal == EBhUndefined)
    1.97 +    {
    1.98 +        mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc,
    1.99 +                               "behavior", name, "invalid");
   1.100 +        return;
   1.101 +    }
   1.102 +
   1.103 +    if (name == kExtAll)
   1.104 +    {
   1.105 +        if (behaviorVal == EBhRequire)
   1.106 +        {
   1.107 +            mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc,
   1.108 +                                   "extension", name,
   1.109 +                                   "cannot have 'require' behavior");
   1.110 +        }
   1.111 +        else if (behaviorVal == EBhEnable)
   1.112 +        {
   1.113 +            mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc,
   1.114 +                                   "extension", name,
   1.115 +                                   "cannot have 'enable' behavior");
   1.116 +        }
   1.117 +        else
   1.118 +        {
   1.119 +            for (TExtensionBehavior::iterator iter = mExtensionBehavior.begin();
   1.120 +                 iter != mExtensionBehavior.end(); ++iter)
   1.121 +                iter->second = behaviorVal;
   1.122 +        }
   1.123 +        return;
   1.124 +    }
   1.125 +
   1.126 +    TExtensionBehavior::iterator iter = mExtensionBehavior.find(name);
   1.127 +    if (iter != mExtensionBehavior.end())
   1.128 +    {
   1.129 +        iter->second = behaviorVal;
   1.130 +        return;
   1.131 +    }
   1.132 +
   1.133 +    pp::Diagnostics::Severity severity = pp::Diagnostics::ERROR;
   1.134 +    switch (behaviorVal) {
   1.135 +      case EBhRequire:
   1.136 +        severity = pp::Diagnostics::ERROR;
   1.137 +        break;
   1.138 +      case EBhEnable:
   1.139 +      case EBhWarn:
   1.140 +      case EBhDisable:
   1.141 +        severity = pp::Diagnostics::WARNING;
   1.142 +        break;
   1.143 +      default:
   1.144 +        UNREACHABLE();
   1.145 +        break;
   1.146 +    }
   1.147 +    mDiagnostics.writeInfo(severity, loc,
   1.148 +                           "extension", name, "is not supported");
   1.149 +}
   1.150 +
   1.151 +void TDirectiveHandler::handleVersion(const pp::SourceLocation& loc,
   1.152 +                                      int version)
   1.153 +{
   1.154 +    static const int kVersion = 100;
   1.155 +
   1.156 +    if (version != kVersion)
   1.157 +    {
   1.158 +        std::stringstream stream;
   1.159 +        stream << version;
   1.160 +        std::string str = stream.str();
   1.161 +        mDiagnostics.writeInfo(pp::Diagnostics::ERROR, loc,
   1.162 +                               "version number", str, "not supported");
   1.163 +    }
   1.164 +}

mercurial