1/*
2 * File:	EvalContext.h
3 *
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
6 */
7
8#include "EvalContext.h"
9#include <stdexcept>
10#include "format_string.h"
11
12using namespace elftosb;
13
14EvalContext::EvalContext()
15:	m_sourcesManager(0)
16{
17}
18
19EvalContext::~EvalContext()
20{
21}
22
23bool EvalContext::isVariableDefined(const std::string & name)
24{
25	variable_map_t::const_iterator it = m_variables.find(name);
26	return it != m_variables.end();
27}
28
29uint32_t EvalContext::getVariableValue(const std::string & name)
30{
31	variable_map_t::const_iterator it = m_variables.find(name);
32	if (it == m_variables.end())
33	{
34		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
35	}
36
37	return it->second.m_value;
38}
39
40int_size_t EvalContext::getVariableSize(const std::string & name)
41{
42	variable_map_t::const_iterator it = m_variables.find(name);
43	if (it == m_variables.end())
44	{
45		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
46	}
47
48	return it->second.m_size;
49}
50
51void EvalContext::setVariable(const std::string & name, uint32_t value, int_size_t size)
52{
53	// check if var is locked
54	variable_map_t::const_iterator it = m_variables.find(name);
55	if (it != m_variables.end() && it->second.m_isLocked)
56	{
57		return;
58	}
59
60	// set var info
61	variable_info_t info;
62	info.m_value = value;
63	info.m_size = size;
64	info.m_isLocked = false;
65	m_variables[name] = info;
66}
67
68bool EvalContext::isVariableLocked(const std::string & name)
69{
70	variable_map_t::const_iterator it = m_variables.find(name);
71	if (it == m_variables.end())
72	{
73		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
74	}
75
76	return it->second.m_isLocked;
77}
78
79void EvalContext::lockVariable(const std::string & name)
80{
81	variable_map_t::iterator it = m_variables.find(name);
82	if (it == m_variables.end())
83	{
84		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
85	}
86
87	it->second.m_isLocked = true;
88}
89
90void EvalContext::unlockVariable(const std::string & name)
91{
92	variable_map_t::iterator it = m_variables.find(name);
93	if (it == m_variables.end())
94	{
95		throw std::runtime_error("undefined variable");
96	}
97
98	it->second.m_isLocked = false;
99}
100
101void EvalContext::dump()
102{
103	variable_map_t::iterator it = m_variables.begin();
104	for (; it != m_variables.end(); ++it)
105	{
106		variable_info_t & info = it->second;
107		const char * lockedString = info.m_isLocked ? "locked" : "unlocked";
108		printf("%s = %u:%d (%s)\n", it->first.c_str(), info.m_value, (int)info.m_size, lockedString);
109	}
110}
111
112