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 "fields/enum_field.h"
18 
19 #include "util.h"
20 
21 const std::string EnumField::kFieldType = "EnumField";
22 
EnumField(std::string name,EnumDef enum_def,std::string value,ParseLocation loc)23 EnumField::EnumField(std::string name, EnumDef enum_def, std::string value, ParseLocation loc)
24     : ScalarField(name, enum_def.size_, loc), enum_def_(enum_def), value_(value) {}
25 
GetFieldType() const26 const std::string& EnumField::GetFieldType() const {
27   return EnumField::kFieldType;
28 }
29 
GetEnumDef()30 EnumDef EnumField::GetEnumDef() {
31   return enum_def_;
32 }
33 
GetDataType() const34 std::string EnumField::GetDataType() const {
35   return enum_def_.name_;
36 }
37 
HasParameterValidator() const38 bool EnumField::HasParameterValidator() const {
39   return false;
40 }
41 
GenParameterValidator(std::ostream &) const42 void EnumField::GenParameterValidator(std::ostream&) const {
43   // Validated at compile time.
44 }
45 
GenInserter(std::ostream & s) const46 void EnumField::GenInserter(std::ostream& s) const {
47   s << "insert(static_cast<" << util::GetTypeForSize(GetSize().bits()) << ">(";
48   s << GetName() << "_), i, " << GetSize().bits() << ");";
49 }
50 
GenValidator(std::ostream &) const51 void EnumField::GenValidator(std::ostream&) const {
52   // Do nothing
53 }
54 
GenStringRepresentation(std::ostream & s,std::string accessor) const55 void EnumField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
56   s << GetDataType() << "Text(" << accessor << ")";
57 }
58