• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/type.h"
16 
17 #include <cassert>
18 #include <memory>
19 
20 #include "src/make_unique.h"
21 
22 namespace amber {
23 namespace type {
24 
25 Type::Type() = default;
26 
27 Type::~Type() = default;
28 
AsList()29 List* Type::AsList() {
30   return static_cast<List*>(this);
31 }
32 
AsNumber()33 Number* Type::AsNumber() {
34   return static_cast<Number*>(this);
35 }
36 
AsStruct()37 Struct* Type::AsStruct() {
38   return static_cast<Struct*>(this);
39 }
40 
AsList() const41 const List* Type::AsList() const {
42   return static_cast<const List*>(this);
43 }
44 
AsNumber() const45 const Number* Type::AsNumber() const {
46   return static_cast<const Number*>(this);
47 }
48 
AsStruct() const49 const Struct* Type::AsStruct() const {
50   return static_cast<const Struct*>(this);
51 }
52 
53 // static
Int(uint32_t bits)54 std::unique_ptr<Number> Number::Int(uint32_t bits) {
55   return MakeUnique<Number>(FormatMode::kSInt, bits);
56 }
57 
58 // static
Uint(uint32_t bits)59 std::unique_ptr<Number> Number::Uint(uint32_t bits) {
60   return MakeUnique<Number>(FormatMode::kUInt, bits);
61 }
62 
63 // static
Float(uint32_t bits)64 std::unique_ptr<Number> Number::Float(uint32_t bits) {
65   return MakeUnique<Number>(FormatMode::kSFloat, bits);
66 }
67 
Number(FormatMode format_mode)68 Number::Number(FormatMode format_mode) : format_mode_(format_mode) {}
69 
Number(FormatMode format_mode,uint32_t bits)70 Number::Number(FormatMode format_mode, uint32_t bits)
71     : format_mode_(format_mode), bits_(bits) {}
72 
73 Number::~Number() = default;
74 
75 List::List() = default;
76 
77 List::~List() = default;
78 
SizeInBytes() const79 uint32_t List::SizeInBytes() const {
80   if (pack_size_in_bits_ > 0)
81     return pack_size_in_bits_;
82 
83   uint32_t size = 0;
84   for (const auto& member : members_)
85     size += member.SizeInBytes();
86 
87   return size;
88 }
89 
90 Struct::Struct() = default;
91 
92 Struct::~Struct() = default;
93 
94 // Struct side is dependent on the layout we're currently in ....
SizeInBytes() const95 uint32_t Struct::SizeInBytes() const {
96   assert(false && "Not reached");
97   return 0;
98 }
99 
100 }  // namespace type
101 }  // namespace amber
102