this repo has no description
1#ifndef RESOURCEDEFINITIONS_H
2#define RESOURCEDEFINITIONS_H
3
4#include <iosfwd>
5#include <memory>
6#include <map>
7
8#include "Expression.h"
9#include "ResType.h"
10
11#include "location.hh"
12
13class TypeSpec
14{
15 ResType type;
16 int id;
17public:
18 static const int noID = 65536;
19
20 TypeSpec() : id(noID) {}
21 TypeSpec(ResType type) : type(type), id(noID) {}
22 TypeSpec(ResType type, int id) : type(type), id(id) {}
23
24 ResType getType() const { return type; }
25 int getID() const { return id; }
26
27 bool hasID() const { return id != noID; }
28
29 bool operator<(TypeSpec y) const
30 {
31 if(type < y.type)
32 return true;
33 else if(y.type < type)
34 return false;
35 else
36 return id < y.id;
37 }
38};
39
40std::ostream& operator<<(std::ostream& out, TypeSpec ts);
41
42
43class ResourceCompiler;
44
45class Field
46{
47public:
48 yy::location location;
49
50 virtual ~Field() = default;
51
52 virtual bool needsValue() { return true; }
53
54 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass) = 0;
55
56 virtual ExprPtr lookupNamedValue(std::string) { return nullptr; }
57};
58typedef std::shared_ptr<Field> FieldPtr;
59
60class SimpleField : public Field
61{
62public:
63 enum class Type
64 {
65 boolean, byte, integer, longint, rect, point, char_,
66 pstring, wstring, string, bitstring
67 };
68
69 enum class Attrs
70 {
71 none = 0, hex = 1, key = 2, unsigned_ = 4, literal = 8, binary = 16
72 };
73
74 Type type;
75 Attrs attrs = Attrs::none;
76 ExprPtr arrayCount;
77
78 ExprPtr value;
79 std::map<std::string, ExprPtr> namedValues;
80 ExprPtr lastNamedValue;
81
82 void addNamedValue(std::string n);
83 void addNamedValue(std::string n, ExprPtr val);
84 ExprPtr lookupNamedValue(std::string);
85
86 virtual bool needsValue();
87 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
88
89private:
90 void compileString(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
91 void compileInt(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
92 void compileCompound(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
93};
94typedef std::shared_ptr<SimpleField> SimpleFieldPtr;
95
96class FillAlignField : public Field
97{
98public:
99 enum class Type
100 {
101 bit, nibble, byte, word, long_
102 };
103
104 FillAlignField(Type type, bool isAlign, ExprPtr count = ExprPtr());
105 virtual bool needsValue();
106 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
107private:
108 Type type;
109 ExprPtr count;
110 bool isAlign;
111};
112
113inline SimpleField::Attrs operator|(SimpleField::Attrs a, SimpleField::Attrs b)
114{
115 return SimpleField::Attrs( int(a) | int(b) );
116}
117
118
119class LabelField : public Field
120{
121 std::string name;
122public:
123 LabelField(std::string name);
124
125 virtual bool needsValue();
126 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
127};
128typedef std::shared_ptr<LabelField> LabelFieldPtr;
129
130
131class FieldList : public Field
132{
133protected:
134 std::vector<FieldPtr> fields;
135public:
136 virtual ~FieldList();
137 void addField(FieldPtr field, yy::location loc);
138 void addLabel(std::string name, yy::location loc);
139
140 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
141};
142typedef std::shared_ptr<FieldList> FieldListPtr;
143
144
145class ArrayField : public FieldList
146{
147 std::string name;
148 ExprPtr arrayCount;
149public:
150 ArrayField(std::string name /* or empty */, ExprPtr count /* may be null*/);
151
152 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
153};
154typedef std::shared_ptr<ArrayField> ArrayFieldPtr;
155
156class SwitchField : public Field
157{
158 std::map<std::string, FieldListPtr> cases;
159public:
160 void addCase(const std::string name, FieldListPtr alternative);
161
162 virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
163};
164typedef std::shared_ptr<SwitchField> SwitchFieldPtr;
165
166class TypeDefinition : public FieldList
167{
168};
169typedef std::shared_ptr<TypeDefinition> TypeDefinitionPtr;
170
171#endif // RESOURCEDEFINITIONS_H