| 1 | /* |
|---|---|
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/sksl/SkSLUtil.h" |
| 9 | |
| 10 | #include "src/sksl/SkSLContext.h" |
| 11 | #include "src/sksl/SkSLStringStream.h" |
| 12 | |
| 13 | #ifndef __STDC_FORMAT_MACROS |
| 14 | #define __STDC_FORMAT_MACROS |
| 15 | #endif |
| 16 | |
| 17 | namespace SkSL { |
| 18 | |
| 19 | #if defined(SKSL_STANDALONE) || !SK_SUPPORT_GPU |
| 20 | StandaloneShaderCaps standaloneCaps; |
| 21 | #endif |
| 22 | |
| 23 | void sksl_abort() { |
| 24 | #ifdef SKSL_STANDALONE |
| 25 | abort(); |
| 26 | #else |
| 27 | sk_abort_no_print(); |
| 28 | exit(1); |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | void write_stringstream(const StringStream& s, OutputStream& out) { |
| 33 | out.write(s.str().c_str(), s.str().size()); |
| 34 | } |
| 35 | |
| 36 | bool is_assignment(Token::Kind op) { |
| 37 | switch (op) { |
| 38 | case Token::Kind::TK_EQ: // fall through |
| 39 | case Token::Kind::TK_PLUSEQ: // fall through |
| 40 | case Token::Kind::TK_MINUSEQ: // fall through |
| 41 | case Token::Kind::TK_STAREQ: // fall through |
| 42 | case Token::Kind::TK_SLASHEQ: // fall through |
| 43 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 44 | case Token::Kind::TK_SHLEQ: // fall through |
| 45 | case Token::Kind::TK_SHREQ: // fall through |
| 46 | case Token::Kind::TK_BITWISEOREQ: // fall through |
| 47 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
| 48 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 49 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 50 | case Token::Kind::TK_LOGICALXOREQ: // fall through |
| 51 | case Token::Kind::TK_LOGICALANDEQ: |
| 52 | return true; |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | Token::Kind remove_assignment(Token::Kind op) { |
| 59 | switch (op) { |
| 60 | case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS; |
| 61 | case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS; |
| 62 | case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR; |
| 63 | case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH; |
| 64 | case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT; |
| 65 | case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL; |
| 66 | case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR; |
| 67 | case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR; |
| 68 | case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR; |
| 69 | case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND; |
| 70 | case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR; |
| 71 | case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR; |
| 72 | case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND; |
| 73 | default: return op; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #if !defined(SKSL_STANDALONE) |
| 78 | bool type_to_grsltype(const Context& context, const Type& type, GrSLType* outType) { |
| 79 | if (type == *context.fFloat_Type) { *outType = kFloat_GrSLType; return true; } |
| 80 | if (type == *context.fHalf_Type) { *outType = kHalf_GrSLType; return true; } |
| 81 | if (type == *context.fFloat2_Type) { *outType = kFloat2_GrSLType; return true; } |
| 82 | if (type == *context.fHalf2_Type) { *outType = kHalf2_GrSLType; return true; } |
| 83 | if (type == *context.fFloat3_Type) { *outType = kFloat3_GrSLType; return true; } |
| 84 | if (type == *context.fHalf3_Type) { *outType = kHalf3_GrSLType; return true; } |
| 85 | if (type == *context.fFloat4_Type) { *outType = kFloat4_GrSLType; return true; } |
| 86 | if (type == *context.fHalf4_Type) { *outType = kHalf4_GrSLType; return true; } |
| 87 | if (type == *context.fFloat2x2_Type) { *outType = kFloat2x2_GrSLType; return true; } |
| 88 | if (type == *context.fHalf2x2_Type) { *outType = kHalf2x2_GrSLType; return true; } |
| 89 | if (type == *context.fFloat3x3_Type) { *outType = kFloat3x3_GrSLType; return true; } |
| 90 | if (type == *context.fHalf3x3_Type) { *outType = kHalf3x3_GrSLType; return true; } |
| 91 | if (type == *context.fFloat4x4_Type) { *outType = kFloat4x4_GrSLType; return true; } |
| 92 | if (type == *context.fHalf4x4_Type) { *outType = kHalf4x4_GrSLType; return true; } |
| 93 | if (type == *context.fVoid_Type) { *outType = kVoid_GrSLType; return true; } |
| 94 | return false; |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | } // namespace SkSL |
| 99 |