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>
|
2021-03-17 22:47:33 +00:00
|
|
|
#include <unordered_set>
|
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-05-10 19:16:46 +00:00
|
|
|
#include "src/sem/binding_point.h"
|
2021-05-15 14:48:46 +00:00
|
|
|
#include "src/sem/block_statement.h"
|
2021-05-19 08:15:18 +00:00
|
|
|
#include "src/sem/function.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/struct.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;
|
2021-06-11 13:22:27 +00:00
|
|
|
class CallStatement;
|
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;
|
2021-05-15 14:48:46 +00:00
|
|
|
class LoopStatement;
|
2020-04-07 16:41:33 +00:00
|
|
|
class MemberAccessorExpression;
|
2021-03-17 14:24:04 +00:00
|
|
|
class ReturnStatement;
|
2021-03-25 12:55:27 +00:00
|
|
|
class SwitchStatement;
|
2020-04-07 19:27:11 +00:00
|
|
|
class UnaryOpExpression;
|
2020-04-06 18:43:56 +00:00
|
|
|
class Variable;
|
|
|
|
} // namespace ast
|
2021-04-16 19:07:51 +00:00
|
|
|
namespace sem {
|
2021-03-15 10:43:11 +00:00
|
|
|
class Array;
|
2021-06-17 19:56:14 +00:00
|
|
|
class Atomic;
|
2021-06-03 16:07:34 +00:00
|
|
|
class Intrinsic;
|
2021-02-16 18:45:45 +00:00
|
|
|
class Statement;
|
2021-04-16 19:07:51 +00:00
|
|
|
} // namespace sem
|
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-06-09 09:12:57 +00:00
|
|
|
/// @param type the given type
|
|
|
|
/// @returns true if the given type is a plain type
|
|
|
|
bool IsPlain(const sem::Type* type);
|
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
/// @param type the given type
|
2021-03-18 20:46:24 +00:00
|
|
|
/// @returns true if the given type is storable
|
2021-05-05 09:09:41 +00:00
|
|
|
bool IsStorable(const sem::Type* type);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-03-18 20:46:24 +00:00
|
|
|
/// @param type the given type
|
2021-03-18 21:03:24 +00:00
|
|
|
/// @returns true if the given type is host-shareable
|
2021-05-05 09:09:41 +00:00
|
|
|
bool IsHostShareable(const sem::Type* type);
|
2021-03-18 20:46:24 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
private:
|
2021-06-09 20:45:09 +00:00
|
|
|
/// Describes the context in which a variable is declared
|
|
|
|
enum class VariableKind { kParameter, kLocal, kGlobal };
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
/// Structure holding semantic information about a variable.
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Used to build the sem::Variable nodes at the end of resolving.
|
2021-02-03 17:51:09 +00:00
|
|
|
struct VariableInfo {
|
2021-04-28 13:50:43 +00:00
|
|
|
VariableInfo(const ast::Variable* decl,
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* type,
|
2021-05-14 17:51:13 +00:00
|
|
|
const std::string& type_name,
|
2021-05-18 10:28:48 +00:00
|
|
|
ast::StorageClass storage_class,
|
2021-06-09 20:45:09 +00:00
|
|
|
ast::Access ac,
|
|
|
|
VariableKind k);
|
2021-02-03 17:51:09 +00:00
|
|
|
~VariableInfo();
|
|
|
|
|
2021-04-28 12:38:13 +00:00
|
|
|
ast::Variable const* const declaration;
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* type;
|
2021-04-28 13:50:43 +00:00
|
|
|
std::string const type_name;
|
2021-02-03 17:51:09 +00:00
|
|
|
ast::StorageClass storage_class;
|
2021-06-04 20:41:47 +00:00
|
|
|
ast::Access const access;
|
2021-02-16 21:15:01 +00:00
|
|
|
std::vector<ast::IdentifierExpression*> users;
|
2021-05-10 19:16:46 +00:00
|
|
|
sem::BindingPoint binding_point;
|
2021-06-09 20:45:09 +00:00
|
|
|
VariableKind kind;
|
2021-02-03 17:51:09 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
struct IntrinsicCallInfo {
|
|
|
|
const ast::CallExpression* call;
|
|
|
|
const sem::Intrinsic* intrinsic;
|
|
|
|
};
|
|
|
|
|
2021-02-03 16:43:20 +00:00
|
|
|
/// Structure holding semantic information about a function.
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Used to build the sem::Function nodes at the end of resolving.
|
2021-02-03 16:43:20 +00:00
|
|
|
struct FunctionInfo {
|
|
|
|
explicit FunctionInfo(ast::Function* decl);
|
|
|
|
~FunctionInfo();
|
|
|
|
|
|
|
|
ast::Function* const declaration;
|
2021-04-09 13:56:08 +00:00
|
|
|
std::vector<VariableInfo*> parameters;
|
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-05-22 12:48:24 +00:00
|
|
|
std::vector<const ast::CallExpression*> callsites;
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* return_type = nullptr;
|
2021-04-30 19:24:29 +00:00
|
|
|
std::string return_type_name;
|
2021-05-19 08:15:18 +00:00
|
|
|
std::array<sem::WorkgroupDimension, 3> workgroup_size;
|
2021-06-03 16:07:34 +00:00
|
|
|
std::vector<IntrinsicCallInfo> intrinsic_calls;
|
2021-03-03 19:54:44 +00:00
|
|
|
|
|
|
|
// List of transitive calls this function makes
|
|
|
|
UniqueVector<FunctionInfo*> transitive_calls;
|
2021-06-03 16:07:34 +00:00
|
|
|
|
|
|
|
// List of entry point functions that transitively call this function
|
|
|
|
UniqueVector<FunctionInfo*> ancestor_entry_points;
|
2021-02-03 16:43:20 +00:00
|
|
|
};
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
/// Structure holding semantic information about an expression.
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Used to build the sem::Expression nodes at the end of resolving.
|
2021-02-16 18:45:45 +00:00
|
|
|
struct ExpressionInfo {
|
2021-05-20 15:10:48 +00:00
|
|
|
sem::Type const* type;
|
2021-04-28 13:50:43 +00:00
|
|
|
std::string const type_name; // Declared type name
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::Statement* statement;
|
2021-02-16 18:45:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Structure holding semantic information about a call expression to an
|
|
|
|
/// ast::Function.
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Used to build the sem::Call nodes at the end of resolving.
|
2021-02-16 18:45:45 +00:00
|
|
|
struct FunctionCallInfo {
|
|
|
|
FunctionInfo* function;
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::Statement* statement;
|
2021-02-16 18:45:45 +00:00
|
|
|
};
|
|
|
|
|
2021-05-18 10:28:48 +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 {
|
|
|
|
enum class Type { kGeneric, kLoop, kLoopContinuing, kSwitchCase };
|
|
|
|
|
|
|
|
BlockInfo(const ast::BlockStatement* block, Type type, BlockInfo* parent);
|
|
|
|
~BlockInfo();
|
|
|
|
|
|
|
|
template <typename Pred>
|
|
|
|
BlockInfo* FindFirstParent(Pred&& pred) {
|
|
|
|
BlockInfo* curr = this;
|
|
|
|
while (curr && !pred(curr)) {
|
|
|
|
curr = curr->parent;
|
|
|
|
}
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInfo* FindFirstParent(BlockInfo::Type ty) {
|
|
|
|
return FindFirstParent(
|
|
|
|
[ty](auto* block_info) { return block_info->type == ty; });
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::BlockStatement const* const block;
|
|
|
|
Type const type;
|
|
|
|
BlockInfo* const parent;
|
|
|
|
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-06-09 14:32:14 +00:00
|
|
|
// Structure holding information for a TypeDecl
|
|
|
|
struct TypeDeclInfo {
|
|
|
|
ast::TypeDecl const* const ast;
|
2021-05-20 08:44:57 +00:00
|
|
|
sem::Type* const sem;
|
|
|
|
};
|
|
|
|
|
2021-06-17 19:56:14 +00:00
|
|
|
// Structure holding a pointer to the sem::Struct and an index to a member of
|
|
|
|
// that structure.
|
|
|
|
struct StructMember {
|
|
|
|
sem::Struct* structure;
|
|
|
|
size_t index;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
bool ValidatePipelineStages();
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Creates the nodes and adds them to the sem::Info mappings of the
|
2021-02-03 17:51:09 +00:00
|
|
|
/// 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.
|
2021-04-19 22:51:23 +00:00
|
|
|
sem::Type* GetImportData(const Source& src,
|
|
|
|
const std::string& path,
|
|
|
|
const std::string& name,
|
|
|
|
const ast::ExpressionList& params,
|
|
|
|
uint32_t* id);
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
|
|
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-04-19 19:16:12 +00:00
|
|
|
bool Assignment(ast::AssignmentStatement* a);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Binary(ast::BinaryExpression*);
|
|
|
|
bool Bitcast(ast::BitcastExpression*);
|
|
|
|
bool Call(ast::CallExpression*);
|
|
|
|
bool CaseStatement(ast::CaseStatement*);
|
|
|
|
bool Constructor(ast::ConstructorExpression*);
|
|
|
|
bool Expression(ast::Expression*);
|
|
|
|
bool Expressions(const ast::ExpressionList&);
|
|
|
|
bool Function(ast::Function*);
|
2021-04-19 19:16:12 +00:00
|
|
|
bool GlobalVariable(ast::Variable* var);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Identifier(ast::IdentifierExpression*);
|
|
|
|
bool IfStatement(ast::IfStatement*);
|
2021-04-16 19:07:51 +00:00
|
|
|
bool IntrinsicCall(ast::CallExpression*, sem::IntrinsicType);
|
2021-06-09 20:17:59 +00:00
|
|
|
bool FunctionCall(const ast::CallExpression* call);
|
2021-05-15 14:48:46 +00:00
|
|
|
bool LoopStatement(ast::LoopStatement*);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool MemberAccessor(ast::MemberAccessorExpression*);
|
2021-04-19 19:16:12 +00:00
|
|
|
bool Parameter(ast::Variable* param);
|
|
|
|
bool Return(ast::ReturnStatement* ret);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Statement(ast::Statement*);
|
|
|
|
bool Statements(const ast::StatementList&);
|
2021-04-19 19:16:12 +00:00
|
|
|
bool Switch(ast::SwitchStatement* s);
|
2021-03-15 10:43:11 +00:00
|
|
|
bool UnaryOp(ast::UnaryOpExpression*);
|
|
|
|
bool VariableDeclStatement(const ast::VariableDeclStatement*);
|
|
|
|
|
2021-03-18 21:14:44 +00:00
|
|
|
// AST and Type validation methods
|
|
|
|
// Each return true on success, false on failure.
|
2021-05-07 20:58:34 +00:00
|
|
|
bool ValidateArray(const sem::Array* arr, const Source& source);
|
2021-04-21 16:45:02 +00:00
|
|
|
bool ValidateArrayStrideDecoration(const ast::StrideDecoration* deco,
|
|
|
|
uint32_t el_size,
|
|
|
|
uint32_t el_align,
|
|
|
|
const Source& source);
|
2021-06-17 19:56:14 +00:00
|
|
|
bool ValidateAtomic(const ast::Atomic* a, const sem::Atomic* s);
|
|
|
|
bool ValidateAtomicUses();
|
2021-04-19 19:16:12 +00:00
|
|
|
bool ValidateAssignment(const ast::AssignmentStatement* a);
|
2021-06-18 19:58:27 +00:00
|
|
|
bool ValidateBuiltinDecoration(const ast::BuiltinDecoration* deco,
|
|
|
|
const sem::Type* storage_type);
|
2021-06-11 13:22:27 +00:00
|
|
|
bool ValidateCallStatement(ast::CallStatement* stmt);
|
2021-04-21 16:13:42 +00:00
|
|
|
bool ValidateEntryPoint(const ast::Function* func, const FunctionInfo* info);
|
|
|
|
bool ValidateFunction(const ast::Function* func, const FunctionInfo* info);
|
2021-04-19 20:20:33 +00:00
|
|
|
bool ValidateGlobalVariable(const VariableInfo* var);
|
2021-06-21 17:08:05 +00:00
|
|
|
bool ValidateMatrix(const sem::Matrix* matirx_type, const Source& source);
|
2021-04-28 13:50:43 +00:00
|
|
|
bool ValidateMatrixConstructor(const ast::TypeConstructorExpression* ctor,
|
2021-05-05 09:09:41 +00:00
|
|
|
const sem::Matrix* matrix_type);
|
2021-05-10 18:01:51 +00:00
|
|
|
bool ValidateParameter(const VariableInfo* info);
|
2021-03-22 23:20:17 +00:00
|
|
|
bool ValidateReturn(const ast::ReturnStatement* ret);
|
2021-06-21 17:53:56 +00:00
|
|
|
bool ValidateStatements(const ast::StatementList& stmts);
|
2021-06-04 20:41:47 +00:00
|
|
|
bool ValidateStorageTexture(const ast::StorageTexture* t);
|
2021-05-07 14:49:34 +00:00
|
|
|
bool ValidateStructure(const sem::Struct* str);
|
2021-03-25 12:55:27 +00:00
|
|
|
bool ValidateSwitch(const ast::SwitchStatement* s);
|
2021-05-10 18:01:51 +00:00
|
|
|
bool ValidateVariable(const VariableInfo* info);
|
2021-05-18 10:28:48 +00:00
|
|
|
bool ValidateVariableConstructor(const ast::Variable* var,
|
|
|
|
const sem::Type* storage_type,
|
|
|
|
const std::string& type_name,
|
|
|
|
const sem::Type* rhs_type,
|
|
|
|
const std::string& rhs_type_name);
|
2021-04-28 13:50:43 +00:00
|
|
|
bool ValidateVectorConstructor(const ast::TypeConstructorExpression* ctor,
|
2021-05-05 09:09:41 +00:00
|
|
|
const sem::Vector* vec_type);
|
2021-06-18 15:32:21 +00:00
|
|
|
bool ValidateScalarConstructor(const ast::TypeConstructorExpression* ctor,
|
|
|
|
const sem::Type* type);
|
2021-06-11 15:51:26 +00:00
|
|
|
bool ValidateArrayConstructor(const ast::TypeConstructorExpression* ctor,
|
|
|
|
const sem::Array* arr_type);
|
2021-06-09 14:32:14 +00:00
|
|
|
bool ValidateTypeDecl(const ast::TypeDecl* named_type) const;
|
2021-06-09 18:53:57 +00:00
|
|
|
bool ValidateNoDuplicateDecorations(const ast::DecorationList& decorations);
|
2021-03-18 21:14:44 +00:00
|
|
|
|
2021-04-21 13:47:12 +00:00
|
|
|
/// @returns the sem::Type for the ast::Type `ty`, building it if it
|
|
|
|
/// hasn't been constructed already. If an error is raised, nullptr is
|
|
|
|
/// returned.
|
|
|
|
/// @param ty the ast::Type
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* Type(const ast::Type* ty);
|
2021-04-21 13:47:12 +00:00
|
|
|
|
2021-05-20 08:44:57 +00:00
|
|
|
/// @param named_type the named type to resolve
|
|
|
|
/// @returns the resolved semantic type
|
2021-06-09 14:32:14 +00:00
|
|
|
sem::Type* TypeDecl(const ast::TypeDecl* named_type);
|
2021-05-20 08:44:57 +00:00
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
/// Builds and returns the semantic information for the array `arr`.
|
|
|
|
/// This method does not mark the ast::Array node, nor attach the generated
|
|
|
|
/// semantic information to the AST node.
|
|
|
|
/// @returns the semantic Array information, or nullptr if an error is raised.
|
2021-04-16 01:15:43 +00:00
|
|
|
/// @param arr the Array to get semantic information for
|
2021-05-07 20:58:34 +00:00
|
|
|
sem::Array* Array(const ast::Array* arr);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
/// Builds and returns the semantic information for the structure `str`.
|
|
|
|
/// This method does not mark the ast::Struct node, nor attach the generated
|
|
|
|
/// semantic information to the AST node.
|
|
|
|
/// @returns the semantic Struct information, or nullptr if an error is
|
|
|
|
/// raised. raised, nullptr is returned.
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Struct* Structure(const ast::Struct* str);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-04-16 01:15:43 +00:00
|
|
|
/// @returns the VariableInfo for the variable `var`, building it if it hasn't
|
|
|
|
/// been constructed already. If an error is raised, nullptr is returned.
|
2021-05-12 10:41:21 +00:00
|
|
|
/// @note this method does not resolve the decorations as these are
|
|
|
|
/// context-dependent (global, local, parameter)
|
2021-04-16 01:15:43 +00:00
|
|
|
/// @param var the variable to create or return the `VariableInfo` for
|
2021-05-18 10:28:48 +00:00
|
|
|
/// @param kind what kind of variable we are declaring
|
|
|
|
VariableInfo* Variable(ast::Variable* var, VariableKind kind);
|
2021-04-16 01:15:43 +00:00
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
/// Records the storage class usage for the given type, and any transient
|
|
|
|
/// dependencies of the type. Validates that the type can be used for the
|
|
|
|
/// given storage class, erroring if it cannot.
|
2021-03-18 20:46:24 +00:00
|
|
|
/// @param sc the storage class to apply to the type and transitent types
|
|
|
|
/// @param ty the type to apply the storage class on
|
|
|
|
/// @param usage the Source of the root variable declaration that uses the
|
|
|
|
/// given type and storage class. Used for generating sensible error messages.
|
2021-03-17 22:47:33 +00:00
|
|
|
/// @returns true on success, false on error
|
2021-03-18 20:46:24 +00:00
|
|
|
bool ApplyStorageClassUsageToType(ast::StorageClass sc,
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* ty,
|
2021-04-16 01:15:43 +00:00
|
|
|
const Source& usage);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
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
|
2021-05-05 09:09:41 +00:00
|
|
|
bool DefaultAlignAndSize(const sem::Type* ty,
|
2021-04-16 01:15:43 +00:00
|
|
|
uint32_t& align,
|
2021-05-07 20:58:34 +00:00
|
|
|
uint32_t& size);
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-06-04 22:17:37 +00:00
|
|
|
/// @param storage_class the storage class
|
|
|
|
/// @returns the default access control for the given storage class
|
|
|
|
ast::AccessControl DefaultAccessForStorageClass(
|
|
|
|
ast::StorageClass storage_class);
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
/// @returns the resolved type of the ast::Expression `expr`
|
|
|
|
/// @param expr the expression
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* TypeOf(const ast::Expression* expr);
|
2021-04-28 13:50:43 +00:00
|
|
|
|
|
|
|
/// @returns the declared type name of the ast::Expression `expr`
|
|
|
|
/// @param expr the type name
|
|
|
|
std::string TypeNameOf(const ast::Expression* expr);
|
|
|
|
|
|
|
|
/// @returns the semantic type of the AST literal `lit`
|
|
|
|
/// @param lit the literal
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* TypeOf(const ast::Literal* lit);
|
2021-01-29 16:43:41 +00:00
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
/// Creates a sem::Expression node with the resolved type `type`, and
|
2021-01-29 16:43:41 +00:00
|
|
|
/// assigns this semantic node to the expression `expr`.
|
|
|
|
/// @param expr the expression
|
|
|
|
/// @param type the resolved type
|
2021-06-09 20:17:59 +00:00
|
|
|
void SetType(const ast::Expression* expr, const sem::Type* type);
|
2021-01-29 16:43:41 +00:00
|
|
|
|
2021-04-28 13:50:43 +00:00
|
|
|
/// Creates a sem::Expression node with the resolved type `type`, the declared
|
|
|
|
/// type name `type_name` and assigns this semantic node to the expression
|
|
|
|
/// `expr`.
|
|
|
|
/// @param expr the expression
|
|
|
|
/// @param type the resolved type
|
|
|
|
/// @param type_name the declared type name
|
2021-06-09 20:17:59 +00:00
|
|
|
void SetType(const ast::Expression* expr,
|
2021-05-20 15:10:48 +00:00
|
|
|
const sem::Type* type,
|
2021-04-28 13:50:43 +00:00
|
|
|
const std::string& type_name);
|
|
|
|
|
2021-05-19 13:40:08 +00:00
|
|
|
/// Resolve the value of a scalar const_expr.
|
|
|
|
/// @param expr the expression
|
|
|
|
/// @param result pointer to the where the result will be stored
|
|
|
|
/// @returns true on success, false on error
|
|
|
|
template <typename T>
|
|
|
|
bool GetScalarConstExprValue(ast::Expression* expr, T* result);
|
|
|
|
|
2021-05-15 14:48:46 +00:00
|
|
|
/// Constructs a new semantic BlockStatement 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.
|
2021-03-09 15:06:37 +00:00
|
|
|
template <typename F>
|
2021-05-15 14:48:46 +00:00
|
|
|
bool BlockScope(const ast::BlockStatement* block, F&& callback);
|
2021-03-09 15:06:37 +00:00
|
|
|
|
2021-03-18 15:43:14 +00:00
|
|
|
/// Returns a human-readable string representation of the vector type name
|
|
|
|
/// with the given parameters.
|
|
|
|
/// @param size the vector dimension
|
|
|
|
/// @param element_type scalar vector sub-element type
|
|
|
|
/// @return pretty string representation
|
2021-05-07 20:58:34 +00:00
|
|
|
std::string VectorPretty(uint32_t size, const sem::Type* element_type);
|
2021-03-18 15:43:14 +00:00
|
|
|
|
2021-04-19 19:16:12 +00:00
|
|
|
/// Mark records that the given AST node has been visited, and asserts that
|
|
|
|
/// the given node has not already been seen. Diamonds in the AST are illegal.
|
|
|
|
/// @param node the AST node.
|
2021-04-28 12:38:13 +00:00
|
|
|
void Mark(const ast::Node* node);
|
2021-04-19 19:16:12 +00:00
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
template <typename CALLBACK>
|
|
|
|
void TraverseCallChain(FunctionInfo* from,
|
|
|
|
FunctionInfo* to,
|
|
|
|
CALLBACK&& callback) const;
|
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
ProgramBuilder* const builder_;
|
2021-05-06 16:04:03 +00:00
|
|
|
diag::List& diagnostics_;
|
2021-02-08 22:42:54 +00:00
|
|
|
std::unique_ptr<IntrinsicTable> const intrinsic_table_;
|
2021-05-15 14:48:46 +00:00
|
|
|
sem::BlockStatement* 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_;
|
2021-06-03 16:07:34 +00:00
|
|
|
std::vector<FunctionInfo*> entry_points_;
|
2021-06-17 19:56:14 +00:00
|
|
|
std::vector<StructMember> atomic_members_;
|
2021-03-19 14:04:51 +00:00
|
|
|
std::unordered_map<const ast::Function*, FunctionInfo*> function_to_info_;
|
2021-03-26 12:47:58 +00:00
|
|
|
std::unordered_map<const ast::Variable*, VariableInfo*> variable_to_info_;
|
2021-06-09 20:17:59 +00:00
|
|
|
std::unordered_map<const ast::CallExpression*, FunctionCallInfo>
|
|
|
|
function_calls_;
|
2021-04-28 13:50:43 +00:00
|
|
|
std::unordered_map<const ast::Expression*, ExpressionInfo> expr_info_;
|
2021-06-09 14:32:14 +00:00
|
|
|
std::unordered_map<Symbol, TypeDeclInfo> named_type_info_;
|
2021-05-20 08:44:57 +00:00
|
|
|
|
2021-04-28 12:38:13 +00:00
|
|
|
std::unordered_set<const ast::Node*> marked_;
|
2021-05-13 20:32:32 +00:00
|
|
|
std::unordered_map<uint32_t, const VariableInfo*> constant_ids_;
|
2021-05-14 17:51:13 +00:00
|
|
|
|
2021-02-03 16:43:20 +00:00
|
|
|
FunctionInfo* current_function_ = nullptr;
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::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_;
|
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_
|