1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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 #pragma once 18 19 #include <memory> 20 21 #include <thrift/compiler/ast/t_const_value.h> 22 #include <thrift/compiler/ast/t_named.h> 23 #include <thrift/compiler/ast/t_type.h> 24 25 namespace apache { 26 namespace thrift { 27 namespace compiler { 28 29 class t_program; 30 31 /** 32 * class t_const 33 * 34 * A const is a constant value defined across languages that has a type and 35 * a value. The trick here is that the declared type might not match the type 36 * of the value object, since that is not determined until after parsing the 37 * whole thing out. 38 * 39 */ 40 class t_const final : public t_named { 41 public: 42 /** 43 * Constructor for t_const 44 * 45 * @param program - An entire thrift program 46 * @param type - A thrift type 47 * @param name - The name of the constant variable 48 * @param value - The constant value 49 */ t_const(t_program * program,t_type_ref type,std::string name,std::unique_ptr<t_const_value> value)50 t_const( 51 t_program* program, 52 t_type_ref type, 53 std::string name, 54 std::unique_ptr<t_const_value> value) 55 : t_named(std::move(name)), 56 program_(program), 57 type_(std::move(type)), 58 value_(std::move(value)) { 59 // value->get_owner() is set when rhs is referencing another constant. 60 if (value_ && value_->get_owner() == nullptr) { 61 value_->set_owner(this); 62 } 63 } 64 program()65 const t_program* program() const { return program_; } type()66 const t_type_ref& type() const { return type_; } value()67 const t_const_value* value() const { return value_.get(); } 68 69 private: 70 t_program* const program_; 71 t_type_ref type_; 72 73 std::unique_ptr<t_const_value> value_; 74 75 // TODO(afuller): Delete everything below here. It is only provided for 76 // backwards compatibility. 77 public: t_const(t_program * program,const t_type * type,std::string name,std::unique_ptr<t_const_value> value)78 t_const( 79 t_program* program, 80 const t_type* type, 81 std::string name, 82 std::unique_ptr<t_const_value> value) 83 : t_const( 84 program, 85 t_type_ref::from_req_ptr(type), 86 std::move(name), 87 std::move(value)) {} 88 get_program()89 t_program* get_program() const { return program_; } get_type()90 const t_type* get_type() const { return type_.get_type(); } get_value()91 t_const_value* get_value() const { return value_.get(); } 92 }; 93 94 } // namespace compiler 95 } // namespace thrift 96 } // namespace apache 97