ptype.pas 34 KB

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