as_datatype.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2017 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_datatype.cpp
  25. //
  26. // This class describes the datatype for expressions during compilation
  27. //
  28. #include "as_config.h"
  29. #include "as_datatype.h"
  30. #include "as_tokendef.h"
  31. #include "as_typeinfo.h"
  32. #include "as_objecttype.h"
  33. #include "as_scriptengine.h"
  34. #include "as_tokenizer.h"
  35. BEGIN_AS_NAMESPACE
  36. asCDataType::asCDataType()
  37. {
  38. tokenType = ttUnrecognizedToken;
  39. typeInfo = 0;
  40. isReference = false;
  41. isReadOnly = false;
  42. isAuto = false;
  43. isObjectHandle = false;
  44. isConstHandle = false;
  45. isHandleToAsHandleType = false;
  46. ifHandleThenConst = false;
  47. }
  48. asCDataType::asCDataType(const asCDataType &dt)
  49. {
  50. tokenType = dt.tokenType;
  51. typeInfo = dt.typeInfo;
  52. isReference = dt.isReference;
  53. isReadOnly = dt.isReadOnly;
  54. isAuto = dt.isAuto;
  55. isObjectHandle = dt.isObjectHandle;
  56. isConstHandle = dt.isConstHandle;
  57. isHandleToAsHandleType = dt.isHandleToAsHandleType;
  58. ifHandleThenConst = dt.ifHandleThenConst;
  59. }
  60. asCDataType::~asCDataType()
  61. {
  62. }
  63. bool asCDataType::IsValid() const
  64. {
  65. if( tokenType == ttUnrecognizedToken &&
  66. !isObjectHandle )
  67. return false;
  68. return true;
  69. }
  70. asCDataType asCDataType::CreateType(asCTypeInfo *ti, bool isConst)
  71. {
  72. asCDataType dt;
  73. dt.tokenType = ttIdentifier;
  74. dt.typeInfo = ti;
  75. dt.isReadOnly = isConst;
  76. return dt;
  77. }
  78. asCDataType asCDataType::CreateAuto(bool isConst)
  79. {
  80. asCDataType dt;
  81. dt.tokenType = ttIdentifier;
  82. dt.isReadOnly = isConst;
  83. dt.isAuto = true;
  84. return dt;
  85. }
  86. asCDataType asCDataType::CreateObjectHandle(asCTypeInfo *ot, bool isConst)
  87. {
  88. asCDataType dt;
  89. asASSERT(CastToObjectType(ot));
  90. dt.tokenType = ttIdentifier;
  91. dt.typeInfo = ot;
  92. dt.isObjectHandle = true;
  93. dt.isConstHandle = isConst;
  94. return dt;
  95. }
  96. asCDataType asCDataType::CreatePrimitive(eTokenType tt, bool isConst)
  97. {
  98. asCDataType dt;
  99. dt.tokenType = tt;
  100. dt.isReadOnly = isConst;
  101. return dt;
  102. }
  103. asCDataType asCDataType::CreateNullHandle()
  104. {
  105. asCDataType dt;
  106. dt.tokenType = ttUnrecognizedToken;
  107. dt.isReadOnly = true;
  108. dt.isObjectHandle = true;
  109. dt.isConstHandle = true;
  110. return dt;
  111. }
  112. bool asCDataType::IsNullHandle() const
  113. {
  114. if( tokenType == ttUnrecognizedToken &&
  115. typeInfo == 0 &&
  116. isObjectHandle )
  117. return true;
  118. return false;
  119. }
  120. asCString asCDataType::Format(asSNameSpace *currNs, bool includeNamespace) const
  121. {
  122. if( IsNullHandle() )
  123. return "<null handle>";
  124. asCString str;
  125. if( isReadOnly )
  126. str = "const ";
  127. // If the type is not declared in the current namespace, then the namespace
  128. // must always be informed to guarantee that the correct type is informed
  129. if (includeNamespace || (typeInfo && typeInfo->nameSpace != currNs))
  130. {
  131. if (typeInfo && typeInfo->nameSpace && typeInfo->nameSpace->name != "")
  132. str += typeInfo->nameSpace->name + "::";
  133. }
  134. if (typeInfo && typeInfo->nameSpace == 0)
  135. {
  136. // If funcDef->nameSpace is null it means the funcDef was declared as member of
  137. // another type, in which case the scope should be built with the name of that type
  138. str += CastToFuncdefType(typeInfo)->parentClass->name + "::";
  139. }
  140. if( tokenType != ttIdentifier )
  141. {
  142. str += asCTokenizer::GetDefinition(tokenType);
  143. }
  144. else if( IsArrayType() && typeInfo && !typeInfo->engine->ep.expandDefaultArrayToTemplate )
  145. {
  146. asCObjectType *ot = CastToObjectType(typeInfo);
  147. asASSERT( ot && ot->templateSubTypes.GetLength() == 1 );
  148. str += ot->templateSubTypes[0].Format(currNs, includeNamespace);
  149. str += "[]";
  150. }
  151. else if(typeInfo)
  152. {
  153. str += typeInfo->name;
  154. asCObjectType *ot = CastToObjectType(typeInfo);
  155. if( ot && ot->templateSubTypes.GetLength() > 0 )
  156. {
  157. str += "<";
  158. for( asUINT subtypeIndex = 0; subtypeIndex < ot->templateSubTypes.GetLength(); subtypeIndex++ )
  159. {
  160. str += ot->templateSubTypes[subtypeIndex].Format(currNs, includeNamespace);
  161. if( subtypeIndex != ot->templateSubTypes.GetLength()-1 )
  162. str += ",";
  163. }
  164. str += ">";
  165. }
  166. }
  167. else if( isAuto )
  168. {
  169. str += "<auto>";
  170. }
  171. else
  172. {
  173. str = "<unknown>";
  174. }
  175. if( isObjectHandle )
  176. {
  177. str += "@";
  178. if( isConstHandle )
  179. str += "const";
  180. }
  181. if( isReference )
  182. str += "&";
  183. return str;
  184. }
  185. asCDataType &asCDataType::operator =(const asCDataType &dt)
  186. {
  187. tokenType = dt.tokenType;
  188. isReference = dt.isReference;
  189. typeInfo = dt.typeInfo;
  190. isReadOnly = dt.isReadOnly;
  191. isObjectHandle = dt.isObjectHandle;
  192. isConstHandle = dt.isConstHandle;
  193. isAuto = dt.isAuto;
  194. isHandleToAsHandleType = dt.isHandleToAsHandleType;
  195. ifHandleThenConst = dt.ifHandleThenConst;
  196. return (asCDataType &)*this;
  197. }
  198. int asCDataType::MakeHandle(bool b, bool acceptHandleForScope)
  199. {
  200. if( !b )
  201. {
  202. isObjectHandle = false;
  203. isConstHandle = false;
  204. isHandleToAsHandleType = false;
  205. }
  206. else
  207. {
  208. if( isAuto )
  209. {
  210. isObjectHandle = true;
  211. }
  212. else if( !isObjectHandle )
  213. {
  214. // Only reference types are allowed to be handles,
  215. // but not nohandle reference types, and not scoped references
  216. // (except when returned from registered function)
  217. // funcdefs are special reference types and support handles
  218. // value types with asOBJ_ASHANDLE are treated as a handle
  219. if( (!typeInfo ||
  220. !((typeInfo->flags & asOBJ_REF) || (typeInfo->flags & asOBJ_TEMPLATE_SUBTYPE) || (typeInfo->flags & asOBJ_ASHANDLE) || (typeInfo->flags & asOBJ_FUNCDEF)) ||
  221. (typeInfo->flags & asOBJ_NOHANDLE) ||
  222. ((typeInfo->flags & asOBJ_SCOPED) && !acceptHandleForScope)) )
  223. return -1;
  224. isObjectHandle = b;
  225. isConstHandle = false;
  226. // ASHANDLE supports being handle, but as it really is a value type it will not be marked as a handle
  227. if( (typeInfo->flags & asOBJ_ASHANDLE) )
  228. {
  229. isObjectHandle = false;
  230. isHandleToAsHandleType = true;
  231. }
  232. }
  233. }
  234. return 0;
  235. }
  236. int asCDataType::MakeArray(asCScriptEngine *engine, asCModule *module)
  237. {
  238. if( engine->defaultArrayObjectType == 0 )
  239. return asINVALID_TYPE;
  240. bool tmpIsReadOnly = isReadOnly;
  241. isReadOnly = false;
  242. asCArray<asCDataType> subTypes;
  243. subTypes.PushLast(*this);
  244. asCObjectType *at = engine->GetTemplateInstanceType(engine->defaultArrayObjectType, subTypes, module);
  245. isReadOnly = tmpIsReadOnly;
  246. isObjectHandle = false;
  247. isConstHandle = false;
  248. typeInfo = at;
  249. tokenType = ttIdentifier;
  250. return 0;
  251. }
  252. int asCDataType::MakeReference(bool b)
  253. {
  254. isReference = b;
  255. return 0;
  256. }
  257. int asCDataType::MakeReadOnly(bool b)
  258. {
  259. if( isObjectHandle )
  260. {
  261. isConstHandle = b;
  262. return 0;
  263. }
  264. isReadOnly = b;
  265. return 0;
  266. }
  267. int asCDataType::MakeHandleToConst(bool b)
  268. {
  269. if( !isObjectHandle ) return -1;
  270. isReadOnly = b;
  271. return 0;
  272. }
  273. bool asCDataType::SupportHandles() const
  274. {
  275. if( typeInfo &&
  276. (typeInfo->flags & (asOBJ_REF | asOBJ_ASHANDLE | asOBJ_FUNCDEF)) &&
  277. !(typeInfo->flags & asOBJ_NOHANDLE) &&
  278. !isObjectHandle )
  279. return true;
  280. return false;
  281. }
  282. bool asCDataType::CanBeInstantiated() const
  283. {
  284. if( GetSizeOnStackDWords() == 0 ) // Void
  285. return false;
  286. if( !IsObject() && !IsFuncdef() ) // Primitives
  287. return true;
  288. if (IsNullHandle()) // null
  289. return false;
  290. if( IsObjectHandle() && !(typeInfo->flags & asOBJ_NOHANDLE) ) // Handles
  291. return true;
  292. // Funcdefs cannot be instantiated without being handles
  293. // The exception being delegates, but these can only be created as temporary objects
  294. if (IsFuncdef())
  295. return false;
  296. asCObjectType *ot = CastToObjectType(typeInfo);
  297. if( ot && (ot->flags & asOBJ_REF) && ot->beh.factories.GetLength() == 0 ) // ref types without factories
  298. return false;
  299. if( ot && (ot->flags & asOBJ_ABSTRACT) && !IsObjectHandle() ) // Can't instantiate abstract classes
  300. return false;
  301. return true;
  302. }
  303. bool asCDataType::IsAbstractClass() const
  304. {
  305. return typeInfo && (typeInfo->flags & asOBJ_ABSTRACT) ? true : false;
  306. }
  307. bool asCDataType::IsInterface() const
  308. {
  309. if (typeInfo == 0)
  310. return false;
  311. asCObjectType *ot = CastToObjectType(typeInfo);
  312. return ot && ot->IsInterface();
  313. }
  314. bool asCDataType::CanBeCopied() const
  315. {
  316. // All primitives can be copied
  317. if( IsPrimitive() ) return true;
  318. // Plain-old-data structures can always be copied
  319. if( typeInfo->flags & asOBJ_POD ) return true;
  320. // It must be possible to instantiate the type
  321. if( !CanBeInstantiated() ) return false;
  322. // It must have a default constructor or factory and the opAssign
  323. // Alternatively it must have the copy constructor
  324. asCObjectType *ot = CastToObjectType(typeInfo);
  325. if (ot && (((ot->beh.construct != 0 || ot->beh.factory != 0) && ot->beh.copy != 0) ||
  326. (ot->beh.copyconstruct != 0 || ot->beh.copyfactory != 0)) )
  327. return true;
  328. return false;
  329. }
  330. bool asCDataType::IsReadOnly() const
  331. {
  332. if( isObjectHandle )
  333. return isConstHandle;
  334. return isReadOnly;
  335. }
  336. bool asCDataType::IsHandleToConst() const
  337. {
  338. if( !isObjectHandle ) return false;
  339. return isReadOnly;
  340. }
  341. bool asCDataType::IsObjectConst() const
  342. {
  343. if( IsObjectHandle() )
  344. return IsHandleToConst();
  345. return IsReadOnly();
  346. }
  347. // TODO: 3.0.0: This should be removed
  348. bool asCDataType::IsArrayType() const
  349. {
  350. // This is only true if the type used is the default array type, i.e. the one used for the [] syntax form
  351. if( typeInfo && typeInfo->engine->defaultArrayObjectType )
  352. return typeInfo->name == typeInfo->engine->defaultArrayObjectType->name;
  353. return false;
  354. }
  355. bool asCDataType::IsTemplate() const
  356. {
  357. if( typeInfo && (typeInfo->flags & asOBJ_TEMPLATE) )
  358. return true;
  359. return false;
  360. }
  361. bool asCDataType::IsScriptObject() const
  362. {
  363. if( typeInfo && (typeInfo->flags & asOBJ_SCRIPT_OBJECT) )
  364. return true;
  365. return false;
  366. }
  367. asCDataType asCDataType::GetSubType(asUINT subtypeIndex) const
  368. {
  369. asASSERT(typeInfo);
  370. asCObjectType *ot = CastToObjectType(typeInfo);
  371. return ot->templateSubTypes[subtypeIndex];
  372. }
  373. bool asCDataType::operator !=(const asCDataType &dt) const
  374. {
  375. return !(*this == dt);
  376. }
  377. bool asCDataType::operator ==(const asCDataType &dt) const
  378. {
  379. if( !IsEqualExceptRefAndConst(dt) ) return false;
  380. if( isReference != dt.isReference ) return false;
  381. if( isReadOnly != dt.isReadOnly ) return false;
  382. if( isConstHandle != dt.isConstHandle ) return false;
  383. return true;
  384. }
  385. bool asCDataType::IsEqualExceptRef(const asCDataType &dt) const
  386. {
  387. if( !IsEqualExceptRefAndConst(dt) ) return false;
  388. if( isReadOnly != dt.isReadOnly ) return false;
  389. if( isConstHandle != dt.isConstHandle ) return false;
  390. return true;
  391. }
  392. bool asCDataType::IsEqualExceptRefAndConst(const asCDataType &dt) const
  393. {
  394. // Check base type
  395. if( tokenType != dt.tokenType ) return false;
  396. if( typeInfo != dt.typeInfo ) return false;
  397. if( isObjectHandle != dt.isObjectHandle ) return false;
  398. if( isObjectHandle )
  399. if( isReadOnly != dt.isReadOnly ) return false;
  400. return true;
  401. }
  402. bool asCDataType::IsEqualExceptConst(const asCDataType &dt) const
  403. {
  404. if( !IsEqualExceptRefAndConst(dt) ) return false;
  405. if( isReference != dt.isReference ) return false;
  406. return true;
  407. }
  408. bool asCDataType::IsPrimitive() const
  409. {
  410. // Enumerations are primitives
  411. if( IsEnumType() )
  412. return true;
  413. // A registered object is never a primitive neither is a pointer nor an array
  414. if( typeInfo )
  415. return false;
  416. // Null handle doesn't have a typeInfo, but it is not a primitive
  417. if( tokenType == ttUnrecognizedToken )
  418. return false;
  419. return true;
  420. }
  421. bool asCDataType::IsMathType() const
  422. {
  423. if( tokenType == ttInt || tokenType == ttInt8 || tokenType == ttInt16 || tokenType == ttInt64 ||
  424. tokenType == ttUInt || tokenType == ttUInt8 || tokenType == ttUInt16 || tokenType == ttUInt64 ||
  425. tokenType == ttFloat || tokenType == ttDouble )
  426. return true;
  427. return false;
  428. }
  429. bool asCDataType::IsIntegerType() const
  430. {
  431. if( tokenType == ttInt ||
  432. tokenType == ttInt8 ||
  433. tokenType == ttInt16 ||
  434. tokenType == ttInt64 )
  435. return true;
  436. // Enums are also integer types
  437. return IsEnumType();
  438. }
  439. bool asCDataType::IsUnsignedType() const
  440. {
  441. if( tokenType == ttUInt ||
  442. tokenType == ttUInt8 ||
  443. tokenType == ttUInt16 ||
  444. tokenType == ttUInt64 )
  445. return true;
  446. return false;
  447. }
  448. bool asCDataType::IsFloatType() const
  449. {
  450. if( tokenType == ttFloat )
  451. return true;
  452. return false;
  453. }
  454. bool asCDataType::IsDoubleType() const
  455. {
  456. if( tokenType == ttDouble )
  457. return true;
  458. return false;
  459. }
  460. bool asCDataType::IsBooleanType() const
  461. {
  462. if( tokenType == ttBool )
  463. return true;
  464. return false;
  465. }
  466. bool asCDataType::IsObject() const
  467. {
  468. if( IsPrimitive() )
  469. return false;
  470. // Null handle doesn't have an object type but should still be considered an object
  471. if( typeInfo == 0 )
  472. return IsNullHandle();
  473. // Template subtypes shouldn't be considered objects
  474. return CastToObjectType(typeInfo) ? true : false;
  475. }
  476. bool asCDataType::IsFuncdef() const
  477. {
  478. if (typeInfo && (typeInfo->flags & asOBJ_FUNCDEF))
  479. return true;
  480. return false;
  481. }
  482. int asCDataType::GetSizeInMemoryBytes() const
  483. {
  484. if( typeInfo != 0 )
  485. return typeInfo->size;
  486. if( tokenType == ttVoid )
  487. return 0;
  488. if( tokenType == ttInt8 ||
  489. tokenType == ttUInt8 )
  490. return 1;
  491. if( tokenType == ttInt16 ||
  492. tokenType == ttUInt16 )
  493. return 2;
  494. if( tokenType == ttDouble ||
  495. tokenType == ttInt64 ||
  496. tokenType == ttUInt64 )
  497. return 8;
  498. if( tokenType == ttBool )
  499. return AS_SIZEOF_BOOL;
  500. // null handle
  501. if( tokenType == ttUnrecognizedToken )
  502. return 4*AS_PTR_SIZE;
  503. return 4;
  504. }
  505. int asCDataType::GetSizeInMemoryDWords() const
  506. {
  507. int s = GetSizeInMemoryBytes();
  508. if( s == 0 ) return 0;
  509. if( s <= 4 ) return 1;
  510. // Pad the size to 4 bytes
  511. if( s & 0x3 )
  512. s += 4 - (s & 0x3);
  513. return s/4;
  514. }
  515. int asCDataType::GetSizeOnStackDWords() const
  516. {
  517. // If the type is the variable type then the typeid is stored on the stack too
  518. int size = tokenType == ttQuestion ? 1 : 0;
  519. if( isReference ) return AS_PTR_SIZE + size;
  520. if( typeInfo && !IsEnumType() ) return AS_PTR_SIZE + size;
  521. return GetSizeInMemoryDWords() + size;
  522. }
  523. #ifdef WIP_16BYTE_ALIGN
  524. int asCDataType::GetAlignment() const
  525. {
  526. if( typeInfo == NULL )
  527. {
  528. // TODO: Small primitives should not be aligned to 4 byte boundaries
  529. return 4; //Default alignment
  530. }
  531. return typeInfo->alignment;
  532. }
  533. #endif
  534. asSTypeBehaviour *asCDataType::GetBehaviour() const
  535. {
  536. if (!typeInfo) return 0;
  537. asCObjectType *ot = CastToObjectType(typeInfo);
  538. return ot ? &ot->beh : 0;
  539. }
  540. bool asCDataType::IsEnumType() const
  541. {
  542. // Do a sanity check on the objectType, to verify that we aren't trying to access memory after it has been released
  543. asASSERT(typeInfo == 0 || typeInfo->name.GetLength() < 100);
  544. if (typeInfo && (typeInfo->flags & asOBJ_ENUM))
  545. return true;
  546. return false;
  547. }
  548. END_AS_NAMESPACE