2020-03-02 20:47:43 +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.
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
#ifndef SRC_RESOLVER_RESOLVER_H_
|
|
|
|
#define SRC_RESOLVER_RESOLVER_H_
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-02-08 22:42:54 +00:00
|
|
|
#include <memory>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <string>
|
2020-04-06 18:43:56 +00:00
|
|
|
#include <unordered_map>
|
2020-11-03 16:26:09 +00:00
|
|
|
#include <vector>
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-02-08 22:42:54 +00:00
|
|
|
#include "src/intrinsic_table.h"
|
2021-01-29 16:43:41 +00:00
|
|
|
#include "src/program_builder.h"
|
2020-04-06 18:43:56 +00:00
|
|
|
#include "src/scope_stack.h"
|
2021-02-17 13:10:49 +00:00
|
|
|
#include "src/utils/unique_vector.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
namespace tint {
|
2020-04-06 18:43:56 +00:00
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
// Forward declarations
|
|
|
|
namespace ast {
|
2020-04-07 12:57:42 +00:00
|
|
|
class ArrayAccessorExpression;
|
2020-04-07 19:27:41 +00:00
|
|
|
class BinaryExpression;
|
2020-09-22 22:07:13 +00:00
|
|
|
class BitcastExpression;
|
2020-04-07 16:41:10 +00:00
|
|
|
class CallExpression;
|
2021-03-09 15:06:37 +00:00
|
|
|
class CaseStatement;
|
2020-04-07 12:46:30 +00:00
|
|
|
class ConstructorExpression;
|
2020-04-07 19:26:39 +00:00
|
|
|
class Function;
|
2020-04-07 12:57:27 +00:00
|
|
|
class IdentifierExpression;
|
2020-04-07 16:41:33 +00:00
|
|
|
class MemberAccessorExpression;
|
2021-03-17 14:24:04 +00:00
|
|
|
class ReturnStatement;
|
2020-04-07 19:27:11 +00:00
|
|
|
class UnaryOpExpression;
|
2020-04-06 18:43:56 +00:00
|
|
|
class Variable;
|
|
|
|
} // namespace ast
|
2021-02-16 18:45:45 +00:00
|
|
|
namespace semantic {
|
2021-03-15 10:43:11 +00:00
|
|
|
class Array;
|
2021-02-16 18:45:45 +00:00
|
|
|
class Statement;
|
|
|
|
} // namespace semantic
|
2021-03-15 10:43:11 +00:00
|
|
|
namespace type {
|
|
|
|
class Struct;
|
|
|
|
} // namespace type
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 15:08:48 +00:00
|
|
|
namespace resolver {
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// Resolves types for all items in the given tint program
|
|
|
|
class Resolver {
|
2020-03-02 20:47:43 +00:00
|
|
|
public:
|
|
|
|
/// Constructor
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param builder the program builder
|
2021-03-09 10:54:37 +00:00
|
|
|
explicit Resolver(ProgramBuilder* builder);
|
2021-01-25 18:14:08 +00:00
|
|
|
|
|
|
|
/// Destructor
|
2021-03-09 10:54:37 +00:00
|
|
|
~Resolver();
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// @returns error messages from the resolver
|
2021-02-17 20:13:34 +00:00
|
|
|
std::string error() const { return diagnostics_.str(); }
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// @returns true if the resolver was successful
|
|
|
|
bool Resolve();
|
2020-04-06 21:07:41 +00:00
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param type the given type
|
|
|
|
/// @returns true if the given type is storable.
|
|
|
|
static bool IsStorable(type::Type* type);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
private:
|
2021-02-16 18:45:45 +00:00
|
|
|
/// Structure holding semantic information about a variable.
|
2021-02-16 21:15:01 +00:00
|
|
|
/// Used to build the semantic::Variable nodes at the end of resolving.
|
2021-02-03 17:51:09 +00:00
|
|
|
struct VariableInfo {
|
|
|
|
explicit VariableInfo(ast::Variable* decl);
|
|
|
|
~VariableInfo();
|
|
|
|
|
|
|
|
ast::Variable* const declaration;
|
|
|
|
ast::StorageClass storage_class;
|
2021-02-16 21:15:01 +00:00
|
|
|
std::vector<ast::IdentifierExpression*> users;
|
2021-02-03 17:51:09 +00:00
|
|
|
};
|
|
|
|
|
2021-02-03 16:43:20 +00:00
|
|
|
/// Structure holding semantic information about a function.
|
|
|
|
/// Used to build the semantic::Function nodes at the end of resolving.
|
|
|
|
struct FunctionInfo {
|
|
|
|
explicit FunctionInfo(ast::Function* decl);
|
|
|
|
~FunctionInfo();
|
|
|
|
|
|
|
|
ast::Function* const declaration;
|
2021-02-03 17:51:09 +00:00
|
|
|
UniqueVector<VariableInfo*> referenced_module_vars;
|
|
|
|
UniqueVector<VariableInfo*> local_referenced_module_vars;
|
2021-03-17 14:24:04 +00:00
|
|
|
std::vector<const ast::ReturnStatement*> return_statements;
|
2021-03-03 19:54:44 +00:00
|
|
|
|
|
|
|
// List of transitive calls this function makes
|
|
|
|
UniqueVector<FunctionInfo*> transitive_calls;
|
2021-02-03 16:43:20 +00:00
|
|
|
};
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
/// Structure holding semantic information about an expression.
|
|
|
|
/// Used to build the semantic::Expression nodes at the end of resolving.
|
|
|
|
struct ExpressionInfo {
|
|
|
|
type::Type* type;
|
|
|
|
semantic::Statement* statement;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Structure holding semantic information about a call expression to an
|
|
|
|
/// ast::Function.
|
|
|
|
/// Used to build the semantic::Call nodes at the end of resolving.
|
|
|
|
struct FunctionCallInfo {
|
|
|
|
FunctionInfo* function;
|
|
|
|
semantic::Statement* statement;
|
|
|
|
};
|
|
|
|
|
2021-03-17 22:01:53 +00:00
|
|
|
/// Structure holding semantic information about a struct.
|
|
|
|
/// Used to build the semantic::Struct nodes at the end of resolving.
|
|
|
|
struct StructInfo {
|
|
|
|
StructInfo();
|
|
|
|
~StructInfo();
|
|
|
|
|
|
|
|
std::vector<semantic::StructMember*> members;
|
|
|
|
uint32_t align = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
uint32_t size_no_padding = 0;
|
|
|
|
};
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// Structure holding semantic information about a block (i.e. scope), such as
|
|
|
|
/// parent block and variables declared in the block.
|
|
|
|
/// Used to validate variable scoping rules.
|
|
|
|
struct BlockInfo {
|
2021-03-09 15:06:37 +00:00
|
|
|
enum class Type { kGeneric, kLoop, kLoopContinuing, kSwitchCase };
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
BlockInfo(Type type, BlockInfo* parent);
|
2021-03-09 10:26:57 +00:00
|
|
|
~BlockInfo();
|
|
|
|
|
|
|
|
template <typename Pred>
|
|
|
|
BlockInfo* FindFirstParent(Pred&& pred) {
|
|
|
|
BlockInfo* curr = this;
|
|
|
|
while (curr && !pred(curr)) {
|
|
|
|
curr = curr->parent;
|
|
|
|
}
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
BlockInfo* FindFirstParent(BlockInfo::Type ty) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return FindFirstParent(
|
2021-03-09 10:54:37 +00:00
|
|
|
[ty](auto* block_info) { return block_info->type == ty; });
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
Type const type;
|
|
|
|
BlockInfo* const parent;
|
2021-03-09 10:26:57 +00:00
|
|
|
std::vector<const ast::Variable*> decls;
|
|
|
|
|
|
|
|
// first_continue is set to the index of the first variable in decls
|
|
|
|
// declared after the first continue statement in a loop block, if any.
|
|
|
|
constexpr static size_t kNoContinue = size_t(~0);
|
|
|
|
size_t first_continue = kNoContinue;
|
|
|
|
};
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
/// Resolves the program, without creating final the semantic nodes.
|
|
|
|
/// @returns true on success, false on error
|
|
|
|
bool ResolveInternal();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
|
|
/// Creates the nodes and adds them to the semantic::Info mappings of the
|
|
|
|
/// ProgramBuilder.
|
|
|
|
void CreateSemanticNodes() const;
|
|
|
|
|
|
|
|
/// Retrieves information for the requested import.
|
|
|
|
/// @param src the source of the import
|
|
|
|
/// @param path the import path
|
|
|
|
/// @param name the method name to get information on
|
|
|
|
/// @param params the parameters to the method call
|
|
|
|
/// @param id out parameter for the external call ID. Must not be a nullptr.
|
|
|
|
/// @returns the return type of `name` in `path` or nullptr on error.
|
|
|
|
type::Type* GetImportData(const Source& src,
|
|
|
|
const std::string& path,
|
|
|
|
const std::string& name,
|
|
|
|
const ast::ExpressionList& params,
|
|
|
|
uint32_t* id);
|
|
|
|
|
|
|
|
void set_referenced_from_function_if_needed(VariableInfo* var, bool local);
|
2020-04-08 19:58:35 +00:00
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
// AST and Type traversal methods
|
|
|
|
// Each return true on success, false on failure.
|
|
|
|
bool ArrayAccessor(ast::ArrayAccessorExpression*);
|
2021-03-16 13:26:03 +00:00
|
|
|
bool ValidateBinary(ast::BinaryExpression* expr);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Binary(ast::BinaryExpression*);
|
|
|
|
bool Bitcast(ast::BitcastExpression*);
|
|
|
|
bool BlockStatement(const ast::BlockStatement*);
|
|
|
|
bool Call(ast::CallExpression*);
|
|
|
|
bool CaseStatement(ast::CaseStatement*);
|
|
|
|
bool Constructor(ast::ConstructorExpression*);
|
2021-03-15 21:21:33 +00:00
|
|
|
bool VectorConstructor(const type::Vector& vec_type,
|
|
|
|
const ast::ExpressionList& values);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Expression(ast::Expression*);
|
|
|
|
bool Expressions(const ast::ExpressionList&);
|
|
|
|
bool Function(ast::Function*);
|
|
|
|
bool Functions(const ast::FunctionList&);
|
|
|
|
bool Identifier(ast::IdentifierExpression*);
|
|
|
|
bool IfStatement(ast::IfStatement*);
|
|
|
|
bool IntrinsicCall(ast::CallExpression*, semantic::IntrinsicType);
|
|
|
|
bool MemberAccessor(ast::MemberAccessorExpression*);
|
|
|
|
bool Statement(ast::Statement*);
|
|
|
|
bool Statements(const ast::StatementList&);
|
|
|
|
bool UnaryOp(ast::UnaryOpExpression*);
|
|
|
|
bool VariableDeclStatement(const ast::VariableDeclStatement*);
|
|
|
|
bool VariableStorageClass(ast::Statement*);
|
|
|
|
|
|
|
|
/// @returns the semantic information for the array `arr`, building it if it
|
|
|
|
/// hasn't been constructed already. If an error is raised, nullptr is
|
|
|
|
/// returned.
|
|
|
|
const semantic::Array* Array(type::Array*);
|
|
|
|
|
2021-03-17 22:01:53 +00:00
|
|
|
/// @returns the StructInfo for the structure `str`, building it if it hasn't
|
|
|
|
/// been constructed already. If an error is raised, nullptr is returned.
|
|
|
|
StructInfo* Structure(type::Struct* str);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
|
|
/// @param align the output default alignment in bytes for the type `ty`
|
|
|
|
/// @param size the output default size in bytes for the type `ty`
|
|
|
|
/// @returns true on success, false on error
|
|
|
|
bool DefaultAlignAndSize(type::Type* ty, uint32_t& align, uint32_t& size);
|
2021-02-26 18:25:56 +00:00
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
VariableInfo* CreateVariableInfo(ast::Variable*);
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
/// @returns the resolved type of the ast::Expression `expr`
|
|
|
|
/// @param expr the expression
|
2021-02-09 17:38:05 +00:00
|
|
|
type::Type* TypeOf(ast::Expression* expr);
|
2021-01-29 16:43:41 +00:00
|
|
|
|
|
|
|
/// Creates a semantic::Expression node with the resolved type `type`, and
|
|
|
|
/// assigns this semantic node to the expression `expr`.
|
|
|
|
/// @param expr the expression
|
|
|
|
/// @param type the resolved type
|
2021-02-09 17:38:05 +00:00
|
|
|
void SetType(ast::Expression* expr, type::Type* type);
|
2021-01-29 16:43:41 +00:00
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
/// Constructs a new BlockInfo with the given type and with #current_block_ as
|
|
|
|
/// its parent, assigns this to #current_block_, and then calls `callback`.
|
|
|
|
/// The original #current_block_ is restored on exit.
|
|
|
|
template <typename F>
|
|
|
|
bool BlockScope(BlockInfo::Type type, F&& callback);
|
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
ProgramBuilder* const builder_;
|
2021-02-08 22:42:54 +00:00
|
|
|
std::unique_ptr<IntrinsicTable> const intrinsic_table_;
|
2021-02-17 20:13:34 +00:00
|
|
|
diag::List diagnostics_;
|
2021-03-09 15:06:37 +00:00
|
|
|
BlockInfo* current_block_ = nullptr;
|
2021-02-03 17:51:09 +00:00
|
|
|
ScopeStack<VariableInfo*> variable_stack_;
|
2021-02-03 16:43:20 +00:00
|
|
|
std::unordered_map<Symbol, FunctionInfo*> symbol_to_function_;
|
|
|
|
std::unordered_map<ast::Function*, FunctionInfo*> function_to_info_;
|
2021-02-03 17:51:09 +00:00
|
|
|
std::unordered_map<ast::Variable*, VariableInfo*> variable_to_info_;
|
2021-02-16 18:45:45 +00:00
|
|
|
std::unordered_map<ast::CallExpression*, FunctionCallInfo> function_calls_;
|
|
|
|
std::unordered_map<ast::Expression*, ExpressionInfo> expr_info_;
|
2021-03-17 22:01:53 +00:00
|
|
|
std::unordered_map<type::Struct*, StructInfo*> struct_info_;
|
2021-02-03 16:43:20 +00:00
|
|
|
FunctionInfo* current_function_ = nullptr;
|
2021-02-16 18:45:45 +00:00
|
|
|
semantic::Statement* current_statement_ = nullptr;
|
2021-02-03 17:51:09 +00:00
|
|
|
BlockAllocator<VariableInfo> variable_infos_;
|
2021-02-03 16:43:20 +00:00
|
|
|
BlockAllocator<FunctionInfo> function_infos_;
|
2021-03-17 22:01:53 +00:00
|
|
|
BlockAllocator<StructInfo> struct_infos_;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2021-03-09 15:08:48 +00:00
|
|
|
} // namespace resolver
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
#endif // SRC_RESOLVER_RESOLVER_H_
|