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,uint64_t value)27 void EnumDef::AddEntry(std::string name, uint64_t value) {
28   if (!util::IsEnumCase(name)) {
29     ERROR() << __func__ << ": Enum " << name << "(" << value
30             << ") should be all uppercase with underscores";
31   }
32   if (value > util::GetMaxValueForBits(size_)) {
33     ERROR() << __func__ << ": Value of " << name << "(" << value
34             << ") is greater than the max possible value for enum " << name_ << "("
35             << util::GetMaxValueForBits(size_) << ")\n";
36   }
37 
38   constants_.insert(std::pair(value, name));
39   entries_.insert(name);
40 }
41 
GetNewField(const std::string & name,ParseLocation loc) const42 PacketField* EnumDef::GetNewField(const std::string& name, ParseLocation loc) const {
43   return new EnumField(name, *this, "What is this for", loc);
44 }
45 
HasEntry(std::string name) const46 bool EnumDef::HasEntry(std::string name) const { return entries_.count(name) != 0; }
47 
GetDefinitionType() const48 TypeDef::Type EnumDef::GetDefinitionType() const { return TypeDef::Type::ENUM; }
49