Skip to content

Commit

Permalink
treewide: Clean up type definition interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Sep 9, 2024
1 parent 2498740 commit 83d8d9c
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 122 deletions.
2 changes: 1 addition & 1 deletion include/vast/Dialect/Core/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_vast_op_interface(TypeDefinitionInterface)

add_vast_op_interface(SymbolInterface)
add_vast_op_interface(SymbolTableInterface)
add_vast_attr_interface(SymbolRefInterface)
add_vast_op_interface(TypeDefinition)
16 changes: 0 additions & 16 deletions include/vast/Dialect/Core/Interfaces/TypeDefinition.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024-present, Trail of Bits, Inc.
// Copyright (c) 2024, Trail of Bits, Inc.

#pragma once

Expand All @@ -9,22 +9,28 @@ VAST_RELAX_WARNINGS
#include <mlir/IR/BuiltinTypes.h>
#include <mlir/IR/Dialect.h>
#include <mlir/IR/OperationSupport.h>
#include <llvm/ADT/StringRef.h>
VAST_RELAX_WARNINGS

#include "vast/Util/Common.hpp"

#include <gap/coro/generator.hpp>

namespace vast {
namespace vast::core {

struct field_info_t
{
std::string name;
mlir_type type;
};

} // namespace vast

} // namespace vast::core

/// Include the generated interface declarations.
#include "vast/Interfaces/AggregateTypeDefinitionInterface.h.inc"
#include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.h.inc"

namespace vast::core {

using type_definition_interface = TypeDefinitionInterface;
using aggregate_interface = AggregateTypeDefinitionInterface;

} // namespace vast::core
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include "mlir/IR/OpBase.td"
include "vast/Dialect/Core/CoreTraits.td"
include "vast/Dialect/Core/Interfaces/Common.td"

def Core_TypeDefinition : Core_OpInterface< "TypeDefinition" > {
def Core_TypeDefinition : Core_OpInterface< "TypeDefinitionInterface" > {
let description = [{
This interface allows to retrieve defined type by an operations and its name.
}];
Expand All @@ -22,18 +22,40 @@ def Core_TypeDefinition : Core_OpInterface< "TypeDefinition" > {
mlir::SymbolTable::getSymbolAttrName()
).getValue();
}

VAST_FATAL("Operation does not have a defined name.");
}]
>,
InterfaceMethod<"Returns the defined type by this operation.",
"mlir::Type", "getDefinedType", (ins), [{}],
/*defaultImplementation=*/ [{
auto op = this->getOperation();
return op->template getAttrOfType< mlir::Type >(
return op->template getAttrOfType< mlir::TypeAttr >(
"type"
);
).getValue();
}]
>
];
}

def Core_AggregateTypeDefinition : Core_OpInterface<
"AggregateTypeDefinitionInterface", [Core_TypeDefinition]
> {
let description = [{
This operation is encoding of a type.
}];

let methods = [
InterfaceMethod< "Returns element in order of their declaration.",
"gap::generator< mlir::Type >", "getFieldTypes", (ins), [{}] >,

InterfaceMethod< "Return all elements in order of their declaration.",
"gap::generator< vast::core::field_info_t >", "getFieldsInfo", (ins), [{}] >,

InterfaceMethod< "Return all nested definitions",
"gap::generator< vast::core::AggregateTypeDefinitionInterface >",
"getNestedDeclarations", (ins), [{}] >,
];
}

#endif // VAST_CORE_TYPE_DEFINITION
3 changes: 2 additions & 1 deletion include/vast/Dialect/HighLevel/HighLevelOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ VAST_UNRELAX_WARNINGS
#include "vast/Dialect/Core/SymbolTable.hpp"

#include "vast/Dialect/Core/Interfaces/SymbolInterface.hpp"
#include "vast/Interfaces/AggregateTypeDefinitionInterface.hpp"
#include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.hpp"

#include "vast/Interfaces/TypeTraitExprInterface.hpp"
#include "vast/Interfaces/AST/DeclInterface.hpp"

Expand Down
38 changes: 15 additions & 23 deletions include/vast/Dialect/HighLevel/HighLevelOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ include "mlir/IR/BuiltinAttributes.td"

include "mlir/Interfaces/CastInterfaces.td"

include "vast/Interfaces/AggregateTypeDefinitionInterface.td"
include "vast/Interfaces/TypeTraitExprInterface.td"
include "vast/Dialect/Core/Interfaces/SymbolInterface.td"
include "vast/Dialect/Core/Interfaces/SymbolTableInterface.td"
include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.td"

include "vast/Dialect/HighLevel/HighLevelCF.td"

Expand Down Expand Up @@ -105,7 +105,10 @@ def HighLevel_TypeDeclOp
}

def HighLevel_TypeDefOp
: HighLevel_Op< "typedef", [Core_TypeSymbol] >
: HighLevel_Op< "typedef", [
Core_TypeSymbol, Core_TypeDefinition,
DeclareOpInterfaceMethods< Core_TypeDefinition, ["getDefinedType"] >
] >
, Arguments<(ins SymbolNameAttr:$sym_name, TypeAttr:$type)>
{
let summary = "VAST typedef operation";
Expand All @@ -117,26 +120,18 @@ def HighLevel_TypeDefOp
This operation corresponds to `clang::TypedefDecl`.
}];

let extraClassDeclaration = [{
mlir_type getDefinedType();
}];

let assemblyFormat = [{ $sym_name attr-dict `:` $type }];
}

def HighLevel_TypeAliasOp
: HighLevel_Op< "alias", [Core_TypeSymbol] >
: HighLevel_Op< "alias", [Core_TypeSymbol, Core_TypeDefinition] >
, Arguments<(ins SymbolNameAttr:$sym_name, TypeAttr:$type)>
{
let summary = "VAST type alias operation";
let description = [{
This operation corresponds to `clang::TypeAliasDecl`.
}];

let extraClassDeclaration = [{
mlir_type getDefinedType();
}];

let assemblyFormat = [{ $sym_name attr-dict `:` $type }];
}

Expand Down Expand Up @@ -213,7 +208,7 @@ def HighLevel_EnumConstantOp
def HighLevel_EnumRegion : Region< Core_HasOneBlock, "enum constants region" >;

def HighLevel_EnumDeclOp
: HighLevel_Op< "enum", [NoTerminator, Core_TypeSymbol] >
: HighLevel_Op< "enum", [NoTerminator, Core_TypeSymbol, Core_TypeDefinition] >
, Arguments<(ins SymbolNameAttr:$name, OptionalAttr<TypeAttr>:$type)>
{
let summary = "VAST enum declaration";
Expand Down Expand Up @@ -253,8 +248,9 @@ def HighLevel_EnumDeclOp
class HighLevel_RecordLikeDeclOp< string mnemonic, list< Trait > traits = [] >
: HighLevel_Op< mnemonic,
!listconcat(traits, [
NoTerminator, Core_TypeSymbol,
DeclareOpInterfaceMethods< AggregateTypeDefinition >
NoTerminator, Core_TypeSymbol, Core_AggregateTypeDefinition,
DeclareOpInterfaceMethods< Core_AggregateTypeDefinition >,
DeclareOpInterfaceMethods< Core_TypeDefinition, ["getDefinedType"] >,
])
>
, Arguments<(ins SymbolNameAttr:$sym_name)>
Expand Down Expand Up @@ -285,16 +281,14 @@ class HighLevel_RecordLikeDeclOp< string mnemonic, list< Trait > traits = [] >
return hl::get_field_types(*this);
}

gap::generator< vast::field_info_t > $cppClass::getFieldsInfo() {
gap::generator< vast::core::field_info_t > $cppClass::getFieldsInfo() {
return hl::get_fields_info(*this);
}

gap::generator< vast::AggregateTypeDefinitionInterface > $cppClass::getNestedDeclarations() {
gap::generator< vast::core::aggregate_interface > $cppClass::getNestedDeclarations() {
return hl::get_nested_declarations(*this);
}

llvm::StringRef $cppClass::getDefinedName() { return this->getSymName(); }

mlir::Type $cppClass::getDefinedType() {
return hl::RecordType::get(getContext(), getDefinedName());
}
Expand All @@ -304,9 +298,7 @@ class HighLevel_RecordLikeDeclOp< string mnemonic, list< Trait > traits = [] >
}

def HighLevel_StructDeclOp
: HighLevel_RecordLikeDeclOp< "struct", [
DeclareOpInterfaceMethods< VAST_RecordDeclInterface >
] >
: HighLevel_RecordLikeDeclOp< "struct" >
{
let summary = "VAST struct declaration";
let description = [{ VAST struct declaration }];
Expand All @@ -327,8 +319,8 @@ def HighLevel_FieldDeclOp
let description = [{ VAST record field declaration }];

let extraClassDeclaration = [{
vast::AggregateTypeDefinitionInterface getParentAggregate() {
return mlir::dyn_cast< vast::AggregateTypeDefinitionInterface >((*this)->getParentOp());
vast::core::aggregate_interface getAggregate() {
return mlir::dyn_cast< vast::core::aggregate_interface >((*this)->getParentOp());
}
}];

Expand Down
20 changes: 9 additions & 11 deletions include/vast/Dialect/HighLevel/HighLevelUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@

namespace vast::hl {

using aggregate_interface = AggregateTypeDefinitionInterface;

static inline gap::generator< mlir_type > get_field_types(auto op) {
for (auto &&[_, type] : get_fields_info(op)) {
co_yield type;
}
}

gap::generator< field_info_t > get_fields_info(auto op) {
gap::generator< core::field_info_t > get_fields_info(auto op) {
for (auto &maybe_field : op.getOps()) {
// Definition of nested structure, we ignore not a field.
if (mlir::isa< aggregate_interface >(maybe_field)) {
if (mlir::isa< core::aggregate_interface >(maybe_field)) {
continue;
}

Expand All @@ -43,9 +41,9 @@ namespace vast::hl {
}
}

gap::generator< aggregate_interface > get_nested_declarations(auto op) {
gap::generator< core::aggregate_interface > get_nested_declarations(auto op) {
for (auto &maybe_field : op.getOps()) {
if (auto casted = mlir::dyn_cast< aggregate_interface >(maybe_field)) {
if (auto casted = mlir::dyn_cast< core::aggregate_interface >(maybe_field)) {
co_yield casted;
}
}
Expand All @@ -54,12 +52,12 @@ namespace vast::hl {
// TODO(hl): This is a placeholder that works in our test cases so far.
// In general, we will need generic resolution for scoping that
// will be used instead of this function.
aggregate_interface definition_of(mlir_type ty, auto scope) {
core::aggregate_interface definition_of(mlir_type ty, auto scope) {
auto type_name = hl::name_of_record(ty);
VAST_CHECK(type_name, "hl::name_of_record failed with {0}", ty);

aggregate_interface out;
auto walker = [&](aggregate_interface op) {
core::aggregate_interface out;
auto walker = [&](core::aggregate_interface op) {
if (op.getDefinedName() == type_name) {
out = op;
return walk_result::interrupt();
Expand Down Expand Up @@ -122,7 +120,7 @@ namespace vast::hl {
}
} // namespace detail

static inline auto field_index(string_ref name, aggregate_interface agg)
static inline auto field_index(string_ref name, core::aggregate_interface agg)
-> std::optional< std::size_t >
{
for (const auto &field : llvm::enumerate(detail::to_vector(agg.getFieldsInfo()))) {
Expand Down Expand Up @@ -150,7 +148,7 @@ namespace vast::hl {
}, scope, std::forward< decltype(yield) >(yield));
}

walk_result users(aggregate_interface op, auto scope, auto &&yield) {
walk_result users(core::aggregate_interface op, auto scope, auto &&yield) {
return type_users([&](mlir_type ty) {
if (auto rt = mlir::dyn_cast< RecordType >(ty))
return rt.getName() == op.getDefinedName();
Expand Down
33 changes: 0 additions & 33 deletions include/vast/Interfaces/AggregateTypeDefinitionInterface.td

This file was deleted.

1 change: 0 additions & 1 deletion include/vast/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
add_vast_op_interface(AggregateTypeDefinitionInterface)
add_vast_op_interface(TypeTraitExprInterface)
add_vast_attr_interface(TypeQualifiersInterfaces)
add_vast_type_interface(AliasTypeInterface)
Expand Down
2 changes: 2 additions & 0 deletions lib/vast/Dialect/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ add_vast_dialect_library(Core
LINK_LIBS PRIVATE
VASTAliasTypeInterface
)

add_subdirectory(Interfaces)
3 changes: 3 additions & 0 deletions lib/vast/Dialect/Core/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_vast_interface_library(TypeDefinitionInterface
TypeDefinitionInterface.cpp
)
10 changes: 10 additions & 0 deletions lib/vast/Dialect/Core/Interfaces/TypeDefinitionInterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024, Trail of Bits, Inc.

#include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.hpp"

//===----------------------------------------------------------------------===//
// Type Definition Interface
//===----------------------------------------------------------------------===//

/// Include the generated interface.
#include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.cpp.inc"
6 changes: 0 additions & 6 deletions lib/vast/Dialect/HighLevel/HighLevelOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,6 @@ namespace vast::hl
detail::build_record_like_decl(bld, st, name, fields);
}

std::vector< vast::ast::VAST_FieldDeclInterface > StructDeclOp::fields()
{
std::vector< vast::ast::VAST_FieldDeclInterface > result;
return result;
}

void UnionDeclOp::build(
Builder &bld, State &st, llvm::StringRef name,
maybe_builder_callback_ref fields
Expand Down
Loading

0 comments on commit 83d8d9c

Please sign in to comment.