xref: /netbsd-src/external/bsd/elftosb/dist/common/EvalContext.h (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
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 #if !defined(_EvalContext_h_)
8 #define _EvalContext_h_
9 
10 #include <map>
11 #include <string>
12 #include "Value.h"
13 #include "int_size.h"
14 #include "SourceFile.h"
15 
16 namespace elftosb
17 {
18 
19 /*!
20  * \brief Context for evaluating AST tree and expressions.
21  *
22  * Keeps a map of variable names to integer values. Each integer value has a
23  * size attribute in addition to the actual value. Variables can be locked, which
24  * simply means that they cannot be assigned a new value.
25  *
26  * \todo Switch to using Value instances to keep track of variable values. This
27  *		will enable variables to have string values, for one.
28  */
29 class EvalContext
30 {
31 public:
32 	/*!
33 	 * \brief Abstract interface for a manager of source files.
34 	 */
35 	class SourceFileManager
36 	{
37 	public:
38 		//! \brief Returns true if a source file with the name \a name exists.
39 		virtual bool hasSourceFile(const std::string & name)=0;
40 
41 		//! \brief Gets the requested source file.
42 		virtual SourceFile * getSourceFile(const std::string & name)=0;
43 
44 		//! \brief Returns the default source file, or NULL if none is set.
45 		virtual SourceFile * getDefaultSourceFile()=0;
46 	};
47 
48 public:
49 	//! \brief Constructor.
50 	EvalContext();
51 
52 	//! \brief Destructor.
53 	virtual ~EvalContext();
54 
55 	//! \name Source file manager
56 	//@{
57 	//! \brief
setSourceFileManager(SourceFileManager * manager)58 	void setSourceFileManager(SourceFileManager * manager) { m_sourcesManager = manager; }
59 
60 	//! \brief
getSourceFileManager()61 	SourceFileManager * getSourceFileManager() { return m_sourcesManager; }
62 	//@}
63 
64 	//! \name Variables
65 	//@{
66 	bool isVariableDefined(const std::string & name);
67 	uint32_t getVariableValue(const std::string & name);
68 	int_size_t getVariableSize(const std::string & name);
69 	void setVariable(const std::string & name, uint32_t value, int_size_t size=kWordSize);
70 	//@}
71 
72 	//! \name Locks
73 	//@{
74 	bool isVariableLocked(const std::string & name);
75 	void lockVariable(const std::string & name);
76 	void unlockVariable(const std::string & name);
77 	//@}
78 
79 	void dump();
80 
81 protected:
82 	//! Information about a variable's value.
83 	struct variable_info_t
84 	{
85 		uint32_t m_value;	//!< Variable value.
86 		int_size_t m_size;	//!< Number of bytes
87 		bool m_isLocked;	//!< Can this variable's value be changed?
88 	};
89 
90 	//! Type to maps between the variable name and its info.
91 	typedef std::map<std::string, variable_info_t> variable_map_t;
92 
93 	SourceFileManager * m_sourcesManager; //!< Interface to source file manager.
94 	variable_map_t m_variables;	//!< Map of variables to their final values.
95 };
96 
97 }; // namespace elftosb
98 
99 #endif // _EvalContext_h_
100