Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 8 | #include "include/private/SkSLString.h" |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 9 | #include "src/sksl/SkSLASTNode.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 10 | #include "src/sksl/SkSLOperators.h" |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 11 | |
| 12 | namespace SkSL { |
| 13 | |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 14 | #ifdef SK_DEBUG |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 15 | String ASTNode::description() const { |
| 16 | switch (fKind) { |
| 17 | case Kind::kNull: return ""; |
| 18 | case Kind::kBinary: |
| 19 | return "(" + this->begin()->description() + " " + |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 20 | getOperator().operatorName() + " " + |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 21 | (this->begin() + 1)->description() + ")"; |
| 22 | case Kind::kBlock: { |
| 23 | String result = "{\n"; |
| 24 | for (const auto& c : *this) { |
| 25 | result += c.description(); |
| 26 | result += "\n"; |
| 27 | } |
| 28 | result += "}"; |
| 29 | return result; |
| 30 | } |
| 31 | case Kind::kBool: |
| 32 | return getBool() ? "true" : "false"; |
| 33 | case Kind::kBreak: |
| 34 | return "break"; |
| 35 | case Kind::kCall: { |
| 36 | auto iter = this->begin(); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 37 | String result = (iter++)->description(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 38 | result += "("; |
| 39 | const char* separator = ""; |
| 40 | while (iter != this->end()) { |
| 41 | result += separator; |
| 42 | result += (iter++)->description(); |
| 43 | separator = ","; |
| 44 | } |
| 45 | result += ")"; |
| 46 | return result; |
| 47 | } |
| 48 | case Kind::kContinue: |
| 49 | return "continue"; |
| 50 | case Kind::kDiscard: |
| 51 | return "discard"; |
| 52 | case Kind::kDo: |
| 53 | return "do " + this->begin()->description() + " while (" + |
| 54 | (this->begin() + 1)->description() + ")"; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 55 | case Kind::kExtension: |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 56 | return "#extension " + getStringView(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 57 | case Kind::kField: |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 58 | return this->begin()->description() + "." + getStringView(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 59 | case Kind::kFile: { |
| 60 | String result; |
| 61 | for (const auto& c : *this) { |
| 62 | result += c.description(); |
| 63 | result += "\n"; |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | case Kind::kFloat: |
| 68 | return to_string(getFloat()); |
| 69 | case Kind::kFor: |
| 70 | return "for (" + this->begin()->description() + "; " + |
| 71 | (this->begin() + 1)->description() + "; " + (this->begin() + 2)->description() + |
| 72 | ") " + (this->begin() + 3)->description(); |
| 73 | case Kind::kFunction: { |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 74 | const FunctionData& fd = getFunctionData(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 75 | String result = fd.fModifiers.description(); |
| 76 | if (result.size()) { |
| 77 | result += " "; |
| 78 | } |
| 79 | auto iter = this->begin(); |
| 80 | result += (iter++)->description() + " " + fd.fName + "("; |
| 81 | const char* separator = ""; |
| 82 | for (size_t i = 0; i < fd.fParameterCount; ++i) { |
| 83 | result += separator; |
| 84 | result += (iter++)->description(); |
| 85 | separator = ", "; |
| 86 | } |
| 87 | result += ")"; |
| 88 | if (iter != this->end()) { |
| 89 | result += " " + (iter++)->description(); |
| 90 | SkASSERT(iter == this->end()); |
| 91 | } |
| 92 | else { |
| 93 | result += ";"; |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | case Kind::kIdentifier: |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 98 | return String(getStringView()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 99 | case Kind::kIndex: |
| 100 | return this->begin()->description() + "[" + (this->begin() + 1)->description() + "]"; |
| 101 | case Kind::kIf: { |
| 102 | String result; |
| 103 | if (getBool()) { |
| 104 | result = "@"; |
| 105 | } |
| 106 | auto iter = this->begin(); |
| 107 | result += "if (" + (iter++)->description() + ") "; |
| 108 | result += (iter++)->description(); |
| 109 | if (iter != this->end()) { |
| 110 | result += " else " + (iter++)->description(); |
| 111 | SkASSERT(iter == this->end()); |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | case Kind::kInt: |
| 116 | return to_string(getInt()); |
| 117 | case Kind::kInterfaceBlock: { |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 118 | const InterfaceBlockData& id = getInterfaceBlockData(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 119 | String result = id.fModifiers.description() + " " + id.fTypeName + " {\n"; |
| 120 | auto iter = this->begin(); |
| 121 | for (size_t i = 0; i < id.fDeclarationCount; ++i) { |
| 122 | result += (iter++)->description() + "\n"; |
| 123 | } |
| 124 | result += "} "; |
| 125 | result += id.fInstanceName; |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 126 | if (id.fIsArray) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 127 | result += "[" + (iter++)->description() + "]"; |
| 128 | } |
| 129 | SkASSERT(iter == this->end()); |
| 130 | result += ";"; |
| 131 | return result; |
| 132 | } |
| 133 | case Kind::kModifiers: |
| 134 | return getModifiers().description(); |
| 135 | case Kind::kParameter: { |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 136 | const ParameterData& pd = getParameterData(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 137 | auto iter = this->begin(); |
| 138 | String result = (iter++)->description() + " " + pd.fName; |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 139 | if (pd.fIsArray) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 140 | result += "[" + (iter++)->description() + "]"; |
| 141 | } |
| 142 | if (iter != this->end()) { |
| 143 | result += " = " + (iter++)->description(); |
| 144 | SkASSERT(iter == this->end()); |
| 145 | } |
| 146 | return result; |
| 147 | } |
| 148 | case Kind::kPostfix: |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 149 | return this->begin()->description() + getOperator().operatorName(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 150 | case Kind::kPrefix: |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 151 | return getOperator().operatorName() + this->begin()->description(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 152 | case Kind::kReturn: |
| 153 | if (this->begin() != this->end()) { |
| 154 | return "return " + this->begin()->description() + ";"; |
| 155 | } |
| 156 | return "return;"; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 157 | case Kind::kSwitchCase: { |
| 158 | auto iter = this->begin(); |
| 159 | String result; |
| 160 | if (*iter) { |
| 161 | result.appendf("case %s:\n", iter->description().c_str()); |
| 162 | } else { |
| 163 | result = "default:\n"; |
| 164 | } |
| 165 | for (++iter; iter != this->end(); ++iter) { |
| 166 | result += "\n" + iter->description(); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | case Kind::kSwitch: { |
| 171 | auto iter = this->begin(); |
| 172 | String result; |
| 173 | if (getBool()) { |
| 174 | result = "@"; |
| 175 | } |
| 176 | result += "switch (" + (iter++)->description() + ") {"; |
| 177 | for (; iter != this->end(); ++iter) { |
| 178 | result += iter->description() + "\n"; |
| 179 | } |
| 180 | result += "}"; |
| 181 | return result; |
| 182 | } |
| 183 | case Kind::kTernary: |
| 184 | return "(" + this->begin()->description() + " ? " + (this->begin() + 1)->description() + |
| 185 | " : " + (this->begin() + 2)->description() + ")"; |
| 186 | case Kind::kType: |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 187 | return String(getStringView()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 188 | case Kind::kVarDeclaration: { |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 189 | const VarData& vd = getVarData(); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 190 | String result(vd.fName); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 191 | auto iter = this->begin(); |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 192 | if (vd.fIsArray) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 193 | result += "[" + (iter++)->description() + "]"; |
| 194 | } |
| 195 | if (iter != this->end()) { |
| 196 | result += " = " + (iter++)->description(); |
| 197 | SkASSERT(iter == this->end()); |
| 198 | } |
| 199 | return result; |
| 200 | } |
| 201 | case Kind::kVarDeclarations: { |
| 202 | auto iter = this->begin(); |
| 203 | String result = (iter++)->description(); |
| 204 | if (result.size()) { |
| 205 | result += " "; |
| 206 | } |
| 207 | result += (iter++)->description(); |
| 208 | const char* separator = " "; |
| 209 | for (; iter != this->end(); ++iter) { |
| 210 | result += separator + iter->description(); |
| 211 | separator = ", "; |
| 212 | } |
| 213 | return result; |
| 214 | } |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 215 | case Kind::kWhile: { |
| 216 | return "while (" + this->begin()->description() + ") " + |
| 217 | (this->begin() + 1)->description(); |
| 218 | |
| 219 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 220 | default: |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 221 | SkDEBUGFAIL("unrecognized AST node kind"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 222 | return "<error>"; |
| 223 | } |
| 224 | } |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 225 | #endif |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 226 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 227 | } // namespace SkSL |