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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
#pragma once
#include "CppName.h"
#include "SrcPos.h"
#include "Auto.h"
#include "Size.h"
class Type;
class Template;
class World;
/**
* Represents a reference to a type in C++. (ie. whenever we're using a type).
*/
class TypeRef : public Refcount {
public:
TypeRef(const SrcPos &pos);
// Position of this type.
SrcPos pos;
// Is this type const?
bool constType;
// Get the size of this type.
virtual Size size() const = 0;
// Is this a gc:d type?
virtual bool gcType() const = 0;
// Resolve type info.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const = 0;
// Print.
virtual void print(wostream &to) const = 0;
// Get an ID to sort functions.
// This does not properly account for all cases to be globally unique, it will, however,
// give a decent ordering of functions to make cache-locality a bit better when unpacking
// at least.
// It is currently constructed as follows:
// The last two digits indicate modifiers (e.g. templates, references). The upper ones are references.
virtual size_t sortId() const = 0;
};
inline wostream &operator <<(wostream &to, const TypeRef &c) {
c.print(to);
if (c.constType)
to << L" const";
return to;
}
/**
* Generic templated type. Only templates in the last position are supported at the moment.
*/
class TemplateType : public TypeRef {
public:
TemplateType(const SrcPos &pos, const CppName &name, const vector<Auto<TypeRef>> ¶ms = vector<Auto<TypeRef>>());
// Name.
CppName name;
// Template types.
vector<Auto<TypeRef>> params;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Resolved templated type.
*/
class ResolvedTemplateType : public TypeRef {
public:
ResolvedTemplateType(const SrcPos &pos, Template *templ, const vector<Auto<TypeRef>> ¶ms);
// Template type.
Template *type;
// Template types.
vector<Auto<TypeRef>> params;
// Get size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return true; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Pointer type.
*/
class PtrType : public TypeRef {
public:
PtrType(Auto<TypeRef> of);
// Type.
Auto<TypeRef> of;
// Get the size of this type.
virtual Size size() const { return Size::sPtr; }
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Ref type.
*/
class RefType : public TypeRef {
public:
RefType(Auto<TypeRef> of);
// Type.
Auto<TypeRef> of;
// Get the size of this type.
virtual Size size() const { return Size::sPtr; }
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Maybe type, for classes (i.e. MAYBE(T *))
*/
class MaybeClassType : public TypeRef {
public:
MaybeClassType(Auto<TypeRef> of);
// Type.
Auto<PtrType> of;
// Get the size of this type.
virtual Size size() const { return of->size(); }
// Is this a gc:d type?
virtual bool gcType() const { return of->gcType(); }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Maybe type, for values (i.e. Maybe<T>).
*/
class MaybeValueType : public TypeRef {
public:
MaybeValueType(Auto<TypeRef> of);
// Type.
Auto<TypeRef> of;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return of->gcType(); }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Named type.
*/
class NamedType : public TypeRef {
public:
NamedType(const SrcPos &pos, const CppName &name);
NamedType(const SrcPos &pos, const String &name);
// Name.
CppName name;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Resolved type.
*/
class ResolvedType : public TypeRef {
public:
explicit ResolvedType(Type *type);
ResolvedType(const TypeRef &templ, Type *type);
// Type.
Type *type;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const;
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Type built into C++.
*/
class BuiltInType : public TypeRef {
public:
BuiltInType(const SrcPos &pos, const String &name, Size size);
// Name.
String name;
// Size.
Size tSize;
// Get the size of this type.
virtual Size size() const { return tSize; }
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* Void type.
*/
class VoidType : public TypeRef {
public:
VoidType(const SrcPos &pos);
// Get the size of this type.
virtual Size size() const { return Size(); }
// Gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* GcArray type.
*/
class GcArrayType : public TypeRef {
public:
GcArrayType(const SrcPos &pos, Auto<TypeRef> of, bool weak);
// Type of what?
Auto<TypeRef> of;
// Weak?
bool weak;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return true; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* GcWatch type.
*/
class GcWatchType : public TypeRef {
public:
GcWatchType(const SrcPos &pos);
// Size of this type.
virtual Size size() const;
// Gc:d type?
virtual bool gcType() const { return true; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* EnginePtr type.
*/
class EnginePtrType : public TypeRef {
public:
EnginePtrType(const SrcPos &pos) : TypeRef(pos) {}
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return false; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
/**
* GcType type.
*/
class GcTypeType : public TypeRef {
public:
GcTypeType(const SrcPos &pos) : TypeRef(pos) {}
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const { return true; }
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Print.
virtual void print(wostream &to) const;
// Get sort id.
virtual size_t sortId() const;
};
class UnknownPrimitive;
/**
* Unknown type. These are to allow GC:d classes to contain types unknown to the preprocessor
* without having to allocate them separatly all the time.
*
* Supports various kind of external types:
* PTR_NOGC - pointer to non-gc object.
* PTR_GC - pointer to gc object.
* INT - integer sized object.
*
* Also: if 'id' is the name of another type, it substitutes that.
*/
class UnknownType : public TypeRef {
public:
// Create. 'id' is the kind of external type.
UnknownType(const CppName &id, Auto<TypeRef> of);
// Type of what?
Auto<TypeRef> of;
// Get the size of this type.
virtual Size size() const;
// Is this a gc:d type?
virtual bool gcType() const;
// Resolve.
virtual Auto<TypeRef> resolve(World &in, const CppName &context) const;
// Find an 'unknown' type wrapper for this type.
Auto<TypeRef> wrapper(World &in) const;
// Print.
virtual void print(wostream &to) const;
// Description of an id.
struct ID {
const wchar_t *name;
const Size &size; // needs to be a reference for some reason.
bool gc;
};
// All known ids.
static const ID ids[];
// Get sort id.
virtual size_t sortId() const;
private:
// Current id.
const ID *id;
// The alternative type we've resolved to.
Auto<TypeRef> alias;
};
inline bool isGcPtr(Auto<TypeRef> t) {
if (Auto<PtrType> p = t.as<PtrType>()) {
return p->of->gcType();
} else if (Auto<RefType> r = t.as<RefType>()) {
return r->of->gcType();
} else if (Auto<MaybeClassType> r = t.as<MaybeClassType>()) {
return isGcPtr(r->of);
} else if (Auto<UnknownType> r = t.as<UnknownType>()) {
return r->gcType();
} else {
return false;
}
}
inline Auto<TypeRef> makeConst(Auto<TypeRef> v) {
v->constType = true;
return v;
}
|