1 /*
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 
16 namespace SkSL {
17 
18 /**
19  * A function declaration or definition. The fBody field will be null for declarations.
20  */
21 struct ASTFunction : public ASTDeclaration {
ASTFunctionASTFunction22     ASTFunction(int offset, Modifiers modifiers,  std::unique_ptr<ASTType> returnType,
23                 StringFragment name, std::vector<std::unique_ptr<ASTParameter>> parameters,
24                 std::unique_ptr<ASTBlock> body)
25     : INHERITED(offset, kFunction_Kind)
26     , fModifiers(modifiers)
27     , fReturnType(std::move(returnType))
28     , fName(name)
29     , fParameters(std::move(parameters))
30     , fBody(std::move(body)) {}
31 
descriptionASTFunction32     String description() const override {
33         String result = fReturnType->description() + " " + fName + "(";
34         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         }
45         return result;
46     }
47 
48     const Modifiers fModifiers;
49     const std::unique_ptr<ASTType> fReturnType;
50     const StringFragment fName;
51     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
60