ptype.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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,
  46. systems,
  47. { target }
  48. paramgr,
  49. { symtable }
  50. symconst,symsym,symtable,
  51. defutil,defcmp,
  52. { pass 1 }
  53. node,ncgrtti,nobj,
  54. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  55. { parser }
  56. scanner,
  57. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  58. procedure generate_specialization(var pt1:tnode;const name:string);
  59. var
  60. st : TSymtable;
  61. pt2 : tnode;
  62. first,
  63. err : boolean;
  64. i : longint;
  65. sym : tsym;
  66. genericdef : tstoreddef;
  67. generictype : ttypesym;
  68. generictypelist : TFPObjectList;
  69. begin
  70. { retrieve generic def that we are going to replace }
  71. genericdef:=tstoreddef(pt1.resultdef);
  72. pt1.resultdef:=nil;
  73. if not(df_generic in genericdef.defoptions) then
  74. begin
  75. Message(parser_e_special_onlygenerics);
  76. pt1.resultdef:=generrordef;
  77. { recover }
  78. consume(_LSHARPBRACKET);
  79. repeat
  80. pt2:=factor(false);
  81. pt2.free;
  82. until not try_to_consume(_COMMA);
  83. consume(_RSHARPBRACKET);
  84. exit;
  85. end;
  86. consume(_LSHARPBRACKET);
  87. block_type:=bt_specialize;
  88. { Parse generic parameters, for each undefineddef in the symtable of
  89. the genericdef we need to have a new def }
  90. err:=false;
  91. first:=true;
  92. generictypelist:=TFPObjectList.create(false);
  93. case genericdef.typ of
  94. procdef :
  95. st:=genericdef.GetSymtable(gs_para);
  96. objectdef,
  97. recorddef :
  98. st:=genericdef.GetSymtable(gs_record);
  99. end;
  100. if not assigned(st) then
  101. internalerror(200511182);
  102. for i:=0 to st.SymList.Count-1 do
  103. begin
  104. sym:=tsym(st.SymList[i]);
  105. if (sym.typ=typesym) and
  106. (ttypesym(sym).typedef.typ=undefineddef) then
  107. begin
  108. if not first then
  109. consume(_COMMA)
  110. else
  111. first:=false;
  112. pt2:=factor(false);
  113. if pt2.nodetype=typen then
  114. begin
  115. if df_generic in pt2.resultdef.defoptions then
  116. Message(parser_e_no_generics_as_params);
  117. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  118. generictypelist.add(generictype);
  119. end
  120. else
  121. begin
  122. Message(type_e_type_id_expected);
  123. err:=true;
  124. end;
  125. pt2.free;
  126. end;
  127. end;
  128. { Reparse the original type definition }
  129. if not err then
  130. begin
  131. if not assigned(genericdef.generictokenbuf) then
  132. internalerror(200511171);
  133. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  134. read_named_type(pt1.resultdef,name,genericdef,generictypelist,false);
  135. { Consume the semicolon if it is also recorded }
  136. try_to_consume(_SEMICOLON);
  137. end;
  138. generictypelist.free;
  139. consume(_RSHARPBRACKET);
  140. end;
  141. procedure id_type(var def : tdef;isforwarddef:boolean);
  142. { reads a type definition }
  143. { to a appropriating tdef, s gets the name of }
  144. { the type to allow name mangling }
  145. var
  146. is_unit_specific : boolean;
  147. pos : tfileposinfo;
  148. srsym : tsym;
  149. srsymtable : TSymtable;
  150. s,sorg : TIDString;
  151. begin
  152. s:=pattern;
  153. sorg:=orgpattern;
  154. pos:=current_tokenpos;
  155. { use of current parsed object:
  156. - classes can be used also in classes
  157. - objects can be parameters }
  158. if assigned(aktobjectdef) and
  159. (aktobjectdef.objname^=pattern) and
  160. (
  161. (testcurobject=2) or
  162. is_class_or_interface(aktobjectdef)
  163. )then
  164. begin
  165. consume(_ID);
  166. def:=aktobjectdef;
  167. exit;
  168. end;
  169. { Use the special searchsym_type that ignores records,objects and
  170. parameters }
  171. searchsym_type(s,srsym,srsymtable);
  172. { handle unit specification like System.Writeln }
  173. is_unit_specific:=try_consume_unitsym(srsym,srsymtable);
  174. consume(_ID);
  175. { Types are first defined with an error def before assigning
  176. the real type so check if it's an errordef. if so then
  177. give an error. Only check for typesyms in the current symbol
  178. table as forwarddef are not resolved directly }
  179. if assigned(srsym) and
  180. (srsym.typ=typesym) and
  181. (ttypesym(srsym).typedef.typ=errordef) then
  182. begin
  183. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  184. def:=generrordef;
  185. exit;
  186. end;
  187. { are we parsing a possible forward def ? }
  188. if isforwarddef and
  189. not(is_unit_specific) then
  190. begin
  191. def:=tforwarddef.create(s,pos);
  192. exit;
  193. end;
  194. { unknown sym ? }
  195. if not assigned(srsym) then
  196. begin
  197. Message1(sym_e_id_not_found,sorg);
  198. def:=generrordef;
  199. exit;
  200. end;
  201. { type sym ? }
  202. if (srsym.typ<>typesym) then
  203. begin
  204. Message(type_e_type_id_expected);
  205. def:=generrordef;
  206. exit;
  207. end;
  208. { Give an error when referring to an errordef }
  209. if (ttypesym(srsym).typedef.typ=errordef) then
  210. begin
  211. Message(sym_e_error_in_type_def);
  212. def:=generrordef;
  213. exit;
  214. end;
  215. def:=ttypesym(srsym).typedef;
  216. end;
  217. procedure single_type(var def:tdef;isforwarddef:boolean);
  218. var
  219. t2 : tdef;
  220. again : boolean;
  221. begin
  222. repeat
  223. again:=false;
  224. case token of
  225. _STRING:
  226. string_dec(def);
  227. _FILE:
  228. begin
  229. consume(_FILE);
  230. if try_to_consume(_OF) then
  231. begin
  232. single_type(t2,false);
  233. def:=tfiledef.createtyped(t2);
  234. end
  235. else
  236. def:=cfiletype;
  237. end;
  238. _ID:
  239. begin
  240. if try_to_consume(_SPECIALIZE) then
  241. begin
  242. if block_type<>bt_type then
  243. Message(parser_f_no_anonymous_specializations);
  244. block_type:=bt_specialize;
  245. again:=true;
  246. end
  247. else
  248. id_type(def,isforwarddef);
  249. end;
  250. else
  251. begin
  252. message(type_e_type_id_expected);
  253. def:=generrordef;
  254. end;
  255. end;
  256. until not again;
  257. end;
  258. { reads a record declaration }
  259. function record_dec : tdef;
  260. var
  261. recst : trecordsymtable;
  262. storetypecanbeforward : boolean;
  263. old_object_option : tsymoptions;
  264. begin
  265. { create recdef }
  266. recst:=trecordsymtable.create(current_settings.packrecords);
  267. record_dec:=trecorddef.create(recst);
  268. { insert in symtablestack }
  269. symtablestack.push(recst);
  270. { parse record }
  271. consume(_RECORD);
  272. old_object_option:=current_object_option;
  273. current_object_option:=[sp_public];
  274. storetypecanbeforward:=typecanbeforward;
  275. { for tp7 don't allow forward types }
  276. if m_tp7 in current_settings.modeswitches then
  277. typecanbeforward:=false;
  278. read_record_fields([vd_record]);
  279. consume(_END);
  280. typecanbeforward:=storetypecanbeforward;
  281. current_object_option:=old_object_option;
  282. { make the record size aligned }
  283. recst.addalignmentpadding;
  284. { restore symtable stack }
  285. symtablestack.pop(recst);
  286. if trecorddef(record_dec).is_packed and
  287. record_dec.needs_inittable then
  288. Message(type_e_no_packed_inittable);
  289. end;
  290. { reads a type definition and returns a pointer to it }
  291. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  292. var
  293. pt : tnode;
  294. tt2 : tdef;
  295. aktenumdef : tenumdef;
  296. s : TIDString;
  297. l,v : TConstExprInt;
  298. oldpackrecords : longint;
  299. defpos,storepos : tfileposinfo;
  300. procedure expr_type;
  301. var
  302. pt1,pt2 : tnode;
  303. lv,hv : TConstExprInt;
  304. old_block_type : tblock_type;
  305. begin
  306. old_block_type:=block_type;
  307. { use of current parsed object:
  308. - classes can be used also in classes
  309. - objects can be parameters }
  310. if (token=_ID) and
  311. assigned(aktobjectdef) and
  312. (aktobjectdef.objname^=pattern) and
  313. (
  314. (testcurobject=2) or
  315. is_class_or_interface(aktobjectdef)
  316. )then
  317. begin
  318. consume(_ID);
  319. def:=aktobjectdef;
  320. exit;
  321. end;
  322. { Generate a specialization? }
  323. if try_to_consume(_SPECIALIZE) then
  324. begin
  325. if name='' then
  326. Message(parser_f_no_anonymous_specializations);
  327. block_type:=bt_specialize;
  328. end;
  329. { we can't accept a equal in type }
  330. pt1:=comp_expr(false);
  331. if (block_type<>bt_specialize) and
  332. try_to_consume(_POINTPOINT) then
  333. begin
  334. { get high value of range }
  335. pt2:=comp_expr(false);
  336. { make both the same type or give an error. This is not
  337. done when both are integer values, because typecasting
  338. between -3200..3200 will result in a signed-unsigned
  339. conflict and give a range check error (PFV) }
  340. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  341. inserttypeconv(pt1,pt2.resultdef);
  342. { both must be evaluated to constants now }
  343. if (pt1.nodetype=ordconstn) and
  344. (pt2.nodetype=ordconstn) then
  345. begin
  346. lv:=tordconstnode(pt1).value;
  347. hv:=tordconstnode(pt2).value;
  348. { Check bounds }
  349. if hv<lv then
  350. Message(parser_e_upper_lower_than_lower)
  351. else
  352. begin
  353. { All checks passed, create the new def }
  354. case pt1.resultdef.typ of
  355. enumdef :
  356. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv,hv);
  357. orddef :
  358. begin
  359. if is_char(pt1.resultdef) then
  360. def:=torddef.create(uchar,lv,hv)
  361. else
  362. if is_boolean(pt1.resultdef) then
  363. def:=torddef.create(bool8bit,lv,hv)
  364. else
  365. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  366. end;
  367. end;
  368. end;
  369. end
  370. else
  371. Message(sym_e_error_in_type_def);
  372. pt2.free;
  373. end
  374. else
  375. begin
  376. { a simple type renaming or generic specialization }
  377. if (pt1.nodetype=typen) then
  378. begin
  379. if (block_type=bt_specialize) then
  380. generate_specialization(pt1,name);
  381. def:=ttypenode(pt1).resultdef;
  382. end
  383. else
  384. Message(sym_e_error_in_type_def);
  385. end;
  386. pt1.free;
  387. block_type:=old_block_type;
  388. end;
  389. procedure set_dec;
  390. begin
  391. consume(_SET);
  392. consume(_OF);
  393. read_anon_type(tt2,true);
  394. if assigned(tt2) then
  395. begin
  396. case tt2.typ of
  397. { don't forget that min can be negativ PM }
  398. enumdef :
  399. if tenumdef(tt2).min>=0 then
  400. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  401. def:=tsetdef.create(tt2,tenumdef(tt2).max)
  402. else
  403. Message(sym_e_ill_type_decl_set);
  404. orddef :
  405. begin
  406. if (torddef(tt2).ordtype<>uvoid) and
  407. (torddef(tt2).low>=0) then
  408. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  409. def:=tsetdef.create(tt2,torddef(tt2).high)
  410. else
  411. Message(sym_e_ill_type_decl_set);
  412. end;
  413. else
  414. Message(sym_e_ill_type_decl_set);
  415. end;
  416. end
  417. else
  418. def:=generrordef;
  419. end;
  420. procedure array_dec(is_packed: boolean);
  421. var
  422. lowval,
  423. highval : TConstExprInt;
  424. indexdef : tdef;
  425. hdef : tdef;
  426. arrdef : tarraydef;
  427. procedure setdefdecl(def:tdef);
  428. begin
  429. case def.typ of
  430. enumdef :
  431. begin
  432. lowval:=tenumdef(def).min;
  433. highval:=tenumdef(def).max;
  434. if (m_fpc in current_settings.modeswitches) and
  435. (tenumdef(def).has_jumps) then
  436. Message(type_e_array_index_enums_with_assign_not_possible);
  437. indexdef:=def;
  438. end;
  439. orddef :
  440. begin
  441. if torddef(def).ordtype in [uchar,
  442. u8bit,u16bit,
  443. s8bit,s16bit,s32bit,
  444. {$ifdef cpu64bit}
  445. u32bit,s64bit,
  446. {$endif cpu64bit}
  447. bool8bit,bool16bit,bool32bit,bool64bit,
  448. uwidechar] then
  449. begin
  450. lowval:=torddef(def).low;
  451. highval:=torddef(def).high;
  452. indexdef:=def;
  453. end
  454. else
  455. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  456. end;
  457. else
  458. Message(sym_e_error_in_type_def);
  459. end;
  460. end;
  461. begin
  462. arrdef:=nil;
  463. consume(_ARRAY);
  464. { open array? }
  465. if try_to_consume(_LECKKLAMMER) then
  466. begin
  467. { defaults }
  468. indexdef:=generrordef;
  469. lowval:=low(aint);
  470. highval:=high(aint);
  471. repeat
  472. { read the expression and check it, check apart if the
  473. declaration is an enum declaration because that needs to
  474. be parsed by readtype (PFV) }
  475. if token=_LKLAMMER then
  476. begin
  477. read_anon_type(hdef,true);
  478. setdefdecl(hdef);
  479. end
  480. else
  481. begin
  482. pt:=expr;
  483. if pt.nodetype=typen then
  484. setdefdecl(pt.resultdef)
  485. else
  486. begin
  487. if (pt.nodetype=rangen) then
  488. begin
  489. if (trangenode(pt).left.nodetype=ordconstn) and
  490. (trangenode(pt).right.nodetype=ordconstn) then
  491. begin
  492. { make both the same type or give an error. This is not
  493. done when both are integer values, because typecasting
  494. between -3200..3200 will result in a signed-unsigned
  495. conflict and give a range check error (PFV) }
  496. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  497. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  498. lowval:=tordconstnode(trangenode(pt).left).value;
  499. highval:=tordconstnode(trangenode(pt).right).value;
  500. if highval<lowval then
  501. begin
  502. Message(parser_e_array_lower_less_than_upper_bound);
  503. highval:=lowval;
  504. end
  505. else if (lowval < low(aint)) or
  506. (highval > high(aint)) then
  507. begin
  508. Message(parser_e_array_range_out_of_bounds);
  509. lowval :=0;
  510. highval:=0;
  511. end;
  512. if is_integer(trangenode(pt).left.resultdef) then
  513. range_to_type(lowval,highval,indexdef)
  514. else
  515. indexdef:=trangenode(pt).left.resultdef;
  516. end
  517. else
  518. Message(type_e_cant_eval_constant_expr);
  519. end
  520. else
  521. Message(sym_e_error_in_type_def)
  522. end;
  523. pt.free;
  524. end;
  525. { if the array is already created add the new arrray
  526. as element of the existing array, otherwise create a new array }
  527. if assigned(arrdef) then
  528. begin
  529. arrdef.elementdef:=tarraydef.create(lowval,highval,indexdef);
  530. arrdef:=tarraydef(arrdef.elementdef);
  531. end
  532. else
  533. begin
  534. arrdef:=tarraydef.create(lowval,highval,indexdef);
  535. def:=arrdef;
  536. end;
  537. if is_packed then
  538. include(arrdef.arrayoptions,ado_IsBitPacked);
  539. if token=_COMMA then
  540. consume(_COMMA)
  541. else
  542. break;
  543. until false;
  544. consume(_RECKKLAMMER);
  545. end
  546. else
  547. begin
  548. if is_packed then
  549. Message(parser_e_packed_dynamic_open_array);
  550. arrdef:=tarraydef.create(0,-1,s32inttype);
  551. include(arrdef.arrayoptions,ado_IsDynamicArray);
  552. def:=arrdef;
  553. end;
  554. consume(_OF);
  555. read_anon_type(tt2,true);
  556. { set element type of the last array definition }
  557. if assigned(arrdef) then
  558. begin
  559. arrdef.elementdef:=tt2;
  560. if is_packed and
  561. tt2.needs_inittable then
  562. Message(type_e_no_packed_inittable);
  563. end;
  564. end;
  565. var
  566. p : tnode;
  567. pd : tabstractprocdef;
  568. is_func,
  569. enumdupmsg, first : boolean;
  570. newtype : ttypesym;
  571. oldlocalswitches : tlocalswitches;
  572. bitpacking: boolean;
  573. begin
  574. def:=nil;
  575. case token of
  576. _STRING,_FILE:
  577. begin
  578. single_type(def,false);
  579. end;
  580. _LKLAMMER:
  581. begin
  582. consume(_LKLAMMER);
  583. first := true;
  584. { allow negativ value_str }
  585. l:=-1;
  586. enumdupmsg:=false;
  587. aktenumdef:=tenumdef.create;
  588. repeat
  589. s:=orgpattern;
  590. defpos:=current_tokenpos;
  591. consume(_ID);
  592. { only allow assigning of specific numbers under fpc mode }
  593. if not(m_tp7 in current_settings.modeswitches) and
  594. (
  595. { in fpc mode also allow := to be compatible
  596. with previous 1.0.x versions }
  597. ((m_fpc in current_settings.modeswitches) and
  598. try_to_consume(_ASSIGNMENT)) or
  599. try_to_consume(_EQUAL)
  600. ) then
  601. begin
  602. oldlocalswitches:=current_settings.localswitches;
  603. include(current_settings.localswitches,cs_allow_enum_calc);
  604. p:=comp_expr(true);
  605. current_settings.localswitches:=oldlocalswitches;
  606. if (p.nodetype=ordconstn) then
  607. begin
  608. { we expect an integer or an enum of the
  609. same type }
  610. if is_integer(p.resultdef) or
  611. is_char(p.resultdef) or
  612. equal_defs(p.resultdef,aktenumdef) then
  613. v:=tordconstnode(p).value
  614. else
  615. IncompatibleTypes(p.resultdef,s32inttype);
  616. end
  617. else
  618. Message(parser_e_illegal_expression);
  619. p.free;
  620. { please leave that a note, allows type save }
  621. { declarations in the win32 units ! }
  622. if (not first) and (v<=l) and (not enumdupmsg) then
  623. begin
  624. Message(parser_n_duplicate_enum);
  625. enumdupmsg:=true;
  626. end;
  627. l:=v;
  628. end
  629. else
  630. inc(l);
  631. first := false;
  632. storepos:=current_tokenpos;
  633. current_tokenpos:=defpos;
  634. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,l));
  635. current_tokenpos:=storepos;
  636. until not try_to_consume(_COMMA);
  637. def:=aktenumdef;
  638. consume(_RKLAMMER);
  639. end;
  640. _ARRAY:
  641. begin
  642. array_dec(false);
  643. end;
  644. _SET:
  645. begin
  646. set_dec;
  647. end;
  648. _CARET:
  649. begin
  650. consume(_CARET);
  651. single_type(tt2,typecanbeforward);
  652. def:=tpointerdef.create(tt2);
  653. end;
  654. _RECORD:
  655. begin
  656. def:=record_dec;
  657. end;
  658. _PACKED,
  659. _BITPACKED:
  660. begin
  661. bitpacking :=
  662. (cs_bitpacking in current_settings.localswitches) or
  663. (token = _BITPACKED);
  664. consume(token);
  665. if token=_ARRAY then
  666. array_dec(bitpacking)
  667. else if token=_SET then
  668. set_dec
  669. else
  670. begin
  671. oldpackrecords:=current_settings.packrecords;
  672. if (not bitpacking) or
  673. (token in [_CLASS,_OBJECT]) then
  674. current_settings.packrecords:=1
  675. else
  676. current_settings.packrecords:=bit_alignment;
  677. if token in [_CLASS,_OBJECT] then
  678. def:=object_dec(name,genericdef,genericlist,nil)
  679. else
  680. def:=record_dec;
  681. current_settings.packrecords:=oldpackrecords;
  682. end;
  683. end;
  684. _DISPINTERFACE,
  685. _CLASS,
  686. _CPPCLASS,
  687. _INTERFACE,
  688. _OBJECT:
  689. begin
  690. def:=object_dec(name,genericdef,genericlist,nil);
  691. end;
  692. _PROCEDURE,
  693. _FUNCTION:
  694. begin
  695. is_func:=(token=_FUNCTION);
  696. consume(token);
  697. pd:=tprocvardef.create(normal_function_level);
  698. if token=_LKLAMMER then
  699. parse_parameter_dec(pd);
  700. if is_func then
  701. begin
  702. consume(_COLON);
  703. single_type(pd.returndef,false);
  704. end;
  705. if token=_OF then
  706. begin
  707. consume(_OF);
  708. consume(_OBJECT);
  709. include(pd.procoptions,po_methodpointer);
  710. end;
  711. def:=pd;
  712. { possible proc directives }
  713. if parseprocvardir then
  714. begin
  715. if check_proc_directive(true) then
  716. begin
  717. newtype:=ttypesym.create('unnamed',def);
  718. parse_var_proc_directives(tsym(newtype));
  719. newtype.typedef:=nil;
  720. def.typesym:=nil;
  721. newtype.free;
  722. end;
  723. { Add implicit hidden parameters and function result }
  724. handle_calling_convention(pd);
  725. end;
  726. end;
  727. else
  728. expr_type;
  729. end;
  730. if def=nil then
  731. def:=generrordef;
  732. end;
  733. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  734. begin
  735. read_named_type(def,'',nil,nil,parseprocvardir);
  736. end;
  737. procedure write_persistent_type_info(st:tsymtable);
  738. var
  739. i : longint;
  740. def : tdef;
  741. vmtwriter : TVMTWriter;
  742. begin
  743. for i:=0 to st.DefList.Count-1 do
  744. begin
  745. def:=tdef(st.DefList[i]);
  746. case def.typ of
  747. recorddef :
  748. write_persistent_type_info(trecorddef(def).symtable);
  749. objectdef :
  750. begin
  751. write_persistent_type_info(tobjectdef(def).symtable);
  752. { Write also VMT }
  753. if not(ds_vmt_written in def.defstates) and
  754. not(oo_is_forward in tobjectdef(def).objectoptions) then
  755. begin
  756. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  757. if is_interface(tobjectdef(def)) then
  758. vmtwriter.writeinterfaceids;
  759. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  760. vmtwriter.writevmt;
  761. vmtwriter.free;
  762. include(def.defstates,ds_vmt_written);
  763. end;
  764. end;
  765. procdef :
  766. begin
  767. if assigned(tprocdef(def).localst) and
  768. (tprocdef(def).localst.symtabletype=localsymtable) then
  769. write_persistent_type_info(tprocdef(def).localst);
  770. if assigned(tprocdef(def).parast) then
  771. write_persistent_type_info(tprocdef(def).parast);
  772. end;
  773. end;
  774. { generate always persistent tables for types in the interface so it can
  775. be reused in other units and give always the same pointer location. }
  776. { Init }
  777. if (
  778. assigned(def.typesym) and
  779. (st.symtabletype=globalsymtable)
  780. ) or
  781. def.needs_inittable or
  782. (ds_init_table_used in def.defstates) then
  783. RTTIWriter.write_rtti(def,initrtti);
  784. { RTTI }
  785. if (
  786. assigned(def.typesym) and
  787. (st.symtabletype=globalsymtable)
  788. ) or
  789. (ds_rtti_table_used in def.defstates) then
  790. RTTIWriter.write_rtti(def,fullrtti);
  791. end;
  792. end;
  793. end.