1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "enum_def.h"
18
19 #include <iostream>
20 #include <map>
21
22 #include "fields/enum_field.h"
23 #include "util.h"
24
EnumDef(std::string name,int size)25 EnumDef::EnumDef(std::string name, int size) : TypeDef(name, size) {}
26
AddEntry(std::string name,uint32_t value)27 void EnumDef::AddEntry(std::string name, uint32_t value) {
28 if (!util::IsEnumCase(name)) {
29 ERROR() << __func__ << ": Enum " << name << "(" << value << ") should be all uppercase with underscores";
30 }
31 if (value > util::GetMaxValueForBits(size_)) {
32 ERROR() << __func__ << ": Value of " << name << "(" << value << ") is greater than the max possible value for enum "
33 << name_ << "(" << util::GetMaxValueForBits(size_) << ")\n";
34 }
35
36 constants_.insert(std::pair(value, name));
37 entries_.insert(name);
38 }
39
GetNewField(const std::string & name,ParseLocation loc) const40 PacketField* EnumDef::GetNewField(const std::string& name, ParseLocation loc) const {
41 return new EnumField(name, *this, "What is this for", loc);
42 }
43
HasEntry(std::string name) const44 bool EnumDef::HasEntry(std::string name) const {
45 return entries_.count(name) != 0;
46 }
47
GetDefinitionType() const48 TypeDef::Type EnumDef::GetDefinitionType() const {
49 return TypeDef::Type::ENUM;
50 }
51