1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#pragma once
#include "SrcPos.h"
#include "CppName.h"
#include "TypeRef.h"
#include "Variable.h"
#include "Function.h"
#include "Namespace.h"
#include "Thread.h"
#include "Doc.h"
class World;
struct ScannedVar {
CppName typeName;
String varName;
};
/**
* Describes a type in C++.
*/
class Type : public Refcount {
public:
// Create a type with name X, where X is the fully qualified name of the type (eg. std::string, Foo:Bar::Baz).
Type(const CppName &name, const String &pkg, const SrcPos &pos, const Auto<Doc> &doc);
// The ID of this type. Set during world.prepare().
nat id;
// Name of this type.
CppName name;
// Package.
String pkg;
// Position of this type.
SrcPos pos;
// Documentation for this type.
Auto<Doc> doc;
// Is this type external to this unit?
bool external;
// Is this type private? (used with nested types)
bool isPrivate;
// Print.
virtual void print(wostream &to) const = 0;
// Resolve types in here.
virtual void resolveTypes(World &world) = 0;
// Compute the unaligned size of this type.
virtual Size rawSize() const = 0;
// Compute the size of this type.
Size size() const;
// Is this type heap-allocated? (value types return false, class types return true).
virtual bool heapAlloc() const = 0;
// Compute pointer offsets into this type.
vector<Offset> ptrOffsets() const;
virtual void ptrOffsets(vector<Offset> &append) const = 0;
// Get all members who need to be scanned (same set as in 'ptrOffsets').
vector<ScannedVar> scannedVars() const;
virtual void scannedVars(vector<ScannedVar> &append) const = 0;
};
wostream &operator <<(wostream &to, const Type &type);
/**
* Describes a class or struct from C++.
*/
class Class : public Type {
public:
// Create a type with name X, where X is the fully qualified name of the type (eg. Foo::Bar::Baz).
Class(const CppName &name, const String &pkg, const SrcPos &pos, const Auto<Doc> &doc);
/**
* Flags for this class.
*/
enum Flags {
// Is this a value-type?
value = 0x01,
// Is this an abstract type? If it is abstract, we allow it to contain abstract
// functions. Otherwise we don't.
abstract = 0x02,
// Is this an exception type? If so, we will generate a 'throwMe' member. We also expect it
// to inherit from an exception type super class.
exception = 0x04,
// Same as 'exceptionType', except that we don't require the parent class to be an
// exception, as this is supposedly the one and only storm::Exception class.
rootException = 0x08 | 0x04,
// Hidden parent?
hiddenParent = 0x10,
// Have we found a destructor?
dtorFound = 0x20,
};
// Flags.
nat flags;
// Flags manipulation.
inline bool has(Flags flag) const { return (flags & flag) == flag; }
inline void set(Flags flag) { flags |= flag; }
inline void clear(Flags flag) { flags &= ~nat(flag); }
// Parent class (if any).
CppName parent;
// Actual type of the parent class.
Type *parentType;
// Which thread is this type associated to?
CppName thread;
// The thread object we're associated with (only for the classes directly inheriting from ObjectOn<T>).
Thread *threadType;
// Is this an actor? Only usable after resolveTypes() have been called.
bool isActor() const;
// Member variables (non-static). All have their name relative to the enclosing type.
vector<Variable> variables;
// Does this type have a declared destructor?
bool hasDtor() const;
// Add a variable.
void add(const Variable &v);
// Add a function.
void add(const Function &f);
// Resolve types in here.
virtual void resolveTypes(World &world);
// Check exceptions. Done after 'resolveTypes'.
void checkException();
// Compute our size.
virtual Size rawSize() const;
// Compute our pointer offsets.
virtual void ptrOffsets(vector<Offset> &append) const;
virtual void scannedVars(vector<ScannedVar> &append) const;
// External offset computation. First call 'baseOffset' to initialize the 'size' variable. Then
// call 'varOffset' for 'var' = 0, 1, .... 'varOffset' returns the offset of variable 'var' and
// updates the size variable.
Size baseOffset() const;
Offset varOffset(nat var, Size &offset) const;
// Print ourselves.
virtual void print(wostream &to) const;
// Is this type heap-allocated?
virtual bool heapAlloc() const { return !has(value); }
};
/**
* Namespace for a class.
*/
class ClassNamespace : public Namespace {
public:
ClassNamespace(World &world, Class &owner);
virtual void add(const Variable &v);
virtual void add(const Function &f);
private:
World &world;
Class &owner;
};
/**
* Describe a primitive type in Storm.
*/
class Primitive : public Type {
public:
// Create.
Primitive(const CppName &name, const String &pkg, const CppName &generate, const SrcPos &pos, const Auto<Doc> &doc);
// Function used to generate this primitive.
CppName generate;
// Print.
virtual void print(wostream &to) const;
// Resolve types in here.
virtual void resolveTypes(World &world);
// Compute the size of this type.
virtual Size rawSize() const;
// Is this type heap-allocated?
virtual bool heapAlloc() const;
// Compute pointer offsets into this type.
virtual void ptrOffsets(vector<Offset> &append) const;
virtual void scannedVars(vector<ScannedVar> &append) const;
private:
// Size of this type.
Size mySize;
};
/**
* Describe an unknown primitive in Storm.
*/
class UnknownPrimitive : public Type {
public:
// Create.
UnknownPrimitive(const CppName &name, const String &pkg, const CppName &generate, const SrcPos &pos);
// Function used to generate this primitive.
CppName generate;
// Print.
virtual void print(wostream &to) const;
// Resolve types in here.
virtual void resolveTypes(World &world);
// Compute the size of this type.
virtual Size rawSize() const;
// Is this type heap-allocated?
virtual bool heapAlloc() const;
// Compute pointer offsets into this type.
virtual void ptrOffsets(vector<Offset> &append) const;
virtual void scannedVars(vector<ScannedVar> &append) const;
private:
// Size of this type.
Size mySize;
// Is it a GC:d pointer?
bool gcPtr;
};
/**
* Describe an enumeration type from C++.
*/
class Enum : public Type {
public:
// Create.
Enum(const CppName &name, const String &pkg, const SrcPos &pos, const Auto<Doc> &doc);
// Members in the enum (not their values, we can easily generate code that fetches those for us).
vector<String> members;
// Names of the enum members in Storm.
vector<String> stormMembers;
// Documentation for all members.
vector<Auto<Doc>> memberDoc;
// Is this enum used as a bitmask?
bool bitmask;
// Resolve types.
virtual void resolveTypes(World &world);
// Compute size.
virtual Size rawSize() const;
// Generate pointer offsets.
virtual void ptrOffsets(vector<Offset> &append) const;
virtual void scannedVars(vector<ScannedVar> &append) const;
// Print.
virtual void print(wostream &to) const;
// Never allocated on heap.
virtual bool heapAlloc() const { return false; }
};
/**
* Describe a template type from C++ (declared using STORM_TEMPLATE(x))
*/
class Template : public Refcount {
public:
// Create.
Template(const CppName &name, const String &pkg, const CppName &generator, const SrcPos &pos, const Auto<Doc> &doc);
// Our id. Set when added to the world.
nat id;
// Name.
CppName name;
// Package.
String pkg;
// Function generating Storm types (only in the compiler right now).
CppName generator;
// Position.
SrcPos pos;
// Documentation.
Auto<Doc> doc;
// External symbol?
bool external;
};
|