1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef ATTRIBUTE_VALUE_H
6#define ATTRIBUTE_VALUE_H
7
8#include "AttributeClasses.h"
9#include "Types.h"
10
11
12class DebugInfoEntry;
13
14
15struct AttributeValue {
16	union {
17		target_addr_t		address;
18		struct {
19			const void*		data;
20			off_t			length;
21		}					block;
22		uint64				constant;
23		bool				flag;
24		off_t				pointer;
25		DebugInfoEntry*		reference;
26		const char*			string;
27	};
28
29	uint16				attributeForm;
30	uint8				attributeClass;
31	bool				isSigned;
32
33	AttributeValue()
34		:
35		attributeClass(ATTRIBUTE_CLASS_UNKNOWN)
36	{
37	}
38
39	~AttributeValue()
40	{
41		Unset();
42	}
43
44	void SetToAddress(target_addr_t address)
45	{
46		Unset();
47		attributeClass = ATTRIBUTE_CLASS_ADDRESS;
48		this->address = address;
49	}
50
51	void SetToBlock(const void* data, off_t length)
52	{
53		Unset();
54		attributeClass = ATTRIBUTE_CLASS_BLOCK;
55		block.data = data;
56		block.length = length;
57	}
58
59	void SetToConstant(uint64 value, bool isSigned)
60	{
61		Unset();
62		attributeClass = ATTRIBUTE_CLASS_CONSTANT;
63		this->constant = value;
64		this->isSigned = isSigned;
65	}
66
67	void SetToFlag(bool value)
68	{
69		Unset();
70		attributeClass = ATTRIBUTE_CLASS_FLAG;
71		this->flag = value;
72	}
73
74	void SetToLinePointer(off_t value)
75	{
76		Unset();
77		attributeClass = ATTRIBUTE_CLASS_LINEPTR;
78		this->pointer = value;
79	}
80
81	void SetToLocationListPointer(off_t value)
82	{
83		Unset();
84		attributeClass = ATTRIBUTE_CLASS_LOCLISTPTR;
85		this->pointer = value;
86	}
87
88	void SetToMacroPointer(off_t value)
89	{
90		Unset();
91		attributeClass = ATTRIBUTE_CLASS_MACPTR;
92		this->pointer = value;
93	}
94
95	void SetToRangeListPointer(off_t value)
96	{
97		Unset();
98		attributeClass = ATTRIBUTE_CLASS_RANGELISTPTR;
99		this->pointer = value;
100	}
101
102	void SetToReference(DebugInfoEntry* entry)
103	{
104		Unset();
105		attributeClass = ATTRIBUTE_CLASS_REFERENCE;
106		this->reference = entry;
107	}
108
109	void SetToString(const char* string)
110	{
111		Unset();
112		attributeClass = ATTRIBUTE_CLASS_STRING;
113		this->string = string;
114	}
115
116	void Unset()
117	{
118		attributeClass = ATTRIBUTE_CLASS_UNKNOWN;
119	}
120
121	const char* ToString(char* buffer, size_t size);
122};
123
124
125struct DynamicAttributeValue {
126	union {
127		uint64				constant;
128		DebugInfoEntry*		reference;
129		struct {
130			const void*		data;
131			off_t			length;
132		}					block;
133	};
134	uint8				attributeClass;
135
136	DynamicAttributeValue()
137		:
138		attributeClass(ATTRIBUTE_CLASS_UNKNOWN)
139	{
140		this->constant = 0;
141	}
142
143	bool IsValid() const
144	{
145		return attributeClass != ATTRIBUTE_CLASS_UNKNOWN;
146	}
147
148	void SetTo(uint64 constant)
149	{
150		this->constant = constant;
151		attributeClass = ATTRIBUTE_CLASS_CONSTANT;
152	}
153
154	void SetTo(DebugInfoEntry* reference)
155	{
156		this->reference = reference;
157		attributeClass = ATTRIBUTE_CLASS_REFERENCE;
158	}
159
160	void SetTo(const void* data, off_t length)
161	{
162		block.data = data;
163		block.length = length;
164		attributeClass = ATTRIBUTE_CLASS_BLOCK;
165	}
166};
167
168
169struct ConstantAttributeValue {
170	union {
171		uint64				constant;
172		const char*			string;
173		struct {
174			const void*		data;
175			off_t			length;
176		}					block;
177	};
178	uint8				attributeClass;
179
180	ConstantAttributeValue()
181		:
182		attributeClass(ATTRIBUTE_CLASS_UNKNOWN)
183	{
184	}
185
186	bool IsValid() const
187	{
188		return attributeClass != ATTRIBUTE_CLASS_UNKNOWN;
189	}
190
191	void SetTo(uint64 constant)
192	{
193		this->constant = constant;
194		attributeClass = ATTRIBUTE_CLASS_CONSTANT;
195	}
196
197	void SetTo(const char* string)
198	{
199		this->string = string;
200		attributeClass = ATTRIBUTE_CLASS_STRING;
201	}
202
203	void SetTo(const void* data, off_t length)
204	{
205		block.data = data;
206		block.length = length;
207		attributeClass = ATTRIBUTE_CLASS_BLOCK;
208	}
209};
210
211
212struct MemberLocation {
213	union {
214		uint64				constant;
215		off_t				listOffset;
216		struct {
217			const void*		data;
218			off_t			length;
219		}					expression;
220	};
221	uint8				attributeClass;
222
223	MemberLocation()
224		:
225		attributeClass(ATTRIBUTE_CLASS_UNKNOWN)
226	{
227	}
228
229	bool IsValid() const
230	{
231		return attributeClass != ATTRIBUTE_CLASS_UNKNOWN;
232	}
233
234	bool IsConstant() const
235	{
236		return attributeClass == ATTRIBUTE_CLASS_CONSTANT;
237	}
238
239	bool IsExpression() const
240	{
241		return attributeClass == ATTRIBUTE_CLASS_BLOCK
242			&& expression.data != NULL;
243	}
244
245	bool IsLocationList() const
246	{
247		return attributeClass == ATTRIBUTE_CLASS_LOCLISTPTR;
248	}
249
250	void SetToConstant(uint64 constant)
251	{
252		this->constant = constant;
253		attributeClass = ATTRIBUTE_CLASS_CONSTANT;
254	}
255
256	void SetToExpression(const void* data, off_t length)
257	{
258		expression.data = data;
259		expression.length = length;
260		attributeClass = ATTRIBUTE_CLASS_BLOCK;
261	}
262
263	void SetToLocationList(off_t listOffset)
264	{
265		this->listOffset = listOffset;
266		attributeClass = ATTRIBUTE_CLASS_LOCLISTPTR;
267	}
268};
269
270
271struct LocationDescription {
272	union {
273		off_t			listOffset;	// location list
274		struct {
275			const void*	data;
276			off_t		length;
277		}				expression;	// location expression
278	};
279	uint8				attributeClass;
280
281	LocationDescription()
282		:
283		attributeClass(ATTRIBUTE_CLASS_BLOCK)
284	{
285		expression.data = NULL;
286		expression.length = 0;
287	}
288
289	bool IsExpression() const
290	{
291		return attributeClass == ATTRIBUTE_CLASS_BLOCK
292			&& expression.data != NULL;
293	}
294
295	bool IsLocationList() const
296	{
297		return attributeClass == ATTRIBUTE_CLASS_LOCLISTPTR;
298	}
299
300	bool IsValid() const
301	{
302		return IsExpression() || IsLocationList();
303	}
304
305	void SetToLocationList(off_t offset)
306	{
307		listOffset = offset;
308		attributeClass = ATTRIBUTE_CLASS_LOCLISTPTR;
309	}
310
311	void SetToExpression(const void* data, off_t length)
312	{
313		expression.data = data;
314		expression.length = length;
315		attributeClass = ATTRIBUTE_CLASS_BLOCK;
316	}
317};
318
319
320struct DeclarationLocation {
321	uint32	file;
322	uint32	line;
323	uint32	column;
324
325	DeclarationLocation()
326		:
327		file(0xffffffff),
328		line(0xffffffff),
329		column(0xffffffff)
330	{
331	}
332
333	void SetFile(uint32 file)
334	{
335		this->file = file;
336	}
337
338	void SetLine(uint32 line)
339	{
340		this->line = line;
341	}
342
343	void SetColumn(uint32 column)
344	{
345		this->column = column;
346	}
347
348	bool IsFileSet() const
349	{
350		return file != 0xffffffff;
351	}
352
353	bool IsLineSet() const
354	{
355		return line != 0xffffffff;
356	}
357
358	bool IsColumnSet() const
359	{
360		return column != 0xffffffff;
361	}
362};
363
364#endif	// ATTRIBUTE_VALUE_H
365