| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- /*
- 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
- [email protected]
- */
- //
- // 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
|