2020-11-03 21:40:20 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SRC_AST_DECORATION_H_
|
|
|
|
#define SRC_AST_DECORATION_H_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <ostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-13 21:43:58 +00:00
|
|
|
#include "src/ast/node.h"
|
2020-11-03 21:40:20 +00:00
|
|
|
#include "src/source.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
/// The decoration kind enumerator
|
|
|
|
enum class DecorationKind {
|
|
|
|
kArray,
|
2020-11-18 19:33:30 +00:00
|
|
|
/*|*/ kStride,
|
2020-11-03 21:40:20 +00:00
|
|
|
kFunction,
|
2020-11-18 19:33:30 +00:00
|
|
|
/*|*/ kStage,
|
|
|
|
/*|*/ kWorkgroup,
|
2020-11-03 21:40:20 +00:00
|
|
|
kStruct,
|
|
|
|
kStructMember,
|
2020-11-18 19:33:30 +00:00
|
|
|
/*|*/ kStructMemberOffset,
|
2020-11-19 18:55:01 +00:00
|
|
|
kType,
|
|
|
|
/*|*/ kAccess,
|
2020-11-18 19:33:30 +00:00
|
|
|
kVariable,
|
|
|
|
/*|*/ kBinding,
|
|
|
|
/*|*/ kBuiltin,
|
|
|
|
/*|*/ kConstantId,
|
|
|
|
/*|*/ kLocation,
|
2020-11-03 21:40:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& out, DecorationKind data);
|
|
|
|
|
|
|
|
/// The base class for all decorations
|
2020-11-13 21:43:58 +00:00
|
|
|
class Decoration : public Node {
|
2020-11-03 21:40:20 +00:00
|
|
|
public:
|
2020-11-13 21:43:58 +00:00
|
|
|
~Decoration() override;
|
2020-11-03 21:40:20 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
/// @return the decoration kind
|
|
|
|
virtual DecorationKind GetKind() const = 0;
|
|
|
|
|
2020-11-18 19:33:30 +00:00
|
|
|
/// @param kind the decoration kind
|
|
|
|
/// @return true if this Decoration is of the (or derives from) the given
|
|
|
|
/// kind.
|
|
|
|
virtual bool IsKind(DecorationKind kind) const = 0;
|
2020-11-03 21:40:20 +00:00
|
|
|
|
|
|
|
/// @return true if this decoration is of (or derives from) type |TO|
|
|
|
|
template <typename TO>
|
|
|
|
bool Is() const {
|
2020-11-18 19:33:30 +00:00
|
|
|
return IsKind(TO::Kind);
|
2020-11-03 21:40:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 21:43:58 +00:00
|
|
|
/// @returns true if the node is valid
|
|
|
|
bool IsValid() const override;
|
2020-11-03 21:48:20 +00:00
|
|
|
|
2020-11-03 21:40:20 +00:00
|
|
|
protected:
|
|
|
|
/// Constructor
|
2020-11-03 21:48:20 +00:00
|
|
|
/// @param source the source of this decoration
|
2020-11-30 23:30:58 +00:00
|
|
|
explicit Decoration(const Source& source) : Node(source) {}
|
2020-11-03 21:40:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// As dynamically casts |deco| to the target type |TO|.
|
2020-11-16 16:31:07 +00:00
|
|
|
/// @return the cast decoration, or nullptr if |deco| is not of the type |TO|.
|
2020-11-03 21:40:20 +00:00
|
|
|
template <typename TO>
|
2020-11-16 16:31:07 +00:00
|
|
|
TO* As(Decoration* deco) {
|
2020-11-03 21:40:20 +00:00
|
|
|
if (deco == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (deco->Is<TO>()) {
|
2020-11-16 16:31:07 +00:00
|
|
|
return static_cast<TO*>(deco);
|
2020-11-03 21:40:20 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
/// A list of decorations
|
|
|
|
using DecorationList = std::vector<Decoration*>;
|
2020-11-03 21:40:20 +00:00
|
|
|
|
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_DECORATION_H_
|