blob: 4fa2482d8bd6c6b706cfa0574b76593e9ba47ba0 [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 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#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/**
Ethan Nicholas0df1b042017-03-31 13:56:23 -040019 * A function declaration or definition. The fBody field will be null for declarations.
ethannicholasb3058bd2016-07-01 08:22:01 -070020 */
21struct ASTFunction : public ASTDeclaration {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070022 ASTFunction(int offset, Modifiers modifiers, std::unique_ptr<ASTType> returnType,
23 StringFragment name, std::vector<std::unique_ptr<ASTParameter>> parameters,
ethannicholasb3058bd2016-07-01 08:22:01 -070024 std::unique_ptr<ASTBlock> body)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070025 : INHERITED(offset, kFunction_Kind)
Ethan Nicholascb670962017-04-20 19:31:52 -040026 , fModifiers(modifiers)
ethannicholasb3058bd2016-07-01 08:22:01 -070027 , fReturnType(std::move(returnType))
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070028 , fName(name)
ethannicholasb3058bd2016-07-01 08:22:01 -070029 , fParameters(std::move(parameters))
30 , fBody(std::move(body)) {}
31
Ethan Nicholas0df1b042017-03-31 13:56:23 -040032 String description() const override {
33 String result = fReturnType->description() + " " + fName + "(";
ethannicholasb3058bd2016-07-01 08:22:01 -070034 for (size_t i = 0; i < fParameters.size(); i++) {
35 if (i > 0) {
36 result += ", ";
37 }
38 result += fParameters[i]->description();
39 }
40 if (fBody) {
41 result += ") " + fBody->description();
42 } else {
43 result += ");";
44 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040045 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -070046 }
47
Ethan Nicholascb670962017-04-20 19:31:52 -040048 const Modifiers fModifiers;
ethannicholasb3058bd2016-07-01 08:22:01 -070049 const std::unique_ptr<ASTType> fReturnType;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070050 const StringFragment fName;
ethannicholasb3058bd2016-07-01 08:22:01 -070051 const std::vector<std::unique_ptr<ASTParameter>> fParameters;
52 const std::unique_ptr<ASTBlock> fBody;
53
54 typedef ASTDeclaration INHERITED;
55};
56
57} // namespace
58
59#endif