ptype.pas 41 KB

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