mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-21 02:39:11 +00:00
Initial commit
This commit is contained in:
32
src/ast/type/alias_type.cc
Normal file
32
src/ast/type/alias_type.cc
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/alias_type.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
AliasType::AliasType(const std::string& name, Type* subtype)
|
||||
: name_(name), subtype_(subtype) {
|
||||
assert(subtype_);
|
||||
}
|
||||
|
||||
AliasType::~AliasType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
59
src/ast/type/alias_type.h
Normal file
59
src/ast/type/alias_type.h
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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_TYPE_ALIAS_TYPE_H_
|
||||
#define SRC_AST_TYPE_ALIAS_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A type alias type. Holds a name a pointer to another type.
|
||||
class AliasType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param name the alias name
|
||||
/// @param subtype the alias'd type
|
||||
AliasType(const std::string& name, Type* subtype);
|
||||
/// Move constructor
|
||||
AliasType(AliasType&&) = default;
|
||||
~AliasType() override;
|
||||
|
||||
/// @returns true if the type is an alias type
|
||||
bool IsAlias() const override { return true; }
|
||||
|
||||
/// @returns the alias name
|
||||
const std::string& name() const { return name_; }
|
||||
/// @returns the alias type
|
||||
Type* type() const { return subtype_; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override {
|
||||
return "__alias_" + name_ + subtype_->type_name();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
Type* subtype_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_ALIAS_TYPE_H_
|
||||
58
src/ast/type/alias_type_test.cc
Normal file
58
src/ast/type/alias_type_test.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/alias_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/u32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using AliasTypeTest = testing::Test;
|
||||
|
||||
TEST_F(AliasTypeTest, Create) {
|
||||
U32Type u32;
|
||||
AliasType a{"a_type", &u32};
|
||||
EXPECT_EQ(a.name(), "a_type");
|
||||
EXPECT_EQ(a.type(), &u32);
|
||||
}
|
||||
|
||||
TEST_F(AliasTypeTest, Is) {
|
||||
I32Type i32;
|
||||
|
||||
AliasType at{"a", &i32};
|
||||
EXPECT_TRUE(at.IsAlias());
|
||||
EXPECT_FALSE(at.IsArray());
|
||||
EXPECT_FALSE(at.IsBool());
|
||||
EXPECT_FALSE(at.IsF32());
|
||||
EXPECT_FALSE(at.IsI32());
|
||||
EXPECT_FALSE(at.IsMatrix());
|
||||
EXPECT_FALSE(at.IsPointer());
|
||||
EXPECT_FALSE(at.IsStruct());
|
||||
EXPECT_FALSE(at.IsU32());
|
||||
EXPECT_FALSE(at.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(AliasTypeTest, TypeName) {
|
||||
I32Type i32;
|
||||
AliasType at{"Particle", &i32};
|
||||
EXPECT_EQ(at.type_name(), "__alias_Particle__i32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
30
src/ast/type/array_type.cc
Normal file
30
src/ast/type/array_type.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/array_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
ArrayType::ArrayType(Type* subtype) : subtype_(subtype) {}
|
||||
|
||||
ArrayType::ArrayType(Type* subtype, size_t size)
|
||||
: subtype_(subtype), size_(size) {}
|
||||
|
||||
ArrayType::~ArrayType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
73
src/ast/type/array_type.h
Normal file
73
src/ast/type/array_type.h
Normal file
@@ -0,0 +1,73 @@
|
||||
// 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_TYPE_ARRAY_TYPE_H_
|
||||
#define SRC_AST_TYPE_ARRAY_TYPE_H_
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// An array type. If size is zero then it is a runtime array.
|
||||
class ArrayType : public Type {
|
||||
public:
|
||||
/// Constructor for runtime array
|
||||
/// @param subtype the type of the array elements
|
||||
explicit ArrayType(Type* subtype);
|
||||
/// Constructor
|
||||
/// @param subtype the type of the array elements
|
||||
/// @param size the number of elements in the array
|
||||
ArrayType(Type* subtype, size_t size);
|
||||
/// Move constructor
|
||||
ArrayType(ArrayType&&) = default;
|
||||
~ArrayType() override;
|
||||
|
||||
/// @returns true if the type is an array type
|
||||
bool IsArray() const override { return true; }
|
||||
/// @returns true if this is a runtime array.
|
||||
/// i.e. the size is determined at runtime
|
||||
bool IsRuntimeArray() const { return size_ == 0; }
|
||||
|
||||
/// @returns the array type
|
||||
Type* type() const { return subtype_; }
|
||||
/// @returns the array size. Size is 0 for a runtime array
|
||||
size_t size() const { return size_; }
|
||||
|
||||
/// @returns the name for th type
|
||||
std::string type_name() const override {
|
||||
assert(subtype_);
|
||||
|
||||
std::string type_name = "__array" + subtype_->type_name();
|
||||
if (!IsRuntimeArray())
|
||||
type_name += "_" + std::to_string(size_);
|
||||
|
||||
return type_name;
|
||||
}
|
||||
|
||||
private:
|
||||
Type* subtype_ = nullptr;
|
||||
size_t size_ = 0;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_ARRAY_TYPE_H_
|
||||
69
src/ast/type/array_type_test.cc
Normal file
69
src/ast/type/array_type_test.cc
Normal file
@@ -0,0 +1,69 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/array_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/u32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using ArrayTypeTest = testing::Test;
|
||||
|
||||
TEST_F(ArrayTypeTest, CreateSizedArray) {
|
||||
U32Type u32;
|
||||
ArrayType arr{&u32, 3};
|
||||
EXPECT_EQ(arr.type(), &u32);
|
||||
EXPECT_EQ(arr.size(), 3);
|
||||
EXPECT_TRUE(arr.IsArray());
|
||||
EXPECT_FALSE(arr.IsRuntimeArray());
|
||||
}
|
||||
|
||||
TEST_F(ArrayTypeTest, CreateRuntimeArray) {
|
||||
U32Type u32;
|
||||
ArrayType arr{&u32};
|
||||
EXPECT_EQ(arr.type(), &u32);
|
||||
EXPECT_EQ(arr.size(), 0);
|
||||
EXPECT_TRUE(arr.IsArray());
|
||||
EXPECT_TRUE(arr.IsRuntimeArray());
|
||||
}
|
||||
|
||||
TEST_F(ArrayTypeTest, Is) {
|
||||
I32Type i32;
|
||||
|
||||
ArrayType arr{&i32, 3};
|
||||
EXPECT_FALSE(arr.IsAlias());
|
||||
EXPECT_TRUE(arr.IsArray());
|
||||
EXPECT_FALSE(arr.IsBool());
|
||||
EXPECT_FALSE(arr.IsF32());
|
||||
EXPECT_FALSE(arr.IsI32());
|
||||
EXPECT_FALSE(arr.IsMatrix());
|
||||
EXPECT_FALSE(arr.IsPointer());
|
||||
EXPECT_FALSE(arr.IsStruct());
|
||||
EXPECT_FALSE(arr.IsU32());
|
||||
EXPECT_FALSE(arr.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(ArrayTypeTest, TypeName) {
|
||||
I32Type i32;
|
||||
ArrayType arr{&i32, 3};
|
||||
EXPECT_EQ(arr.type_name(), "__array__i32_3");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
27
src/ast/type/bool_type.cc
Normal file
27
src/ast/type/bool_type.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/bool_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
BoolType::BoolType() = default;
|
||||
|
||||
BoolType::~BoolType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
46
src/ast/type/bool_type.h
Normal file
46
src/ast/type/bool_type.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_TYPE_BOOL_TYPE_H_
|
||||
#define SRC_AST_TYPE_BOOL_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A boolean type
|
||||
class BoolType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
BoolType();
|
||||
/// Move constructor
|
||||
BoolType(BoolType&&) = default;
|
||||
~BoolType() override;
|
||||
|
||||
/// @returns true if the type is a bool type
|
||||
bool IsBool() const override { return true; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override { return "__bool"; }
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_BOOL_TYPE_H_
|
||||
46
src/ast/type/bool_type_test.cc
Normal file
46
src/ast/type/bool_type_test.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/bool_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using BoolTypeTest = testing::Test;
|
||||
|
||||
TEST_F(BoolTypeTest, Is) {
|
||||
BoolType b;
|
||||
EXPECT_FALSE(b.IsAlias());
|
||||
EXPECT_FALSE(b.IsArray());
|
||||
EXPECT_TRUE(b.IsBool());
|
||||
EXPECT_FALSE(b.IsF32());
|
||||
EXPECT_FALSE(b.IsI32());
|
||||
EXPECT_FALSE(b.IsMatrix());
|
||||
EXPECT_FALSE(b.IsPointer());
|
||||
EXPECT_FALSE(b.IsStruct());
|
||||
EXPECT_FALSE(b.IsU32());
|
||||
EXPECT_FALSE(b.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(BoolTypeTest, TypeName) {
|
||||
BoolType b;
|
||||
EXPECT_EQ(b.type_name(), "__bool");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
27
src/ast/type/f32_type.cc
Normal file
27
src/ast/type/f32_type.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/f32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
F32Type::F32Type() = default;
|
||||
|
||||
F32Type::~F32Type() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
46
src/ast/type/f32_type.h
Normal file
46
src/ast/type/f32_type.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_TYPE_F32_TYPE_H_
|
||||
#define SRC_AST_TYPE_F32_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A float 32 type
|
||||
class F32Type : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
F32Type();
|
||||
/// Move constructor
|
||||
F32Type(F32Type&&) = default;
|
||||
~F32Type() override;
|
||||
|
||||
/// @returns true if the type is an f32 type
|
||||
bool IsF32() const override { return true; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override { return "__f32"; }
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_F32_TYPE_H_
|
||||
46
src/ast/type/f32_type_test.cc
Normal file
46
src/ast/type/f32_type_test.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/f32_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using F32TypeTest = testing::Test;
|
||||
|
||||
TEST_F(F32TypeTest, Is) {
|
||||
F32Type f;
|
||||
EXPECT_FALSE(f.IsAlias());
|
||||
EXPECT_FALSE(f.IsArray());
|
||||
EXPECT_FALSE(f.IsBool());
|
||||
EXPECT_TRUE(f.IsF32());
|
||||
EXPECT_FALSE(f.IsI32());
|
||||
EXPECT_FALSE(f.IsMatrix());
|
||||
EXPECT_FALSE(f.IsPointer());
|
||||
EXPECT_FALSE(f.IsStruct());
|
||||
EXPECT_FALSE(f.IsU32());
|
||||
EXPECT_FALSE(f.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(F32TypeTest, TypeName) {
|
||||
F32Type f;
|
||||
EXPECT_EQ(f.type_name(), "__f32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
27
src/ast/type/i32_type.cc
Normal file
27
src/ast/type/i32_type.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
I32Type::I32Type() = default;
|
||||
|
||||
I32Type::~I32Type() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
46
src/ast/type/i32_type.h
Normal file
46
src/ast/type/i32_type.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_TYPE_I32_TYPE_H_
|
||||
#define SRC_AST_TYPE_I32_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A signed int 32 type.
|
||||
class I32Type : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
I32Type();
|
||||
/// Move constructor
|
||||
I32Type(I32Type&&) = default;
|
||||
~I32Type() override;
|
||||
|
||||
/// @returns true if the type is an i32 type
|
||||
bool IsI32() const override { return true; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override { return "__i32"; }
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_I32_TYPE_H_
|
||||
46
src/ast/type/i32_type_test.cc
Normal file
46
src/ast/type/i32_type_test.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using I32TypeTest = testing::Test;
|
||||
|
||||
TEST_F(I32TypeTest, Is) {
|
||||
I32Type i;
|
||||
EXPECT_FALSE(i.IsAlias());
|
||||
EXPECT_FALSE(i.IsArray());
|
||||
EXPECT_FALSE(i.IsBool());
|
||||
EXPECT_FALSE(i.IsF32());
|
||||
EXPECT_TRUE(i.IsI32());
|
||||
EXPECT_FALSE(i.IsMatrix());
|
||||
EXPECT_FALSE(i.IsPointer());
|
||||
EXPECT_FALSE(i.IsStruct());
|
||||
EXPECT_FALSE(i.IsU32());
|
||||
EXPECT_FALSE(i.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(I32TypeTest, TypeName) {
|
||||
I32Type i;
|
||||
EXPECT_EQ(i.type_name(), "__i32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
35
src/ast/type/matrix_type.cc
Normal file
35
src/ast/type/matrix_type.cc
Normal file
@@ -0,0 +1,35 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/matrix_type.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
MatrixType::MatrixType(Type* subtype, size_t rows, size_t columns)
|
||||
: subtype_(subtype), rows_(rows), columns_(columns) {
|
||||
assert(rows > 1);
|
||||
assert(rows < 5);
|
||||
assert(columns > 1);
|
||||
assert(columns < 5);
|
||||
}
|
||||
|
||||
MatrixType::~MatrixType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
64
src/ast/type/matrix_type.h
Normal file
64
src/ast/type/matrix_type.h
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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_TYPE_MATRIX_TYPE_H_
|
||||
#define SRC_AST_TYPE_MATRIX_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A matrix type
|
||||
class MatrixType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param subtype type matrix type
|
||||
/// @param rows the number of rows in the matrix
|
||||
/// @param columns the number of columns in the matrix
|
||||
MatrixType(Type* subtype, size_t rows, size_t columns);
|
||||
/// Move constructor
|
||||
MatrixType(MatrixType&&) = default;
|
||||
~MatrixType() override;
|
||||
|
||||
/// @returns true if the type is a matrix type
|
||||
bool IsMatrix() const override { return true; }
|
||||
|
||||
/// @returns the type of the matrix
|
||||
Type* type() const { return subtype_; }
|
||||
/// @returns the number of rows in the matrix
|
||||
size_t rows() const { return rows_; }
|
||||
/// @returns the number of columns in the matrix
|
||||
size_t columns() const { return columns_; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override {
|
||||
return "__mat_" + std::to_string(rows_) + "_" + std::to_string(columns_) +
|
||||
subtype_->type_name();
|
||||
}
|
||||
|
||||
private:
|
||||
Type* subtype_ = nullptr;
|
||||
size_t rows_ = 2;
|
||||
size_t columns_ = 2;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_MATRIX_TYPE_H_
|
||||
57
src/ast/type/matrix_type_test.cc
Normal file
57
src/ast/type/matrix_type_test.cc
Normal file
@@ -0,0 +1,57 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/matrix_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using MatrixTypeTest = testing::Test;
|
||||
|
||||
TEST_F(MatrixTypeTest, Creation) {
|
||||
I32Type i32;
|
||||
MatrixType m{&i32, 2, 4};
|
||||
EXPECT_EQ(m.type(), &i32);
|
||||
EXPECT_EQ(m.rows(), 2);
|
||||
EXPECT_EQ(m.columns(), 4);
|
||||
}
|
||||
|
||||
TEST_F(MatrixTypeTest, Is) {
|
||||
I32Type i32;
|
||||
MatrixType m{&i32, 2, 3};
|
||||
EXPECT_FALSE(m.IsAlias());
|
||||
EXPECT_FALSE(m.IsArray());
|
||||
EXPECT_FALSE(m.IsBool());
|
||||
EXPECT_FALSE(m.IsF32());
|
||||
EXPECT_FALSE(m.IsI32());
|
||||
EXPECT_TRUE(m.IsMatrix());
|
||||
EXPECT_FALSE(m.IsPointer());
|
||||
EXPECT_FALSE(m.IsStruct());
|
||||
EXPECT_FALSE(m.IsU32());
|
||||
EXPECT_FALSE(m.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(MatrixTypeTest, TypeName) {
|
||||
I32Type i32;
|
||||
MatrixType m{&i32, 2, 3};
|
||||
EXPECT_EQ(m.type_name(), "__mat_2_3__i32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
28
src/ast/type/pointer_type.cc
Normal file
28
src/ast/type/pointer_type.cc
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/pointer_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
PointerType::PointerType(Type* subtype, StorageClass storage_class)
|
||||
: subtype_(subtype), storage_class_(storage_class) {}
|
||||
|
||||
PointerType::~PointerType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
63
src/ast/type/pointer_type.h
Normal file
63
src/ast/type/pointer_type.h
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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_TYPE_POINTER_TYPE_H_
|
||||
#define SRC_AST_TYPE_POINTER_TYPE_H_
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/storage_class.h"
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A pointer type.
|
||||
class PointerType : public Type {
|
||||
public:
|
||||
/// Construtor
|
||||
/// @param subtype the pointee type
|
||||
/// @param storage_class the storage class of the pointer
|
||||
explicit PointerType(Type* subtype, StorageClass storage_class);
|
||||
/// Move constructor
|
||||
PointerType(PointerType&&) = default;
|
||||
~PointerType() override;
|
||||
|
||||
/// @returns true if the type is a pointer type
|
||||
bool IsPointer() const override { return true; }
|
||||
|
||||
/// @returns the pointee type
|
||||
Type* type() const { return subtype_; }
|
||||
/// @returns the storage class of the pointer
|
||||
StorageClass storage_class() const { return storage_class_; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override {
|
||||
std::ostringstream out;
|
||||
out << "__ptr_" << storage_class_ << subtype_->type_name();
|
||||
return out.str();
|
||||
}
|
||||
|
||||
private:
|
||||
Type* subtype_;
|
||||
StorageClass storage_class_;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_POINTER_TYPE_H_
|
||||
56
src/ast/type/pointer_type_test.cc
Normal file
56
src/ast/type/pointer_type_test.cc
Normal file
@@ -0,0 +1,56 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/pointer_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using PointerTypeTest = testing::Test;
|
||||
|
||||
TEST_F(PointerTypeTest, Creation) {
|
||||
I32Type i32;
|
||||
PointerType p{&i32, StorageClass::kStorageBuffer};
|
||||
EXPECT_EQ(p.type(), &i32);
|
||||
EXPECT_EQ(p.storage_class(), StorageClass::kStorageBuffer);
|
||||
}
|
||||
|
||||
TEST_F(PointerTypeTest, Is) {
|
||||
I32Type i32;
|
||||
PointerType p{&i32, StorageClass::kFunction};
|
||||
EXPECT_FALSE(p.IsAlias());
|
||||
EXPECT_FALSE(p.IsArray());
|
||||
EXPECT_FALSE(p.IsBool());
|
||||
EXPECT_FALSE(p.IsF32());
|
||||
EXPECT_FALSE(p.IsI32());
|
||||
EXPECT_FALSE(p.IsMatrix());
|
||||
EXPECT_TRUE(p.IsPointer());
|
||||
EXPECT_FALSE(p.IsStruct());
|
||||
EXPECT_FALSE(p.IsU32());
|
||||
EXPECT_FALSE(p.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(PointerTypeTest, TypeName) {
|
||||
I32Type i32;
|
||||
PointerType p{&i32, StorageClass::kWorkgroup};
|
||||
EXPECT_EQ(p.type_name(), "__ptr_workgroup__i32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
30
src/ast/type/struct_type.cc
Normal file
30
src/ast/type/struct_type.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/struct_type.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
StructType::StructType(std::unique_ptr<Struct> impl)
|
||||
: struct_(std::move(impl)) {}
|
||||
|
||||
StructType::~StructType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
62
src/ast/type/struct_type.h
Normal file
62
src/ast/type/struct_type.h
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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_TYPE_STRUCT_TYPE_H_
|
||||
#define SRC_AST_TYPE_STRUCT_TYPE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/struct.h"
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A structure type
|
||||
class StructType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param impl the struct data
|
||||
explicit StructType(std::unique_ptr<Struct> impl);
|
||||
/// Move constructor
|
||||
StructType(StructType&&) = default;
|
||||
~StructType() override;
|
||||
|
||||
/// Sets the name of the struct
|
||||
/// @param name the name to set
|
||||
void set_name(const std::string& name) { name_ = name; }
|
||||
/// @returns the struct name
|
||||
const std::string& name() const { return name_; }
|
||||
|
||||
/// @returns true if the type is a struct type
|
||||
bool IsStruct() const override { return true; }
|
||||
|
||||
/// @returns the struct name
|
||||
Struct* impl() const { return struct_.get(); }
|
||||
|
||||
/// @returns the name for th type
|
||||
std::string type_name() const override { return "__struct_" + name_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::unique_ptr<Struct> struct_;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_STRUCT_TYPE_H_
|
||||
59
src/ast/type/struct_type_test.cc
Normal file
59
src/ast/type/struct_type_test.cc
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/struct_type.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using StructTypeTest = testing::Test;
|
||||
|
||||
TEST_F(StructTypeTest, Creation) {
|
||||
auto impl = std::make_unique<Struct>();
|
||||
auto ptr = impl.get();
|
||||
StructType s{std::move(impl)};
|
||||
EXPECT_EQ(s.impl(), ptr);
|
||||
}
|
||||
|
||||
TEST_F(StructTypeTest, Is) {
|
||||
auto impl = std::make_unique<Struct>();
|
||||
StructType s{std::move(impl)};
|
||||
EXPECT_FALSE(s.IsAlias());
|
||||
EXPECT_FALSE(s.IsArray());
|
||||
EXPECT_FALSE(s.IsBool());
|
||||
EXPECT_FALSE(s.IsF32());
|
||||
EXPECT_FALSE(s.IsI32());
|
||||
EXPECT_FALSE(s.IsMatrix());
|
||||
EXPECT_FALSE(s.IsPointer());
|
||||
EXPECT_TRUE(s.IsStruct());
|
||||
EXPECT_FALSE(s.IsU32());
|
||||
EXPECT_FALSE(s.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(StructTypeTest, TypeName) {
|
||||
auto impl = std::make_unique<Struct>();
|
||||
StructType s{std::move(impl)};
|
||||
s.set_name("my_struct");
|
||||
EXPECT_EQ(s.type_name(), "__struct_my_struct");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
96
src/ast/type/type.cc
Normal file
96
src/ast/type/type.cc
Normal file
@@ -0,0 +1,96 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "src/ast/type/alias_type.h"
|
||||
#include "src/ast/type/array_type.h"
|
||||
#include "src/ast/type/bool_type.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/matrix_type.h"
|
||||
#include "src/ast/type/pointer_type.h"
|
||||
#include "src/ast/type/struct_type.h"
|
||||
#include "src/ast/type/u32_type.h"
|
||||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/type/void_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
Type::Type() = default;
|
||||
|
||||
Type::~Type() = default;
|
||||
|
||||
AliasType* Type::AsAlias() {
|
||||
assert(IsAlias());
|
||||
return static_cast<AliasType*>(this);
|
||||
}
|
||||
|
||||
ArrayType* Type::AsArray() {
|
||||
assert(IsArray());
|
||||
return static_cast<ArrayType*>(this);
|
||||
}
|
||||
|
||||
BoolType* Type::AsBool() {
|
||||
assert(IsBool());
|
||||
return static_cast<BoolType*>(this);
|
||||
}
|
||||
|
||||
F32Type* Type::AsF32() {
|
||||
assert(IsF32());
|
||||
return static_cast<F32Type*>(this);
|
||||
}
|
||||
|
||||
I32Type* Type::AsI32() {
|
||||
assert(IsI32());
|
||||
return static_cast<I32Type*>(this);
|
||||
}
|
||||
|
||||
MatrixType* Type::AsMatrix() {
|
||||
assert(IsMatrix());
|
||||
return static_cast<MatrixType*>(this);
|
||||
}
|
||||
|
||||
PointerType* Type::AsPointer() {
|
||||
assert(IsPointer());
|
||||
return static_cast<PointerType*>(this);
|
||||
}
|
||||
|
||||
StructType* Type::AsStruct() {
|
||||
assert(IsStruct());
|
||||
return static_cast<StructType*>(this);
|
||||
}
|
||||
|
||||
U32Type* Type::AsU32() {
|
||||
assert(IsU32());
|
||||
return static_cast<U32Type*>(this);
|
||||
}
|
||||
|
||||
VectorType* Type::AsVector() {
|
||||
assert(IsVector());
|
||||
return static_cast<VectorType*>(this);
|
||||
}
|
||||
|
||||
VoidType* Type::AsVoid() {
|
||||
assert(IsVoid());
|
||||
return static_cast<VoidType*>(this);
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
100
src/ast/type/type.h
Normal file
100
src/ast/type/type.h
Normal file
@@ -0,0 +1,100 @@
|
||||
// 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_TYPE_TYPE_H_
|
||||
#define SRC_AST_TYPE_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
class AliasType;
|
||||
class ArrayType;
|
||||
class BoolType;
|
||||
class F32Type;
|
||||
class I32Type;
|
||||
class MatrixType;
|
||||
class PointerType;
|
||||
class StructType;
|
||||
class U32Type;
|
||||
class VectorType;
|
||||
class VoidType;
|
||||
|
||||
/// Base class for a type in the system
|
||||
class Type {
|
||||
public:
|
||||
/// Move constructor
|
||||
Type(Type&&) = default;
|
||||
virtual ~Type();
|
||||
|
||||
/// @returns true if the type is an alias type
|
||||
virtual bool IsAlias() const { return false; }
|
||||
/// @returns true if the type is an array type
|
||||
virtual bool IsArray() const { return false; }
|
||||
/// @returns true if the type is a bool type
|
||||
virtual bool IsBool() const { return false; }
|
||||
/// @returns true if the type is an f32 type
|
||||
virtual bool IsF32() const { return false; }
|
||||
/// @returns true if the type is an i32 type
|
||||
virtual bool IsI32() const { return false; }
|
||||
/// @returns true if the type is a matrix type
|
||||
virtual bool IsMatrix() const { return false; }
|
||||
/// @returns true if the type is a ptr type
|
||||
virtual bool IsPointer() const { return false; }
|
||||
/// @returns true if the type is a struct type
|
||||
virtual bool IsStruct() const { return false; }
|
||||
/// @returns true if the type is a u32 type
|
||||
virtual bool IsU32() const { return false; }
|
||||
/// @returns true if the type is a vec type
|
||||
virtual bool IsVector() const { return false; }
|
||||
/// @returns true if the type is a void type
|
||||
virtual bool IsVoid() const { return false; }
|
||||
|
||||
/// @returns the name for this type
|
||||
virtual std::string type_name() const = 0;
|
||||
|
||||
/// @returns the type as an alias type
|
||||
AliasType* AsAlias();
|
||||
/// @returns the type as an array type
|
||||
ArrayType* AsArray();
|
||||
/// @returns the type as a bool type
|
||||
BoolType* AsBool();
|
||||
/// @returns the type as a f32 type
|
||||
F32Type* AsF32();
|
||||
/// @returns the type as an i32 type
|
||||
I32Type* AsI32();
|
||||
/// @returns the type as a matrix type
|
||||
MatrixType* AsMatrix();
|
||||
/// @returns the type as a pointer type
|
||||
PointerType* AsPointer();
|
||||
/// @returns the type as a struct type
|
||||
StructType* AsStruct();
|
||||
/// @returns the type as a u32 type
|
||||
U32Type* AsU32();
|
||||
/// @returns the type as a vector type
|
||||
VectorType* AsVector();
|
||||
/// @returns the type as a void type
|
||||
VoidType* AsVoid();
|
||||
|
||||
protected:
|
||||
Type();
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_TYPE_H_
|
||||
27
src/ast/type/u32_type.cc
Normal file
27
src/ast/type/u32_type.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/u32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
U32Type::U32Type() = default;
|
||||
|
||||
U32Type::~U32Type() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
46
src/ast/type/u32_type.h
Normal file
46
src/ast/type/u32_type.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_TYPE_U32_TYPE_H_
|
||||
#define SRC_AST_TYPE_U32_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A unsigned int 32 type.
|
||||
class U32Type : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
U32Type();
|
||||
/// Move constructor
|
||||
U32Type(U32Type&&) = default;
|
||||
~U32Type() override;
|
||||
|
||||
/// @returns true if the type is a u32 type
|
||||
bool IsU32() const override { return true; }
|
||||
|
||||
/// @returns the name for th type
|
||||
std::string type_name() const override { return "__u32"; }
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_U32_TYPE_H_
|
||||
46
src/ast/type/u32_type_test.cc
Normal file
46
src/ast/type/u32_type_test.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/u32_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using U32TypeTest = testing::Test;
|
||||
|
||||
TEST_F(U32TypeTest, Is) {
|
||||
U32Type u;
|
||||
EXPECT_FALSE(u.IsAlias());
|
||||
EXPECT_FALSE(u.IsArray());
|
||||
EXPECT_FALSE(u.IsBool());
|
||||
EXPECT_FALSE(u.IsF32());
|
||||
EXPECT_FALSE(u.IsI32());
|
||||
EXPECT_FALSE(u.IsMatrix());
|
||||
EXPECT_FALSE(u.IsPointer());
|
||||
EXPECT_FALSE(u.IsStruct());
|
||||
EXPECT_TRUE(u.IsU32());
|
||||
EXPECT_FALSE(u.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(U32TypeTest, TypeName) {
|
||||
U32Type u;
|
||||
EXPECT_EQ(u.type_name(), "__u32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
33
src/ast/type/vector_type.cc
Normal file
33
src/ast/type/vector_type.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/vector_type.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
VectorType::VectorType(Type* subtype, size_t size)
|
||||
: subtype_(subtype), size_(size) {
|
||||
assert(size_ > 1);
|
||||
assert(size_ < 5);
|
||||
}
|
||||
|
||||
VectorType::~VectorType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
59
src/ast/type/vector_type.h
Normal file
59
src/ast/type/vector_type.h
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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_TYPE_VECTOR_TYPE_H_
|
||||
#define SRC_AST_TYPE_VECTOR_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A vector type.
|
||||
class VectorType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param subtype the vector element type
|
||||
/// @param size the number of elements in the vector
|
||||
VectorType(Type* subtype, size_t size);
|
||||
/// Move constructor
|
||||
VectorType(VectorType&&) = default;
|
||||
~VectorType() override;
|
||||
|
||||
/// @returns true if the type is a vector type
|
||||
bool IsVector() const override { return true; }
|
||||
|
||||
/// @returns the type of the vector elements
|
||||
Type* type() const { return subtype_; }
|
||||
/// @returns the size of the vector
|
||||
size_t size() const { return size_; }
|
||||
|
||||
/// @returns the name for th type
|
||||
std::string type_name() const override {
|
||||
return "__vec_" + std::to_string(size_) + subtype_->type_name();
|
||||
}
|
||||
|
||||
private:
|
||||
Type* subtype_ = nullptr;
|
||||
size_t size_ = 2;
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_VECTOR_TYPE_H_
|
||||
56
src/ast/type/vector_type_test.cc
Normal file
56
src/ast/type/vector_type_test.cc
Normal file
@@ -0,0 +1,56 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/vector_type.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
using VectorTypeTest = testing::Test;
|
||||
|
||||
TEST_F(VectorTypeTest, Creation) {
|
||||
I32Type i32;
|
||||
VectorType v{&i32, 2};
|
||||
EXPECT_EQ(v.type(), &i32);
|
||||
EXPECT_EQ(v.size(), 2);
|
||||
}
|
||||
|
||||
TEST_F(VectorTypeTest, Is) {
|
||||
I32Type i32;
|
||||
VectorType v{&i32, 4};
|
||||
EXPECT_FALSE(v.IsAlias());
|
||||
EXPECT_FALSE(v.IsArray());
|
||||
EXPECT_FALSE(v.IsBool());
|
||||
EXPECT_FALSE(v.IsF32());
|
||||
EXPECT_FALSE(v.IsI32());
|
||||
EXPECT_FALSE(v.IsMatrix());
|
||||
EXPECT_FALSE(v.IsPointer());
|
||||
EXPECT_FALSE(v.IsStruct());
|
||||
EXPECT_FALSE(v.IsU32());
|
||||
EXPECT_TRUE(v.IsVector());
|
||||
}
|
||||
|
||||
TEST_F(VectorTypeTest, TypeName) {
|
||||
I32Type i32;
|
||||
VectorType v{&i32, 3};
|
||||
EXPECT_EQ(v.type_name(), "__vec_3__i32");
|
||||
}
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
27
src/ast/type/void_type.cc
Normal file
27
src/ast/type/void_type.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#include "src/ast/type/void_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
VoidType::VoidType() = default;
|
||||
|
||||
VoidType::~VoidType() = default;
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
46
src/ast/type/void_type.h
Normal file
46
src/ast/type/void_type.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_TYPE_VOID_TYPE_H_
|
||||
#define SRC_AST_TYPE_VOID_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
namespace type {
|
||||
|
||||
/// A void type
|
||||
class VoidType : public Type {
|
||||
public:
|
||||
/// Constructor
|
||||
VoidType();
|
||||
/// Move constructor
|
||||
VoidType(VoidType&&) = default;
|
||||
~VoidType() override;
|
||||
|
||||
/// @returns true if the type is a void type
|
||||
bool IsVoid() const override { return true; }
|
||||
|
||||
/// @returns the name for this type
|
||||
std::string type_name() const override { return "__void"; }
|
||||
};
|
||||
|
||||
} // namespace type
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_TYPE_VOID_TYPE_H_
|
||||
Reference in New Issue
Block a user