1/*
2 * Copyright 2021 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
8#include "src/sksl/ir/SkSLPostfixExpression.h"
9
10#include "include/sksl/SkSLErrorReporter.h"
11#include "src/sksl/SkSLAnalysis.h"
12#include "src/sksl/SkSLContext.h"
13#include "src/sksl/ir/SkSLVariableReference.h"
14
15namespace SkSL {
16
17std::unique_ptr<Expression> PostfixExpression::Convert(const Context& context,
18                                                       std::unique_ptr<Expression> base,
19                                                       Operator op) {
20    const Type& baseType = base->type();
21    if (!baseType.isNumber()) {
22        context.fErrors->error(base->fLine,
23                               "'" + String(op.operatorName()) + "' cannot operate on '" +
24                               baseType.displayName() + "'");
25        return nullptr;
26    }
27    if (!Analysis::UpdateVariableRefKind(base.get(), VariableRefKind::kReadWrite,
28                                         context.fErrors)) {
29        return nullptr;
30    }
31    return PostfixExpression::Make(context, std::move(base), op);
32}
33
34std::unique_ptr<Expression> PostfixExpression::Make(const Context&,
35                                                    std::unique_ptr<Expression> base,
36                                                    Operator op) {
37    SkASSERT(base->type().isNumber());
38    SkASSERT(Analysis::IsAssignable(*base));
39    return std::make_unique<PostfixExpression>(std::move(base), op);
40}
41
42}  // namespace SkSL
43