@@ -23,22 +23,19 @@ T ReadSymbolFromTarget(SBTarget& target, SBAddress& start, const char* name,
2323 return res;
2424}
2525
26- std::pair<int64_t , bool > Constants::LookupConstant (SBTarget target,
27- const char * name,
28- int64_t def) {
29- int64_t res = 0 ;
30- res = def;
26+ Constant<int64_t > Constants::LookupConstant (SBTarget target, const char * name) {
27+ int64_t res;
3128
3229 SBSymbolContextList context_list = target.FindSymbols (name);
3330
3431 if (!context_list.IsValid () || context_list.GetSize () == 0 ) {
35- return {res, false } ;
32+ return Constant< int64_t >() ;
3633 }
3734
3835 SBSymbolContext context = context_list.GetContextAtIndex (0 );
3936 SBSymbol symbol = context.GetSymbol ();
4037 if (!symbol.IsValid ()) {
41- return {res, false } ;
38+ return Constant< int64_t >() ;
4239 }
4340
4441 SBAddress start = symbol.GetStartAddress ();
@@ -59,10 +56,12 @@ std::pair<int64_t, bool> Constants::LookupConstant(SBTarget target,
5956 int8_t tmp = ReadSymbolFromTarget<int8_t >(target, start, name, sberr);
6057 res = static_cast <int64_t >(tmp);
6158 } else {
62- return {res, false } ;
59+ return Constant< int64_t >() ;
6360 }
6461
65- return {res, !sberr.Fail ()};
62+ if (sberr.Fail ()) return Constant<int64_t >();
63+
64+ return Constant<int64_t >(res, name);
6665}
6766
6867void Constants::Assign (SBTarget target) {
@@ -72,46 +71,49 @@ void Constants::Assign(SBTarget target) {
7271
7372
7473int64_t Constants::LoadRawConstant (const char * name, int64_t def) {
75- auto v = Constants::LookupConstant (target_, name, def );
76- if (!v. second ) {
74+ auto constant = Constants::LookupConstant (target_, name);
75+ if (!constant. Check () ) {
7776 PRINT_DEBUG (" Failed to load raw constant %s, default to %" PRId64, name,
7877 def);
78+ return def;
7979 }
8080
81- return v. first ;
81+ return *constant ;
8282}
8383
8484int64_t Constants::LoadConstant (const char * name, int64_t def) {
85- auto v = Constants::LookupConstant (target_,
86- (constant_prefix () + name).c_str (), def);
87- if (!v.second ) {
88- PRINT_DEBUG (" Failed to load constant %s, default to %" PRId64, name, def);
85+ auto constant =
86+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
87+ if (!constant.Check ()) {
88+ PRINT_DEBUG (" Failed to load constant %s, default to %" PRId64, name);
89+ return def;
8990 }
9091
91- return v. first ;
92+ return *constant ;
9293}
9394
9495int64_t Constants::LoadConstant (const char * name, const char * fallback,
9596 int64_t def) {
96- auto v = Constants::LookupConstant (target_,
97- (constant_prefix () + name).c_str (), def );
98- if (!v. second )
99- v = Constants::LookupConstant (target_,
100- (constant_prefix () + fallback).c_str (), def );
101- if (!v. second ) {
97+ auto constant =
98+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
99+ if (!constant. Check () )
100+ constant = Constants::LookupConstant (
101+ target_, (constant_prefix () + fallback).c_str ());
102+ if (!constant. Check () ) {
102103 PRINT_DEBUG (" Failed to load constant %s, fallback %s, default to %" PRId64,
103104 name, fallback, def);
105+ return def;
104106 }
105107
106- return v. first ;
108+ return *constant ;
107109}
108110
109111Constant<int64_t > Constants::LoadConstant (
110112 std::initializer_list<const char *> names) {
111113 for (std::string name : names) {
112- auto v = Constants::LookupConstant (target_,
113- (constant_prefix () + name).c_str (), - 1 );
114- if (v. second ) return Constant< int64_t >(v. first , name) ;
114+ auto constant =
115+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
116+ if (constant. Check ()) return constant ;
115117 }
116118
117119 if (Error::IsDebugMode ()) {
@@ -128,9 +130,9 @@ Constant<int64_t> Constants::LoadConstant(
128130Constant<int64_t > Constants::LoadOptionalConstant (
129131 std::initializer_list<const char *> names, int def) {
130132 for (std::string name : names) {
131- auto v = Constants::LookupConstant (target_,
132- (constant_prefix () + name).c_str (), - 1 );
133- if (v. second ) return Constant< int64_t >(v. first , name) ;
133+ auto constant =
134+ Constants::LookupConstant (target_, (constant_prefix () + name).c_str ());
135+ if (constant. Check ()) return constant ;
134136 }
135137
136138 return Constant<int64_t >(def);
0 commit comments