|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 | import SwiftSyntax
|
13 | 13 |
|
| 14 | +extension ExprSyntax { |
| 15 | + // Is this an unresolved explicit cast? |
| 16 | + fileprivate var isUnresolvedExplicitCast: Bool { |
| 17 | + self.is(UnresolvedIsExprSyntax.self) || self.is(UnresolvedAsExprSyntax.self) |
| 18 | + } |
| 19 | +} |
14 | 20 | extension OperatorPrecedence {
|
15 | 21 | private struct PrecedenceBound {
|
16 | 22 | let groupName: PrecedenceGroupName?
|
@@ -69,6 +75,11 @@ extension OperatorPrecedence {
|
69 | 75 | return "AssignmentPrecedence"
|
70 | 76 | }
|
71 | 77 |
|
| 78 | + // Cast operators have fixed precedence. |
| 79 | + if expr.isUnresolvedExplicitCast { |
| 80 | + return "CastingPrecedence" |
| 81 | + } |
| 82 | + |
72 | 83 | // FIXME: Handle all of the language-defined precedence relationships.
|
73 | 84 | return nil
|
74 | 85 | }
|
@@ -116,6 +127,34 @@ extension OperatorPrecedence {
|
116 | 127 | )
|
117 | 128 | }
|
118 | 129 |
|
| 130 | + // An "is" type check. |
| 131 | + if let isExpr = op.as(UnresolvedIsExprSyntax.self) { |
| 132 | + // FIXME: Do we actually have a guarantee that the right-hand side is a |
| 133 | + // type expression here? |
| 134 | + return ExprSyntax( |
| 135 | + IsExprSyntax( |
| 136 | + expression: lhs, |
| 137 | + isExpr.unexpectedBeforeIsTok, |
| 138 | + isTok: isExpr.isTok, |
| 139 | + typeName: rhs.as(TypeExprSyntax.self)!.type) |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + // An "as" cast. |
| 144 | + if let asExpr = op.as(UnresolvedAsExprSyntax.self) { |
| 145 | + // FIXME: Do we actually have a guarantee that the right-hand side is a |
| 146 | + // type expression here? |
| 147 | + return ExprSyntax( |
| 148 | + AsExprSyntax( |
| 149 | + expression: lhs, |
| 150 | + asExpr.unexpectedBeforeAsTok, |
| 151 | + asTok: asExpr.asTok, |
| 152 | + asExpr.unexpectedBetweenAsTokAndQuestionOrExclamationMark, |
| 153 | + questionOrExclamationMark: asExpr.questionOrExclamationMark, |
| 154 | + typeName: rhs.as(TypeExprSyntax.self)!.type) |
| 155 | + ) |
| 156 | + } |
| 157 | + |
119 | 158 | // FIXME: Fallback that we should never need
|
120 | 159 | fatalError("Unknown binary operator")
|
121 | 160 | }
|
@@ -203,18 +242,22 @@ extension OperatorPrecedence {
|
203 | 242 | rest = rest.dropFirst()
|
204 | 243 |
|
205 | 244 | while !rest.isEmpty {
|
206 |
| - #if compiler(>=10.0) && false |
207 | 245 | // If the operator is a cast operator, the RHS can't extend past the type
|
208 | 246 | // that's part of the cast production.
|
209 |
| - if (isa<ExplicitCastExpr>(op1.op)) { |
210 |
| - LHS = makeBinOp(Ctx, op1.op, LHS, RHS, op1.precedence, S.empty()); |
211 |
| - op1 = getNextOperator(); |
212 |
| - if (!op1) return LHS; |
213 |
| - RHS = S[1]; |
214 |
| - S = S.slice(2); |
215 |
| - continue; |
| 247 | + if op1.isUnresolvedExplicitCast { |
| 248 | + lhs = Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs) |
| 249 | + guard let (newOp1, newOp1Precedence) = try getNextOperator() else { |
| 250 | + return lhs |
| 251 | + } |
| 252 | + |
| 253 | + op1 = newOp1 |
| 254 | + op1Precedence = newOp1Precedence |
| 255 | + |
| 256 | + rest = rest.dropFirst() |
| 257 | + rhs = rest.first! |
| 258 | + rest = rest.dropFirst() |
| 259 | + continue |
216 | 260 | }
|
217 |
| - #endif |
218 | 261 |
|
219 | 262 | // Pull out the next binary operator.
|
220 | 263 | let op2 = rest.first!
|
|
0 commit comments