ptype.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing types for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. symtype,symdef,symbase;
  23. const
  24. { forward types should only be possible inside a TYPE statement }
  25. typecanbeforward : boolean = false;
  26. var
  27. { hack, which allows to use the current parsed }
  28. { object type as function argument type }
  29. testcurobject : byte;
  30. { reads a type identifier }
  31. procedure id_type(var def : tdef;isforwarddef:boolean);
  32. { reads a string, file type or a type identifier }
  33. procedure single_type(var def:tdef;isforwarddef:boolean);
  34. { reads any type declaration, where the resulting type will get name as type identifier }
  35. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  36. { reads any type declaration }
  37. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  38. { generate persistent type information like VMT, RTTI and inittables }
  39. procedure write_persistent_type_info(st:tsymtable);
  40. implementation
  41. uses
  42. { common }
  43. cutils,
  44. { global }
  45. globals,tokens,verbose,constexp,
  46. systems,
  47. { target }
  48. paramgr,procinfo,
  49. { symtable }
  50. symconst,symsym,symtable,
  51. defutil,defcmp,
  52. { modules }
  53. fmodule,
  54. { pass 1 }
  55. node,ncgrtti,nobj,
  56. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  57. { parser }
  58. scanner,
  59. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  60. procedure generate_specialization(var tt:tdef);
  61. var
  62. st : TSymtable;
  63. srsym : tsym;
  64. pt2 : tnode;
  65. first,
  66. err : boolean;
  67. i : longint;
  68. sym : tsym;
  69. old_block_type : tblock_type;
  70. genericdef : tstoreddef;
  71. generictype : ttypesym;
  72. generictypelist : TFPObjectList;
  73. oldsymtablestack : tsymtablestack;
  74. hmodule : tmodule;
  75. pu : tused_unit;
  76. uspecializename,
  77. specializename : string;
  78. vmtbuilder : TVMTBuilder;
  79. onlyparsepara : boolean;
  80. specializest : tsymtable;
  81. begin
  82. { retrieve generic def that we are going to replace }
  83. genericdef:=tstoreddef(tt);
  84. tt:=nil;
  85. onlyparsepara:=false;
  86. if not(df_generic in genericdef.defoptions) then
  87. begin
  88. Message(parser_e_special_onlygenerics);
  89. tt:=generrordef;
  90. onlyparsepara:=true;
  91. end;
  92. { Only need to record the tokens, then we don't know the type yet }
  93. if parse_generic then
  94. begin
  95. tt:=cundefinedtype;
  96. onlyparsepara:=true;
  97. end;
  98. { Only parse the parameters for recovery or
  99. for recording in genericbuf }
  100. if onlyparsepara then
  101. begin
  102. consume(_LSHARPBRACKET);
  103. repeat
  104. pt2:=factor(false);
  105. pt2.free;
  106. until not try_to_consume(_COMMA);
  107. consume(_RSHARPBRACKET);
  108. exit;
  109. end;
  110. consume(_LSHARPBRACKET);
  111. old_block_type:=block_type;
  112. block_type:=bt_specialize;
  113. { Parse generic parameters, for each undefineddef in the symtable of
  114. the genericdef we need to have a new def }
  115. err:=false;
  116. first:=true;
  117. generictypelist:=TFPObjectList.create(false);
  118. case genericdef.typ of
  119. procdef :
  120. st:=genericdef.GetSymtable(gs_para);
  121. objectdef,
  122. recorddef :
  123. st:=genericdef.GetSymtable(gs_record);
  124. end;
  125. if not assigned(st) then
  126. internalerror(200511182);
  127. { Parse type parameters }
  128. if not assigned(genericdef.typesym) then
  129. internalerror(200710173);
  130. specializename:=genericdef.typesym.realname;
  131. for i:=0 to st.SymList.Count-1 do
  132. begin
  133. sym:=tsym(st.SymList[i]);
  134. if (sp_generic_para in sym.symoptions) then
  135. begin
  136. if not first then
  137. consume(_COMMA)
  138. else
  139. first:=false;
  140. pt2:=factor(false);
  141. if pt2.nodetype=typen then
  142. begin
  143. if df_generic in pt2.resultdef.defoptions then
  144. Message(parser_e_no_generics_as_params);
  145. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  146. generictypelist.add(generictype);
  147. if not assigned(pt2.resultdef.typesym) then
  148. internalerror(200710172);
  149. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  150. end
  151. else
  152. begin
  153. Message(type_e_type_id_expected);
  154. err:=true;
  155. end;
  156. pt2.free;
  157. end;
  158. end;
  159. uspecializename:=upper(specializename);
  160. { force correct error location if too much type parameters are passed }
  161. if token<>_RSHARPBRACKET then
  162. consume(_RSHARPBRACKET);
  163. { Special case if we are referencing the current defined object }
  164. if assigned(current_objectdef) and
  165. (current_objectdef.objname^=uspecializename) then
  166. tt:=current_objectdef;
  167. { for units specializations can already be needed in the interface, therefor we
  168. will use the global symtable. Programs don't have a globalsymtable and there we
  169. use the localsymtable }
  170. if current_module.is_unit then
  171. specializest:=current_module.globalsymtable
  172. else
  173. specializest:=current_module.localsymtable;
  174. { Can we reuse an already specialized type? }
  175. if not assigned(tt) then
  176. begin
  177. srsym:=tsym(specializest.find(uspecializename));
  178. if assigned(srsym) then
  179. begin
  180. if srsym.typ<>typesym then
  181. internalerror(200710171);
  182. tt:=ttypesym(srsym).typedef;
  183. end;
  184. end;
  185. if not assigned(tt) then
  186. begin
  187. { Setup symtablestack at definition time
  188. to get types right, however this is not perfect, we should probably record
  189. the resolved symbols }
  190. oldsymtablestack:=symtablestack;
  191. symtablestack:=tsymtablestack.create;
  192. if not assigned(genericdef) then
  193. internalerror(200705151);
  194. hmodule:=find_module_from_symtable(genericdef.owner);
  195. if hmodule=nil then
  196. internalerror(200705152);
  197. pu:=tused_unit(hmodule.used_units.first);
  198. while assigned(pu) do
  199. begin
  200. if not assigned(pu.u.globalsymtable) then
  201. internalerror(200705153);
  202. symtablestack.push(pu.u.globalsymtable);
  203. pu:=tused_unit(pu.next);
  204. end;
  205. if assigned(hmodule.globalsymtable) then
  206. symtablestack.push(hmodule.globalsymtable);
  207. { hacky, but necessary to insert the newly generated class properly }
  208. symtablestack.push(oldsymtablestack.top);
  209. { Reparse the original type definition }
  210. if not err then
  211. begin
  212. { Firsta new typesym so we can reuse this specialization and
  213. references to this specialization can be handled }
  214. srsym:=ttypesym.create(specializename,generrordef);
  215. specializest.insert(srsym);
  216. if not assigned(genericdef.generictokenbuf) then
  217. internalerror(200511171);
  218. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  219. read_named_type(tt,specializename,genericdef,generictypelist,false);
  220. ttypesym(srsym).typedef:=tt;
  221. tt.typesym:=srsym;
  222. { Consume the semicolon if it is also recorded }
  223. try_to_consume(_SEMICOLON);
  224. { Build VMT indexes for classes }
  225. if (tt.typ=objectdef) then
  226. begin
  227. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  228. vmtbuilder.generate_vmt;
  229. vmtbuilder.free;
  230. end;
  231. end;
  232. { Restore symtablestack }
  233. symtablestack.free;
  234. symtablestack:=oldsymtablestack;
  235. end;
  236. generictypelist.free;
  237. consume(_RSHARPBRACKET);
  238. block_type:=old_block_type;
  239. end;
  240. procedure id_type(var def : tdef;isforwarddef:boolean);
  241. { reads a type definition }
  242. { to a appropriating tdef, s gets the name of }
  243. { the type to allow name mangling }
  244. var
  245. is_unit_specific : boolean;
  246. pos : tfileposinfo;
  247. srsym : tsym;
  248. srsymtable : TSymtable;
  249. s,sorg : TIDString;
  250. t : ttoken;
  251. begin
  252. s:=pattern;
  253. sorg:=orgpattern;
  254. pos:=current_tokenpos;
  255. { use of current parsed object:
  256. - classes can be used also in classes
  257. - objects can be parameters }
  258. if assigned(current_objectdef) and
  259. (current_objectdef.objname^=pattern) and
  260. (
  261. (testcurobject=2) or
  262. is_class_or_interface(current_objectdef)
  263. )then
  264. begin
  265. consume(_ID);
  266. def:=current_objectdef;
  267. exit;
  268. end;
  269. { Use the special searchsym_type that ignores records,objects and
  270. parameters }
  271. searchsym_type(s,srsym,srsymtable);
  272. { handle unit specification like System.Writeln }
  273. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  274. consume(t);
  275. { Types are first defined with an error def before assigning
  276. the real type so check if it's an errordef. if so then
  277. give an error. Only check for typesyms in the current symbol
  278. table as forwarddef are not resolved directly }
  279. if assigned(srsym) and
  280. (srsym.typ=typesym) and
  281. (ttypesym(srsym).typedef.typ=errordef) then
  282. begin
  283. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  284. def:=generrordef;
  285. exit;
  286. end;
  287. { are we parsing a possible forward def ? }
  288. if isforwarddef and
  289. not(is_unit_specific) then
  290. begin
  291. def:=tforwarddef.create(s,pos);
  292. exit;
  293. end;
  294. { unknown sym ? }
  295. if not assigned(srsym) then
  296. begin
  297. Message1(sym_e_id_not_found,sorg);
  298. def:=generrordef;
  299. exit;
  300. end;
  301. { type sym ? }
  302. if (srsym.typ<>typesym) then
  303. begin
  304. Message(type_e_type_id_expected);
  305. def:=generrordef;
  306. exit;
  307. end;
  308. { Give an error when referring to an errordef }
  309. if (ttypesym(srsym).typedef.typ=errordef) then
  310. begin
  311. Message(sym_e_error_in_type_def);
  312. def:=generrordef;
  313. exit;
  314. end;
  315. def:=ttypesym(srsym).typedef;
  316. end;
  317. procedure single_type(var def:tdef;isforwarddef:boolean);
  318. var
  319. t2 : tdef;
  320. dospecialize,
  321. again : boolean;
  322. begin
  323. dospecialize:=false;
  324. repeat
  325. again:=false;
  326. case token of
  327. _STRING:
  328. string_dec(def);
  329. _FILE:
  330. begin
  331. consume(_FILE);
  332. if try_to_consume(_OF) then
  333. begin
  334. single_type(t2,false);
  335. def:=tfiledef.createtyped(t2);
  336. end
  337. else
  338. def:=cfiletype;
  339. end;
  340. _ID:
  341. begin
  342. if try_to_consume(_SPECIALIZE) then
  343. begin
  344. dospecialize:=true;
  345. again:=true;
  346. end
  347. else
  348. begin
  349. id_type(def,isforwarddef);
  350. { handle types inside classes for generics, e.g. TNode.TLongint }
  351. while (token=_POINT) do
  352. begin
  353. if parse_generic then
  354. begin
  355. consume(_POINT);
  356. consume(_ID);
  357. end
  358. else if ((def.typ=objectdef) and (df_specialization in def.defoptions)) then
  359. begin
  360. symtablestack.push(tobjectdef(def).symtable);
  361. consume(_POINT);
  362. id_type(t2,isforwarddef);
  363. symtablestack.pop(tobjectdef(def).symtable);
  364. def:=t2;
  365. end
  366. else
  367. break;
  368. end;
  369. end;
  370. end;
  371. else
  372. begin
  373. message(type_e_type_id_expected);
  374. def:=generrordef;
  375. end;
  376. end;
  377. until not again;
  378. if dospecialize then
  379. generate_specialization(def)
  380. else
  381. begin
  382. if (df_generic in def.defoptions) then
  383. begin
  384. Message(parser_e_no_generics_as_types);
  385. def:=generrordef;
  386. end;
  387. end;
  388. end;
  389. { reads a record declaration }
  390. function record_dec : tdef;
  391. var
  392. recst : trecordsymtable;
  393. storetypecanbeforward : boolean;
  394. old_object_option : tsymoptions;
  395. begin
  396. { create recdef }
  397. recst:=trecordsymtable.create(current_settings.packrecords);
  398. record_dec:=trecorddef.create(recst);
  399. { insert in symtablestack }
  400. symtablestack.push(recst);
  401. { parse record }
  402. consume(_RECORD);
  403. old_object_option:=current_object_option;
  404. current_object_option:=[sp_public];
  405. storetypecanbeforward:=typecanbeforward;
  406. { for tp7 don't allow forward types }
  407. if m_tp7 in current_settings.modeswitches then
  408. typecanbeforward:=false;
  409. read_record_fields([vd_record]);
  410. consume(_END);
  411. typecanbeforward:=storetypecanbeforward;
  412. current_object_option:=old_object_option;
  413. { make the record size aligned }
  414. recst.addalignmentpadding;
  415. { restore symtable stack }
  416. symtablestack.pop(recst);
  417. if trecorddef(record_dec).is_packed and
  418. record_dec.needs_inittable then
  419. Message(type_e_no_packed_inittable);
  420. end;
  421. { reads a type definition and returns a pointer to it }
  422. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  423. var
  424. pt : tnode;
  425. tt2 : tdef;
  426. aktenumdef : tenumdef;
  427. s : TIDString;
  428. l,v : TConstExprInt;
  429. oldpackrecords : longint;
  430. defpos,storepos : tfileposinfo;
  431. procedure expr_type;
  432. var
  433. pt1,pt2 : tnode;
  434. lv,hv : TConstExprInt;
  435. old_block_type : tblock_type;
  436. dospecialize : boolean;
  437. begin
  438. old_block_type:=block_type;
  439. dospecialize:=false;
  440. { use of current parsed object:
  441. - classes can be used also in classes
  442. - objects can be parameters }
  443. if (token=_ID) and
  444. assigned(current_objectdef) and
  445. (current_objectdef.objname^=pattern) and
  446. (
  447. (testcurobject=2) or
  448. is_class_or_interface(current_objectdef)
  449. )then
  450. begin
  451. consume(_ID);
  452. def:=current_objectdef;
  453. exit;
  454. end;
  455. { Generate a specialization? }
  456. if try_to_consume(_SPECIALIZE) then
  457. dospecialize:=true;
  458. { we can't accept a equal in type }
  459. pt1:=comp_expr(false);
  460. if not dospecialize and
  461. try_to_consume(_POINTPOINT) then
  462. begin
  463. { get high value of range }
  464. pt2:=comp_expr(false);
  465. { make both the same type or give an error. This is not
  466. done when both are integer values, because typecasting
  467. between -3200..3200 will result in a signed-unsigned
  468. conflict and give a range check error (PFV) }
  469. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  470. inserttypeconv(pt1,pt2.resultdef);
  471. { both must be evaluated to constants now }
  472. if (pt1.nodetype=ordconstn) and
  473. (pt2.nodetype=ordconstn) then
  474. begin
  475. lv:=tordconstnode(pt1).value;
  476. hv:=tordconstnode(pt2).value;
  477. { Check bounds }
  478. if hv<lv then
  479. message(parser_e_upper_lower_than_lower)
  480. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  481. message(type_e_cant_eval_constant_expr)
  482. else
  483. begin
  484. { All checks passed, create the new def }
  485. case pt1.resultdef.typ of
  486. enumdef :
  487. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  488. orddef :
  489. begin
  490. if is_char(pt1.resultdef) then
  491. def:=torddef.create(uchar,lv,hv)
  492. else
  493. if is_boolean(pt1.resultdef) then
  494. def:=torddef.create(pasbool,lv,hv)
  495. else if is_signed(pt1.resultdef) then
  496. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  497. else
  498. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  499. end;
  500. end;
  501. end;
  502. end
  503. else
  504. Message(sym_e_error_in_type_def);
  505. pt2.free;
  506. end
  507. else
  508. begin
  509. { a simple type renaming or generic specialization }
  510. if (pt1.nodetype=typen) then
  511. begin
  512. def:=ttypenode(pt1).resultdef;
  513. if dospecialize then
  514. generate_specialization(def)
  515. else
  516. begin
  517. if (df_generic in def.defoptions) then
  518. begin
  519. Message(parser_e_no_generics_as_types);
  520. def:=generrordef;
  521. end;
  522. end;
  523. end
  524. else
  525. Message(sym_e_error_in_type_def);
  526. end;
  527. pt1.free;
  528. block_type:=old_block_type;
  529. end;
  530. procedure set_dec;
  531. begin
  532. consume(_SET);
  533. consume(_OF);
  534. read_anon_type(tt2,true);
  535. if assigned(tt2) then
  536. begin
  537. case tt2.typ of
  538. { don't forget that min can be negativ PM }
  539. enumdef :
  540. if (tenumdef(tt2).min>=0) and
  541. (tenumdef(tt2).max<=255) then
  542. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  543. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  544. else
  545. Message(sym_e_ill_type_decl_set);
  546. orddef :
  547. begin
  548. if (torddef(tt2).ordtype<>uvoid) and
  549. (torddef(tt2).ordtype<>uwidechar) and
  550. (torddef(tt2).low>=0) then
  551. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  552. if Torddef(tt2).high>int64(high(byte)) then
  553. message(sym_e_ill_type_decl_set)
  554. else
  555. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  556. else
  557. Message(sym_e_ill_type_decl_set);
  558. end;
  559. else
  560. Message(sym_e_ill_type_decl_set);
  561. end;
  562. end
  563. else
  564. def:=generrordef;
  565. end;
  566. procedure array_dec(is_packed: boolean);
  567. var
  568. lowval,
  569. highval : TConstExprInt;
  570. indexdef : tdef;
  571. hdef : tdef;
  572. arrdef : tarraydef;
  573. procedure setdefdecl(def:tdef);
  574. begin
  575. case def.typ of
  576. enumdef :
  577. begin
  578. lowval:=tenumdef(def).min;
  579. highval:=tenumdef(def).max;
  580. if (m_fpc in current_settings.modeswitches) and
  581. (tenumdef(def).has_jumps) then
  582. Message(type_e_array_index_enums_with_assign_not_possible);
  583. indexdef:=def;
  584. end;
  585. orddef :
  586. begin
  587. if torddef(def).ordtype in [uchar,
  588. u8bit,u16bit,
  589. s8bit,s16bit,s32bit,
  590. {$ifdef cpu64bitaddr}
  591. u32bit,s64bit,
  592. {$endif cpu64bitaddr}
  593. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  594. uwidechar] then
  595. begin
  596. lowval:=torddef(def).low;
  597. highval:=torddef(def).high;
  598. indexdef:=def;
  599. end
  600. else
  601. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  602. end;
  603. else
  604. Message(sym_e_error_in_type_def);
  605. end;
  606. end;
  607. begin
  608. arrdef:=nil;
  609. consume(_ARRAY);
  610. { open array? }
  611. if try_to_consume(_LECKKLAMMER) then
  612. begin
  613. { defaults }
  614. indexdef:=generrordef;
  615. { use defaults which don't overflow the compiler }
  616. lowval:=0;
  617. highval:=0;
  618. repeat
  619. { read the expression and check it, check apart if the
  620. declaration is an enum declaration because that needs to
  621. be parsed by readtype (PFV) }
  622. if token=_LKLAMMER then
  623. begin
  624. read_anon_type(hdef,true);
  625. setdefdecl(hdef);
  626. end
  627. else
  628. begin
  629. pt:=expr;
  630. if pt.nodetype=typen then
  631. setdefdecl(pt.resultdef)
  632. else
  633. begin
  634. if (pt.nodetype=rangen) then
  635. begin
  636. if (trangenode(pt).left.nodetype=ordconstn) and
  637. (trangenode(pt).right.nodetype=ordconstn) then
  638. begin
  639. { make both the same type or give an error. This is not
  640. done when both are integer values, because typecasting
  641. between -3200..3200 will result in a signed-unsigned
  642. conflict and give a range check error (PFV) }
  643. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  644. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  645. lowval:=tordconstnode(trangenode(pt).left).value;
  646. highval:=tordconstnode(trangenode(pt).right).value;
  647. if highval<lowval then
  648. begin
  649. Message(parser_e_array_lower_less_than_upper_bound);
  650. highval:=lowval;
  651. end
  652. else if (lowval<int64(low(aint))) or
  653. (highval > high(aint)) then
  654. begin
  655. Message(parser_e_array_range_out_of_bounds);
  656. lowval :=0;
  657. highval:=0;
  658. end;
  659. if is_integer(trangenode(pt).left.resultdef) then
  660. range_to_type(lowval,highval,indexdef)
  661. else
  662. indexdef:=trangenode(pt).left.resultdef;
  663. end
  664. else
  665. Message(type_e_cant_eval_constant_expr);
  666. end
  667. else
  668. Message(sym_e_error_in_type_def)
  669. end;
  670. pt.free;
  671. end;
  672. { if the array is already created add the new arrray
  673. as element of the existing array, otherwise create a new array }
  674. if assigned(arrdef) then
  675. begin
  676. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  677. arrdef:=tarraydef(arrdef.elementdef);
  678. end
  679. else
  680. begin
  681. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  682. def:=arrdef;
  683. end;
  684. if is_packed then
  685. include(arrdef.arrayoptions,ado_IsBitPacked);
  686. if token=_COMMA then
  687. consume(_COMMA)
  688. else
  689. break;
  690. until false;
  691. consume(_RECKKLAMMER);
  692. end
  693. else
  694. begin
  695. if is_packed then
  696. Message(parser_e_packed_dynamic_open_array);
  697. arrdef:=tarraydef.create(0,-1,s32inttype);
  698. include(arrdef.arrayoptions,ado_IsDynamicArray);
  699. def:=arrdef;
  700. end;
  701. consume(_OF);
  702. read_anon_type(tt2,true);
  703. { set element type of the last array definition }
  704. if assigned(arrdef) then
  705. begin
  706. arrdef.elementdef:=tt2;
  707. if is_packed and
  708. tt2.needs_inittable then
  709. Message(type_e_no_packed_inittable);
  710. end;
  711. end;
  712. var
  713. p : tnode;
  714. pd : tabstractprocdef;
  715. is_func,
  716. enumdupmsg, first : boolean;
  717. newtype : ttypesym;
  718. oldlocalswitches : tlocalswitches;
  719. bitpacking: boolean;
  720. begin
  721. def:=nil;
  722. case token of
  723. _STRING,_FILE:
  724. begin
  725. single_type(def,false);
  726. end;
  727. _LKLAMMER:
  728. begin
  729. consume(_LKLAMMER);
  730. first := true;
  731. { allow negativ value_str }
  732. l:=int64(-1);
  733. enumdupmsg:=false;
  734. aktenumdef:=tenumdef.create;
  735. repeat
  736. s:=orgpattern;
  737. defpos:=current_tokenpos;
  738. consume(_ID);
  739. { only allow assigning of specific numbers under fpc mode }
  740. if not(m_tp7 in current_settings.modeswitches) and
  741. (
  742. { in fpc mode also allow := to be compatible
  743. with previous 1.0.x versions }
  744. ((m_fpc in current_settings.modeswitches) and
  745. try_to_consume(_ASSIGNMENT)) or
  746. try_to_consume(_EQUAL)
  747. ) then
  748. begin
  749. oldlocalswitches:=current_settings.localswitches;
  750. include(current_settings.localswitches,cs_allow_enum_calc);
  751. p:=comp_expr(true);
  752. current_settings.localswitches:=oldlocalswitches;
  753. if (p.nodetype=ordconstn) then
  754. begin
  755. { we expect an integer or an enum of the
  756. same type }
  757. if is_integer(p.resultdef) or
  758. is_char(p.resultdef) or
  759. equal_defs(p.resultdef,aktenumdef) then
  760. v:=tordconstnode(p).value
  761. else
  762. IncompatibleTypes(p.resultdef,s32inttype);
  763. end
  764. else
  765. Message(parser_e_illegal_expression);
  766. p.free;
  767. { please leave that a note, allows type save }
  768. { declarations in the win32 units ! }
  769. if (not first) and (v<=l) and (not enumdupmsg) then
  770. begin
  771. Message(parser_n_duplicate_enum);
  772. enumdupmsg:=true;
  773. end;
  774. l:=v;
  775. end
  776. else
  777. inc(l.svalue);
  778. first := false;
  779. storepos:=current_tokenpos;
  780. current_tokenpos:=defpos;
  781. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  782. current_tokenpos:=storepos;
  783. until not try_to_consume(_COMMA);
  784. def:=aktenumdef;
  785. consume(_RKLAMMER);
  786. end;
  787. _ARRAY:
  788. begin
  789. array_dec(false);
  790. end;
  791. _SET:
  792. begin
  793. set_dec;
  794. end;
  795. _CARET:
  796. begin
  797. consume(_CARET);
  798. single_type(tt2,typecanbeforward);
  799. def:=tpointerdef.create(tt2);
  800. end;
  801. _RECORD:
  802. begin
  803. def:=record_dec;
  804. end;
  805. _PACKED,
  806. _BITPACKED:
  807. begin
  808. bitpacking :=
  809. (cs_bitpacking in current_settings.localswitches) or
  810. (token = _BITPACKED);
  811. consume(token);
  812. if token=_ARRAY then
  813. array_dec(bitpacking)
  814. else if token=_SET then
  815. set_dec
  816. else
  817. begin
  818. oldpackrecords:=current_settings.packrecords;
  819. if (not bitpacking) or
  820. (token in [_CLASS,_OBJECT]) then
  821. current_settings.packrecords:=1
  822. else
  823. current_settings.packrecords:=bit_alignment;
  824. if token in [_CLASS,_OBJECT] then
  825. def:=object_dec(name,genericdef,genericlist,nil)
  826. else
  827. def:=record_dec;
  828. current_settings.packrecords:=oldpackrecords;
  829. end;
  830. end;
  831. _DISPINTERFACE,
  832. _CLASS,
  833. _CPPCLASS,
  834. _INTERFACE,
  835. _OBJECT:
  836. begin
  837. def:=object_dec(name,genericdef,genericlist,nil);
  838. end;
  839. _PROCEDURE,
  840. _FUNCTION:
  841. begin
  842. is_func:=(token=_FUNCTION);
  843. consume(token);
  844. pd:=tprocvardef.create(normal_function_level);
  845. if token=_LKLAMMER then
  846. parse_parameter_dec(pd);
  847. if is_func then
  848. begin
  849. consume(_COLON);
  850. single_type(pd.returndef,false);
  851. end;
  852. if token=_OF then
  853. begin
  854. consume(_OF);
  855. consume(_OBJECT);
  856. include(pd.procoptions,po_methodpointer);
  857. end;
  858. def:=pd;
  859. { possible proc directives }
  860. if parseprocvardir then
  861. begin
  862. if check_proc_directive(true) then
  863. begin
  864. newtype:=ttypesym.create('unnamed',def);
  865. parse_var_proc_directives(tsym(newtype));
  866. newtype.typedef:=nil;
  867. def.typesym:=nil;
  868. newtype.free;
  869. end;
  870. { Add implicit hidden parameters and function result }
  871. handle_calling_convention(pd);
  872. end;
  873. end;
  874. else
  875. expr_type;
  876. end;
  877. if def=nil then
  878. def:=generrordef;
  879. end;
  880. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  881. begin
  882. read_named_type(def,'',nil,nil,parseprocvardir);
  883. end;
  884. procedure write_persistent_type_info(st:tsymtable);
  885. var
  886. i : longint;
  887. def : tdef;
  888. vmtwriter : TVMTWriter;
  889. begin
  890. for i:=0 to st.DefList.Count-1 do
  891. begin
  892. def:=tdef(st.DefList[i]);
  893. case def.typ of
  894. recorddef :
  895. write_persistent_type_info(trecorddef(def).symtable);
  896. objectdef :
  897. begin
  898. { Skip generics and forward defs }
  899. if (df_generic in def.defoptions) or
  900. (oo_is_forward in tobjectdef(def).objectoptions) then
  901. continue;
  902. write_persistent_type_info(tobjectdef(def).symtable);
  903. { Write also VMT if not done yet }
  904. if not(ds_vmt_written in def.defstates) then
  905. begin
  906. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  907. if is_interface(tobjectdef(def)) then
  908. vmtwriter.writeinterfaceids;
  909. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  910. vmtwriter.writevmt;
  911. vmtwriter.free;
  912. include(def.defstates,ds_vmt_written);
  913. end;
  914. end;
  915. procdef :
  916. begin
  917. if assigned(tprocdef(def).localst) and
  918. (tprocdef(def).localst.symtabletype=localsymtable) then
  919. write_persistent_type_info(tprocdef(def).localst);
  920. if assigned(tprocdef(def).parast) then
  921. write_persistent_type_info(tprocdef(def).parast);
  922. end;
  923. end;
  924. { generate always persistent tables for types in the interface so it can
  925. be reused in other units and give always the same pointer location. }
  926. { Init }
  927. if (
  928. assigned(def.typesym) and
  929. (st.symtabletype=globalsymtable)
  930. ) or
  931. def.needs_inittable or
  932. (ds_init_table_used in def.defstates) then
  933. RTTIWriter.write_rtti(def,initrtti);
  934. { RTTI }
  935. if (
  936. assigned(def.typesym) and
  937. (st.symtabletype=globalsymtable)
  938. ) or
  939. (ds_rtti_table_used in def.defstates) then
  940. RTTIWriter.write_rtti(def,fullrtti);
  941. end;
  942. end;
  943. end.