blob: 32f4da71f23a65e6bcdc8ff2b8264fc03a047921 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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#ifndef SKSL_ASTFUNCTION
9#define SKSL_ASTFUNCTION
10
11#include "SkSLASTBlock.h"
12#include "SkSLASTDeclaration.h"
13#include "SkSLASTParameter.h"
14#include "SkSLASTType.h"
15
16namespace SkSL {
17
18/**
19 * A function declaration or definition. The fBody field will be null for declarations.
20 */
21struct ASTFunction : public ASTDeclaration {
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050022 ASTFunction(Position position, std::unique_ptr<ASTType> returnType, SkString name,
ethannicholasb3058bd2016-07-01 08:22:01 -070023 std::vector<std::unique_ptr<ASTParameter>> parameters,
24 std::unique_ptr<ASTBlock> body)
25 : INHERITED(position, kFunction_Kind)
26 , fReturnType(std::move(returnType))
27 , fName(std::move(name))
28 , fParameters(std::move(parameters))
29 , fBody(std::move(body)) {}
30
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050031 SkString description() const override {
32 SkString result = fReturnType->description() + " " + fName + "(";
ethannicholasb3058bd2016-07-01 08:22:01 -070033 for (size_t i = 0; i < fParameters.size(); i++) {
34 if (i > 0) {
35 result += ", ";
36 }
37 result += fParameters[i]->description();
38 }
39 if (fBody) {
40 result += ") " + fBody->description();
41 } else {
42 result += ");";
43 }
44 return result;
45 }
46
47 const std::unique_ptr<ASTType> fReturnType;
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050048 const SkString fName;
ethannicholasb3058bd2016-07-01 08:22:01 -070049 const std::vector<std::unique_ptr<ASTParameter>> fParameters;
50 const std::unique_ptr<ASTBlock> fBody;
51
52 typedef ASTDeclaration INHERITED;
53};
54
55} // namespace
56
57#endif