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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
|
/*
AngelCode Scripting Library
Copyright (c) 2003-2017 Andreas Jonsson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_datatype.cpp
//
// This class describes the datatype for expressions during compilation
//
#include "as_config.h"
#include "as_datatype.h"
#include "as_tokendef.h"
#include "as_typeinfo.h"
#include "as_objecttype.h"
#include "as_scriptengine.h"
#include "as_tokenizer.h"
BEGIN_AS_NAMESPACE
asCDataType::asCDataType()
{
tokenType = ttUnrecognizedToken;
typeInfo = 0;
isReference = false;
isReadOnly = false;
isAuto = false;
isObjectHandle = false;
isConstHandle = false;
isHandleToAsHandleType = false;
ifHandleThenConst = false;
}
asCDataType::asCDataType(const asCDataType &dt)
{
tokenType = dt.tokenType;
typeInfo = dt.typeInfo;
isReference = dt.isReference;
isReadOnly = dt.isReadOnly;
isAuto = dt.isAuto;
isObjectHandle = dt.isObjectHandle;
isConstHandle = dt.isConstHandle;
isHandleToAsHandleType = dt.isHandleToAsHandleType;
ifHandleThenConst = dt.ifHandleThenConst;
}
asCDataType::~asCDataType()
{
}
bool asCDataType::IsValid() const
{
if( tokenType == ttUnrecognizedToken &&
!isObjectHandle )
return false;
return true;
}
asCDataType asCDataType::CreateType(asCTypeInfo *ti, bool isConst)
{
asCDataType dt;
dt.tokenType = ttIdentifier;
dt.typeInfo = ti;
dt.isReadOnly = isConst;
return dt;
}
asCDataType asCDataType::CreateAuto(bool isConst)
{
asCDataType dt;
dt.tokenType = ttIdentifier;
dt.isReadOnly = isConst;
dt.isAuto = true;
return dt;
}
asCDataType asCDataType::CreateObjectHandle(asCTypeInfo *ot, bool isConst)
{
asCDataType dt;
asASSERT(CastToObjectType(ot));
dt.tokenType = ttIdentifier;
dt.typeInfo = ot;
dt.isObjectHandle = true;
dt.isConstHandle = isConst;
return dt;
}
asCDataType asCDataType::CreatePrimitive(eTokenType tt, bool isConst)
{
asCDataType dt;
dt.tokenType = tt;
dt.isReadOnly = isConst;
return dt;
}
asCDataType asCDataType::CreateNullHandle()
{
asCDataType dt;
dt.tokenType = ttUnrecognizedToken;
dt.isReadOnly = true;
dt.isObjectHandle = true;
dt.isConstHandle = true;
return dt;
}
bool asCDataType::IsNullHandle() const
{
if( tokenType == ttUnrecognizedToken &&
typeInfo == 0 &&
isObjectHandle )
return true;
return false;
}
asCString asCDataType::Format(asSNameSpace *currNs, bool includeNamespace) const
{
if( IsNullHandle() )
return "<null handle>";
asCString str;
if( isReadOnly )
str = "const ";
// If the type is not declared in the current namespace, then the namespace
// must always be informed to guarantee that the correct type is informed
if (includeNamespace || (typeInfo && typeInfo->nameSpace != currNs))
{
if (typeInfo && typeInfo->nameSpace && typeInfo->nameSpace->name != "")
str += typeInfo->nameSpace->name + "::";
}
if (typeInfo && typeInfo->nameSpace == 0)
{
// If funcDef->nameSpace is null it means the funcDef was declared as member of
// another type, in which case the scope should be built with the name of that type
str += CastToFuncdefType(typeInfo)->parentClass->name + "::";
}
if( tokenType != ttIdentifier )
{
str += asCTokenizer::GetDefinition(tokenType);
}
else if( IsArrayType() && typeInfo && !typeInfo->engine->ep.expandDefaultArrayToTemplate )
{
asCObjectType *ot = CastToObjectType(typeInfo);
asASSERT( ot && ot->templateSubTypes.GetLength() == 1 );
str += ot->templateSubTypes[0].Format(currNs, includeNamespace);
str += "[]";
}
else if(typeInfo)
{
str += typeInfo->name;
asCObjectType *ot = CastToObjectType(typeInfo);
if( ot && ot->templateSubTypes.GetLength() > 0 )
{
str += "<";
for( asUINT subtypeIndex = 0; subtypeIndex < ot->templateSubTypes.GetLength(); subtypeIndex++ )
{
str += ot->templateSubTypes[subtypeIndex].Format(currNs, includeNamespace);
if( subtypeIndex != ot->templateSubTypes.GetLength()-1 )
str += ",";
}
str += ">";
}
}
else if( isAuto )
{
str += "<auto>";
}
else
{
str = "<unknown>";
}
if( isObjectHandle )
{
str += "@";
if( isConstHandle )
str += "const";
}
if( isReference )
str += "&";
return str;
}
asCDataType &asCDataType::operator =(const asCDataType &dt)
{
tokenType = dt.tokenType;
isReference = dt.isReference;
typeInfo = dt.typeInfo;
isReadOnly = dt.isReadOnly;
isObjectHandle = dt.isObjectHandle;
isConstHandle = dt.isConstHandle;
isAuto = dt.isAuto;
isHandleToAsHandleType = dt.isHandleToAsHandleType;
ifHandleThenConst = dt.ifHandleThenConst;
return (asCDataType &)*this;
}
int asCDataType::MakeHandle(bool b, bool acceptHandleForScope)
{
if( !b )
{
isObjectHandle = false;
isConstHandle = false;
isHandleToAsHandleType = false;
}
else
{
if( isAuto )
{
isObjectHandle = true;
}
else if( !isObjectHandle )
{
// Only reference types are allowed to be handles,
// but not nohandle reference types, and not scoped references
// (except when returned from registered function)
// funcdefs are special reference types and support handles
// value types with asOBJ_ASHANDLE are treated as a handle
if( (!typeInfo ||
!((typeInfo->flags & asOBJ_REF) || (typeInfo->flags & asOBJ_TEMPLATE_SUBTYPE) || (typeInfo->flags & asOBJ_ASHANDLE) || (typeInfo->flags & asOBJ_FUNCDEF)) ||
(typeInfo->flags & asOBJ_NOHANDLE) ||
((typeInfo->flags & asOBJ_SCOPED) && !acceptHandleForScope)) )
return -1;
isObjectHandle = b;
isConstHandle = false;
// ASHANDLE supports being handle, but as it really is a value type it will not be marked as a handle
if( (typeInfo->flags & asOBJ_ASHANDLE) )
{
isObjectHandle = false;
isHandleToAsHandleType = true;
}
}
}
return 0;
}
int asCDataType::MakeArray(asCScriptEngine *engine, asCModule *module)
{
if( engine->defaultArrayObjectType == 0 )
return asINVALID_TYPE;
bool tmpIsReadOnly = isReadOnly;
isReadOnly = false;
asCArray<asCDataType> subTypes;
subTypes.PushLast(*this);
asCObjectType *at = engine->GetTemplateInstanceType(engine->defaultArrayObjectType, subTypes, module);
isReadOnly = tmpIsReadOnly;
isObjectHandle = false;
isConstHandle = false;
typeInfo = at;
tokenType = ttIdentifier;
return 0;
}
int asCDataType::MakeReference(bool b)
{
isReference = b;
return 0;
}
int asCDataType::MakeReadOnly(bool b)
{
if( isObjectHandle )
{
isConstHandle = b;
return 0;
}
isReadOnly = b;
return 0;
}
int asCDataType::MakeHandleToConst(bool b)
{
if( !isObjectHandle ) return -1;
isReadOnly = b;
return 0;
}
bool asCDataType::SupportHandles() const
{
if( typeInfo &&
(typeInfo->flags & (asOBJ_REF | asOBJ_ASHANDLE | asOBJ_FUNCDEF)) &&
!(typeInfo->flags & asOBJ_NOHANDLE) &&
!isObjectHandle )
return true;
return false;
}
bool asCDataType::CanBeInstantiated() const
{
if( GetSizeOnStackDWords() == 0 ) // Void
return false;
if( !IsObject() && !IsFuncdef() ) // Primitives
return true;
if (IsNullHandle()) // null
return false;
if( IsObjectHandle() && !(typeInfo->flags & asOBJ_NOHANDLE) ) // Handles
return true;
// Funcdefs cannot be instantiated without being handles
// The exception being delegates, but these can only be created as temporary objects
if (IsFuncdef())
return false;
asCObjectType *ot = CastToObjectType(typeInfo);
if( ot && (ot->flags & asOBJ_REF) && ot->beh.factories.GetLength() == 0 ) // ref types without factories
return false;
if( ot && (ot->flags & asOBJ_ABSTRACT) && !IsObjectHandle() ) // Can't instantiate abstract classes
return false;
return true;
}
bool asCDataType::IsAbstractClass() const
{
return typeInfo && (typeInfo->flags & asOBJ_ABSTRACT) ? true : false;
}
bool asCDataType::IsInterface() const
{
if (typeInfo == 0)
return false;
asCObjectType *ot = CastToObjectType(typeInfo);
return ot && ot->IsInterface();
}
bool asCDataType::CanBeCopied() const
{
// All primitives can be copied
if( IsPrimitive() ) return true;
// Plain-old-data structures can always be copied
if( typeInfo->flags & asOBJ_POD ) return true;
// It must be possible to instantiate the type
if( !CanBeInstantiated() ) return false;
// It must have a default constructor or factory and the opAssign
// Alternatively it must have the copy constructor
asCObjectType *ot = CastToObjectType(typeInfo);
if (ot && (((ot->beh.construct != 0 || ot->beh.factory != 0) && ot->beh.copy != 0) ||
(ot->beh.copyconstruct != 0 || ot->beh.copyfactory != 0)) )
return true;
return false;
}
bool asCDataType::IsReadOnly() const
{
if( isObjectHandle )
return isConstHandle;
return isReadOnly;
}
bool asCDataType::IsHandleToConst() const
{
if( !isObjectHandle ) return false;
return isReadOnly;
}
bool asCDataType::IsObjectConst() const
{
if( IsObjectHandle() )
return IsHandleToConst();
return IsReadOnly();
}
// TODO: 3.0.0: This should be removed
bool asCDataType::IsArrayType() const
{
// This is only true if the type used is the default array type, i.e. the one used for the [] syntax form
if( typeInfo && typeInfo->engine->defaultArrayObjectType )
return typeInfo->name == typeInfo->engine->defaultArrayObjectType->name;
return false;
}
bool asCDataType::IsTemplate() const
{
if( typeInfo && (typeInfo->flags & asOBJ_TEMPLATE) )
return true;
return false;
}
bool asCDataType::IsScriptObject() const
{
if( typeInfo && (typeInfo->flags & asOBJ_SCRIPT_OBJECT) )
return true;
return false;
}
asCDataType asCDataType::GetSubType(asUINT subtypeIndex) const
{
asASSERT(typeInfo);
asCObjectType *ot = CastToObjectType(typeInfo);
return ot->templateSubTypes[subtypeIndex];
}
bool asCDataType::operator !=(const asCDataType &dt) const
{
return !(*this == dt);
}
bool asCDataType::operator ==(const asCDataType &dt) const
{
if( !IsEqualExceptRefAndConst(dt) ) return false;
if( isReference != dt.isReference ) return false;
if( isReadOnly != dt.isReadOnly ) return false;
if( isConstHandle != dt.isConstHandle ) return false;
return true;
}
bool asCDataType::IsEqualExceptRef(const asCDataType &dt) const
{
if( !IsEqualExceptRefAndConst(dt) ) return false;
if( isReadOnly != dt.isReadOnly ) return false;
if( isConstHandle != dt.isConstHandle ) return false;
return true;
}
bool asCDataType::IsEqualExceptRefAndConst(const asCDataType &dt) const
{
// Check base type
if( tokenType != dt.tokenType ) return false;
if( typeInfo != dt.typeInfo ) return false;
if( isObjectHandle != dt.isObjectHandle ) return false;
if( isObjectHandle )
if( isReadOnly != dt.isReadOnly ) return false;
return true;
}
bool asCDataType::IsEqualExceptConst(const asCDataType &dt) const
{
if( !IsEqualExceptRefAndConst(dt) ) return false;
if( isReference != dt.isReference ) return false;
return true;
}
bool asCDataType::IsPrimitive() const
{
// Enumerations are primitives
if( IsEnumType() )
return true;
// A registered object is never a primitive neither is a pointer nor an array
if( typeInfo )
return false;
// Null handle doesn't have a typeInfo, but it is not a primitive
if( tokenType == ttUnrecognizedToken )
return false;
return true;
}
bool asCDataType::IsMathType() const
{
if( tokenType == ttInt || tokenType == ttInt8 || tokenType == ttInt16 || tokenType == ttInt64 ||
tokenType == ttUInt || tokenType == ttUInt8 || tokenType == ttUInt16 || tokenType == ttUInt64 ||
tokenType == ttFloat || tokenType == ttDouble )
return true;
return false;
}
bool asCDataType::IsIntegerType() const
{
if( tokenType == ttInt ||
tokenType == ttInt8 ||
tokenType == ttInt16 ||
tokenType == ttInt64 )
return true;
// Enums are also integer types
return IsEnumType();
}
bool asCDataType::IsUnsignedType() const
{
if( tokenType == ttUInt ||
tokenType == ttUInt8 ||
tokenType == ttUInt16 ||
tokenType == ttUInt64 )
return true;
return false;
}
bool asCDataType::IsFloatType() const
{
if( tokenType == ttFloat )
return true;
return false;
}
bool asCDataType::IsDoubleType() const
{
if( tokenType == ttDouble )
return true;
return false;
}
bool asCDataType::IsBooleanType() const
{
if( tokenType == ttBool )
return true;
return false;
}
bool asCDataType::IsObject() const
{
if( IsPrimitive() )
return false;
// Null handle doesn't have an object type but should still be considered an object
if( typeInfo == 0 )
return IsNullHandle();
// Template subtypes shouldn't be considered objects
return CastToObjectType(typeInfo) ? true : false;
}
bool asCDataType::IsFuncdef() const
{
if (typeInfo && (typeInfo->flags & asOBJ_FUNCDEF))
return true;
return false;
}
int asCDataType::GetSizeInMemoryBytes() const
{
if( typeInfo != 0 )
return typeInfo->size;
if( tokenType == ttVoid )
return 0;
if( tokenType == ttInt8 ||
tokenType == ttUInt8 )
return 1;
if( tokenType == ttInt16 ||
tokenType == ttUInt16 )
return 2;
if( tokenType == ttDouble ||
tokenType == ttInt64 ||
tokenType == ttUInt64 )
return 8;
if( tokenType == ttBool )
return AS_SIZEOF_BOOL;
// null handle
if( tokenType == ttUnrecognizedToken )
return 4*AS_PTR_SIZE;
return 4;
}
int asCDataType::GetSizeInMemoryDWords() const
{
int s = GetSizeInMemoryBytes();
if( s == 0 ) return 0;
if( s <= 4 ) return 1;
// Pad the size to 4 bytes
if( s & 0x3 )
s += 4 - (s & 0x3);
return s/4;
}
int asCDataType::GetSizeOnStackDWords() const
{
// If the type is the variable type then the typeid is stored on the stack too
int size = tokenType == ttQuestion ? 1 : 0;
if( isReference ) return AS_PTR_SIZE + size;
if( typeInfo && !IsEnumType() ) return AS_PTR_SIZE + size;
return GetSizeInMemoryDWords() + size;
}
#ifdef WIP_16BYTE_ALIGN
int asCDataType::GetAlignment() const
{
if( typeInfo == NULL )
{
// TODO: Small primitives should not be aligned to 4 byte boundaries
return 4; //Default alignment
}
return typeInfo->alignment;
}
#endif
asSTypeBehaviour *asCDataType::GetBehaviour() const
{
if (!typeInfo) return 0;
asCObjectType *ot = CastToObjectType(typeInfo);
return ot ? &ot->beh : 0;
}
bool asCDataType::IsEnumType() const
{
// Do a sanity check on the objectType, to verify that we aren't trying to access memory after it has been released
asASSERT(typeInfo == 0 || typeInfo->name.GetLength() < 100);
if (typeInfo && (typeInfo->flags & asOBJ_ENUM))
return true;
return false;
}
END_AS_NAMESPACE
|