ptype.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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: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(tsym(srsym).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: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);
  386. _FILE:
  387. begin
  388. consume(_FILE);
  389. if try_to_consume(_OF) then
  390. begin
  391. single_type(t2,false);
  392. def:=tfiledef.createtyped(t2);
  393. end
  394. else
  395. def:=cfiletype;
  396. end;
  397. _ID:
  398. begin
  399. if try_to_consume(_SPECIALIZE) then
  400. begin
  401. dospecialize:=true;
  402. again:=true;
  403. end
  404. else
  405. begin
  406. id_type(def,isforwarddef);
  407. { handle types inside classes for generics, e.g. TNode.TLongint }
  408. while (token=_POINT) do
  409. begin
  410. if parse_generic then
  411. begin
  412. consume(_POINT);
  413. consume(_ID);
  414. end
  415. else if ((def.typ=objectdef) and (df_specialization in def.defoptions)) then
  416. begin
  417. symtablestack.push(tobjectdef(def).symtable);
  418. consume(_POINT);
  419. id_type(t2,isforwarddef);
  420. symtablestack.pop(tobjectdef(def).symtable);
  421. def:=t2;
  422. end
  423. else
  424. break;
  425. end;
  426. end;
  427. end;
  428. else
  429. begin
  430. message(type_e_type_id_expected);
  431. def:=generrordef;
  432. end;
  433. end;
  434. until not again;
  435. if dospecialize then
  436. generate_specialization(def)
  437. else
  438. begin
  439. if (df_generic in def.defoptions) then
  440. begin
  441. Message(parser_e_no_generics_as_types);
  442. def:=generrordef;
  443. end;
  444. end;
  445. end;
  446. { reads a record declaration }
  447. function record_dec : tdef;
  448. var
  449. recst : trecordsymtable;
  450. begin
  451. { create recdef }
  452. recst:=trecordsymtable.create(current_settings.packrecords);
  453. record_dec:=trecorddef.create(recst);
  454. { insert in symtablestack }
  455. symtablestack.push(recst);
  456. { parse record }
  457. consume(_RECORD);
  458. read_record_fields([vd_record]);
  459. consume(_END);
  460. { make the record size aligned }
  461. recst.addalignmentpadding;
  462. { restore symtable stack }
  463. symtablestack.pop(recst);
  464. if trecorddef(record_dec).is_packed and
  465. record_dec.needs_inittable then
  466. Message(type_e_no_packed_inittable);
  467. end;
  468. { reads a type definition and returns a pointer to it }
  469. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  470. var
  471. pt : tnode;
  472. tt2 : tdef;
  473. aktenumdef : tenumdef;
  474. s : TIDString;
  475. l,v : TConstExprInt;
  476. oldpackrecords : longint;
  477. defpos,storepos : tfileposinfo;
  478. procedure expr_type;
  479. var
  480. pt1,pt2 : tnode;
  481. lv,hv : TConstExprInt;
  482. old_block_type : tblock_type;
  483. dospecialize : boolean;
  484. begin
  485. old_block_type:=block_type;
  486. dospecialize:=false;
  487. { use of current parsed object:
  488. - classes can be used also in classes
  489. - objects can be parameters }
  490. if (token=_ID) and
  491. assigned(current_objectdef) and
  492. (current_objectdef.objname^=pattern) and
  493. (
  494. (testcurobject=2) or
  495. is_class_or_interface(current_objectdef)
  496. )then
  497. begin
  498. consume(_ID);
  499. def:=current_objectdef;
  500. exit;
  501. end;
  502. { Generate a specialization? }
  503. if try_to_consume(_SPECIALIZE) then
  504. dospecialize:=true;
  505. { we can't accept a equal in type }
  506. pt1:=comp_expr(false);
  507. if not dospecialize and
  508. try_to_consume(_POINTPOINT) then
  509. begin
  510. { get high value of range }
  511. pt2:=comp_expr(false);
  512. { make both the same type or give an error. This is not
  513. done when both are integer values, because typecasting
  514. between -3200..3200 will result in a signed-unsigned
  515. conflict and give a range check error (PFV) }
  516. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  517. inserttypeconv(pt1,pt2.resultdef);
  518. { both must be evaluated to constants now }
  519. if (pt1.nodetype=ordconstn) and
  520. (pt2.nodetype=ordconstn) then
  521. begin
  522. lv:=tordconstnode(pt1).value;
  523. hv:=tordconstnode(pt2).value;
  524. { Check bounds }
  525. if hv<lv then
  526. message(parser_e_upper_lower_than_lower)
  527. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  528. message(type_e_cant_eval_constant_expr)
  529. else
  530. begin
  531. { All checks passed, create the new def }
  532. case pt1.resultdef.typ of
  533. enumdef :
  534. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  535. orddef :
  536. begin
  537. if is_char(pt1.resultdef) then
  538. def:=torddef.create(uchar,lv,hv)
  539. else
  540. if is_boolean(pt1.resultdef) then
  541. def:=torddef.create(pasbool,lv,hv)
  542. else if is_signed(pt1.resultdef) then
  543. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  544. else
  545. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  546. end;
  547. end;
  548. end;
  549. end
  550. else
  551. Message(sym_e_error_in_type_def);
  552. pt2.free;
  553. end
  554. else
  555. begin
  556. { a simple type renaming or generic specialization }
  557. if (pt1.nodetype=typen) then
  558. begin
  559. def:=ttypenode(pt1).resultdef;
  560. if dospecialize then
  561. generate_specialization(def)
  562. else
  563. begin
  564. if (df_generic in def.defoptions) then
  565. begin
  566. Message(parser_e_no_generics_as_types);
  567. def:=generrordef;
  568. end;
  569. end;
  570. end
  571. else
  572. Message(sym_e_error_in_type_def);
  573. end;
  574. pt1.free;
  575. block_type:=old_block_type;
  576. end;
  577. procedure set_dec;
  578. begin
  579. consume(_SET);
  580. consume(_OF);
  581. read_anon_type(tt2,true);
  582. if assigned(tt2) then
  583. begin
  584. case tt2.typ of
  585. { don't forget that min can be negativ PM }
  586. enumdef :
  587. if (tenumdef(tt2).min>=0) and
  588. (tenumdef(tt2).max<=255) then
  589. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  590. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  591. else
  592. Message(sym_e_ill_type_decl_set);
  593. orddef :
  594. begin
  595. if (torddef(tt2).ordtype<>uvoid) and
  596. (torddef(tt2).ordtype<>uwidechar) and
  597. (torddef(tt2).low>=0) then
  598. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  599. if Torddef(tt2).high>int64(high(byte)) then
  600. message(sym_e_ill_type_decl_set)
  601. else
  602. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  603. else
  604. Message(sym_e_ill_type_decl_set);
  605. end;
  606. else
  607. Message(sym_e_ill_type_decl_set);
  608. end;
  609. end
  610. else
  611. def:=generrordef;
  612. end;
  613. procedure array_dec(is_packed: boolean);
  614. var
  615. lowval,
  616. highval : TConstExprInt;
  617. indexdef : tdef;
  618. hdef : tdef;
  619. arrdef : tarraydef;
  620. procedure setdefdecl(def:tdef);
  621. begin
  622. case def.typ of
  623. enumdef :
  624. begin
  625. lowval:=tenumdef(def).min;
  626. highval:=tenumdef(def).max;
  627. if (m_fpc in current_settings.modeswitches) and
  628. (tenumdef(def).has_jumps) then
  629. Message(type_e_array_index_enums_with_assign_not_possible);
  630. indexdef:=def;
  631. end;
  632. orddef :
  633. begin
  634. if torddef(def).ordtype in [uchar,
  635. u8bit,u16bit,
  636. s8bit,s16bit,s32bit,
  637. {$ifdef cpu64bitaddr}
  638. u32bit,s64bit,
  639. {$endif cpu64bitaddr}
  640. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  641. uwidechar] then
  642. begin
  643. lowval:=torddef(def).low;
  644. highval:=torddef(def).high;
  645. indexdef:=def;
  646. end
  647. else
  648. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  649. end;
  650. else
  651. Message(sym_e_error_in_type_def);
  652. end;
  653. end;
  654. begin
  655. arrdef:=nil;
  656. consume(_ARRAY);
  657. { open array? }
  658. if try_to_consume(_LECKKLAMMER) then
  659. begin
  660. { defaults }
  661. indexdef:=generrordef;
  662. { use defaults which don't overflow the compiler }
  663. lowval:=0;
  664. highval:=0;
  665. repeat
  666. { read the expression and check it, check apart if the
  667. declaration is an enum declaration because that needs to
  668. be parsed by readtype (PFV) }
  669. if token=_LKLAMMER then
  670. begin
  671. read_anon_type(hdef,true);
  672. setdefdecl(hdef);
  673. end
  674. else
  675. begin
  676. pt:=expr;
  677. if pt.nodetype=typen then
  678. setdefdecl(pt.resultdef)
  679. else
  680. begin
  681. if (pt.nodetype=rangen) then
  682. begin
  683. if (trangenode(pt).left.nodetype=ordconstn) and
  684. (trangenode(pt).right.nodetype=ordconstn) then
  685. begin
  686. { make both the same type or give an error. This is not
  687. done when both are integer values, because typecasting
  688. between -3200..3200 will result in a signed-unsigned
  689. conflict and give a range check error (PFV) }
  690. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  691. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  692. lowval:=tordconstnode(trangenode(pt).left).value;
  693. highval:=tordconstnode(trangenode(pt).right).value;
  694. if highval<lowval then
  695. begin
  696. Message(parser_e_array_lower_less_than_upper_bound);
  697. highval:=lowval;
  698. end
  699. else if (lowval<int64(low(aint))) or
  700. (highval > high(aint)) then
  701. begin
  702. Message(parser_e_array_range_out_of_bounds);
  703. lowval :=0;
  704. highval:=0;
  705. end;
  706. if is_integer(trangenode(pt).left.resultdef) then
  707. range_to_type(lowval,highval,indexdef)
  708. else
  709. indexdef:=trangenode(pt).left.resultdef;
  710. end
  711. else
  712. Message(type_e_cant_eval_constant_expr);
  713. end
  714. else
  715. Message(sym_e_error_in_type_def)
  716. end;
  717. pt.free;
  718. end;
  719. { if the array is already created add the new arrray
  720. as element of the existing array, otherwise create a new array }
  721. if assigned(arrdef) then
  722. begin
  723. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  724. arrdef:=tarraydef(arrdef.elementdef);
  725. end
  726. else
  727. begin
  728. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  729. def:=arrdef;
  730. end;
  731. if is_packed then
  732. include(arrdef.arrayoptions,ado_IsBitPacked);
  733. if token=_COMMA then
  734. consume(_COMMA)
  735. else
  736. break;
  737. until false;
  738. consume(_RECKKLAMMER);
  739. end
  740. else
  741. begin
  742. if is_packed then
  743. Message(parser_e_packed_dynamic_open_array);
  744. arrdef:=tarraydef.create(0,-1,s32inttype);
  745. include(arrdef.arrayoptions,ado_IsDynamicArray);
  746. def:=arrdef;
  747. end;
  748. consume(_OF);
  749. read_anon_type(tt2,true);
  750. { set element type of the last array definition }
  751. if assigned(arrdef) then
  752. begin
  753. arrdef.elementdef:=tt2;
  754. if is_packed and
  755. tt2.needs_inittable then
  756. Message(type_e_no_packed_inittable);
  757. end;
  758. end;
  759. var
  760. p : tnode;
  761. hdef : tdef;
  762. pd : tabstractprocdef;
  763. is_func,
  764. enumdupmsg, first : boolean;
  765. newtype : ttypesym;
  766. oldlocalswitches : tlocalswitches;
  767. bitpacking: boolean;
  768. begin
  769. def:=nil;
  770. case token of
  771. _STRING,_FILE:
  772. begin
  773. single_type(def,false);
  774. end;
  775. _LKLAMMER:
  776. begin
  777. consume(_LKLAMMER);
  778. first := true;
  779. { allow negativ value_str }
  780. l:=int64(-1);
  781. enumdupmsg:=false;
  782. aktenumdef:=tenumdef.create;
  783. repeat
  784. s:=orgpattern;
  785. defpos:=current_tokenpos;
  786. consume(_ID);
  787. { only allow assigning of specific numbers under fpc mode }
  788. if not(m_tp7 in current_settings.modeswitches) and
  789. (
  790. { in fpc mode also allow := to be compatible
  791. with previous 1.0.x versions }
  792. ((m_fpc in current_settings.modeswitches) and
  793. try_to_consume(_ASSIGNMENT)) or
  794. try_to_consume(_EQUAL)
  795. ) then
  796. begin
  797. oldlocalswitches:=current_settings.localswitches;
  798. include(current_settings.localswitches,cs_allow_enum_calc);
  799. p:=comp_expr(true);
  800. current_settings.localswitches:=oldlocalswitches;
  801. if (p.nodetype=ordconstn) then
  802. begin
  803. { we expect an integer or an enum of the
  804. same type }
  805. if is_integer(p.resultdef) or
  806. is_char(p.resultdef) or
  807. equal_defs(p.resultdef,aktenumdef) then
  808. v:=tordconstnode(p).value
  809. else
  810. IncompatibleTypes(p.resultdef,s32inttype);
  811. end
  812. else
  813. Message(parser_e_illegal_expression);
  814. p.free;
  815. { please leave that a note, allows type save }
  816. { declarations in the win32 units ! }
  817. if (not first) and (v<=l) and (not enumdupmsg) then
  818. begin
  819. Message(parser_n_duplicate_enum);
  820. enumdupmsg:=true;
  821. end;
  822. l:=v;
  823. end
  824. else
  825. inc(l.svalue);
  826. first := false;
  827. storepos:=current_tokenpos;
  828. current_tokenpos:=defpos;
  829. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  830. current_tokenpos:=storepos;
  831. until not try_to_consume(_COMMA);
  832. def:=aktenumdef;
  833. consume(_RKLAMMER);
  834. end;
  835. _ARRAY:
  836. begin
  837. array_dec(false);
  838. end;
  839. _SET:
  840. begin
  841. set_dec;
  842. end;
  843. _CARET:
  844. begin
  845. consume(_CARET);
  846. single_type(tt2,(block_type=bt_type));
  847. def:=tpointerdef.create(tt2);
  848. if tt2.typ=forwarddef then
  849. current_module.checkforwarddefs.add(def);
  850. end;
  851. _RECORD:
  852. begin
  853. def:=record_dec;
  854. end;
  855. _PACKED,
  856. _BITPACKED:
  857. begin
  858. bitpacking :=
  859. (cs_bitpacking in current_settings.localswitches) or
  860. (token = _BITPACKED);
  861. consume(token);
  862. if token=_ARRAY then
  863. array_dec(bitpacking)
  864. else if token=_SET then
  865. set_dec
  866. else
  867. begin
  868. oldpackrecords:=current_settings.packrecords;
  869. if (not bitpacking) or
  870. (token in [_CLASS,_OBJECT]) then
  871. current_settings.packrecords:=1
  872. else
  873. current_settings.packrecords:=bit_alignment;
  874. case token of
  875. _CLASS :
  876. begin
  877. consume(_CLASS);
  878. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  879. end;
  880. _OBJECT :
  881. begin
  882. consume(_OBJECT);
  883. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  884. end;
  885. else
  886. def:=record_dec;
  887. end;
  888. current_settings.packrecords:=oldpackrecords;
  889. end;
  890. end;
  891. _DISPINTERFACE :
  892. begin
  893. { need extra check here since interface is a keyword
  894. in all pascal modes }
  895. if not(m_class in current_settings.modeswitches) then
  896. Message(parser_f_need_objfpc_or_delphi_mode);
  897. consume(token);
  898. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  899. end;
  900. _CLASS :
  901. begin
  902. consume(token);
  903. { Delphi only allows class of in type blocks }
  904. if (token=_OF) and
  905. (
  906. not(m_delphi in current_settings.modeswitches) or
  907. (block_type=bt_type)
  908. ) then
  909. begin
  910. consume(_OF);
  911. single_type(hdef,(block_type=bt_type));
  912. if is_class(hdef) then
  913. def:=tclassrefdef.create(hdef)
  914. else
  915. if hdef.typ=forwarddef then
  916. begin
  917. def:=tclassrefdef.create(hdef);
  918. current_module.checkforwarddefs.add(def);
  919. end
  920. else
  921. Message1(type_e_class_type_expected,hdef.typename);
  922. end
  923. else
  924. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  925. end;
  926. _CPPCLASS :
  927. begin
  928. consume(token);
  929. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  930. end;
  931. _INTERFACE :
  932. begin
  933. { need extra check here since interface is a keyword
  934. in all pascal modes }
  935. if not(m_class in current_settings.modeswitches) then
  936. Message(parser_f_need_objfpc_or_delphi_mode);
  937. consume(token);
  938. if current_settings.interfacetype=it_interfacecom then
  939. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  940. else {it_interfacecorba}
  941. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  942. end;
  943. _OBJECT :
  944. begin
  945. consume(token);
  946. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  947. end;
  948. _PROCEDURE,
  949. _FUNCTION:
  950. begin
  951. is_func:=(token=_FUNCTION);
  952. consume(token);
  953. pd:=tprocvardef.create(normal_function_level);
  954. if token=_LKLAMMER then
  955. parse_parameter_dec(pd);
  956. if is_func then
  957. begin
  958. consume(_COLON);
  959. single_type(pd.returndef,false);
  960. end;
  961. if token=_OF then
  962. begin
  963. consume(_OF);
  964. consume(_OBJECT);
  965. include(pd.procoptions,po_methodpointer);
  966. end;
  967. def:=pd;
  968. { possible proc directives }
  969. if parseprocvardir then
  970. begin
  971. if check_proc_directive(true) then
  972. begin
  973. newtype:=ttypesym.create('unnamed',def);
  974. parse_var_proc_directives(tsym(newtype));
  975. newtype.typedef:=nil;
  976. def.typesym:=nil;
  977. newtype.free;
  978. end;
  979. { Add implicit hidden parameters and function result }
  980. handle_calling_convention(pd);
  981. end;
  982. end;
  983. else
  984. expr_type;
  985. end;
  986. if def=nil then
  987. def:=generrordef;
  988. end;
  989. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  990. begin
  991. read_named_type(def,'',nil,nil,parseprocvardir);
  992. end;
  993. procedure write_persistent_type_info(st:tsymtable);
  994. var
  995. i : longint;
  996. def : tdef;
  997. vmtwriter : TVMTWriter;
  998. begin
  999. for i:=0 to st.DefList.Count-1 do
  1000. begin
  1001. def:=tdef(st.DefList[i]);
  1002. case def.typ of
  1003. recorddef :
  1004. write_persistent_type_info(trecorddef(def).symtable);
  1005. objectdef :
  1006. begin
  1007. { Skip generics and forward defs }
  1008. if (df_generic in def.defoptions) or
  1009. (oo_is_forward in tobjectdef(def).objectoptions) then
  1010. continue;
  1011. write_persistent_type_info(tobjectdef(def).symtable);
  1012. { Write also VMT if not done yet }
  1013. if not(ds_vmt_written in def.defstates) then
  1014. begin
  1015. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1016. if is_interface(tobjectdef(def)) then
  1017. vmtwriter.writeinterfaceids;
  1018. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1019. vmtwriter.writevmt;
  1020. vmtwriter.free;
  1021. include(def.defstates,ds_vmt_written);
  1022. end;
  1023. end;
  1024. procdef :
  1025. begin
  1026. if assigned(tprocdef(def).localst) and
  1027. (tprocdef(def).localst.symtabletype=localsymtable) then
  1028. write_persistent_type_info(tprocdef(def).localst);
  1029. if assigned(tprocdef(def).parast) then
  1030. write_persistent_type_info(tprocdef(def).parast);
  1031. end;
  1032. end;
  1033. { generate always persistent tables for types in the interface so it can
  1034. be reused in other units and give always the same pointer location. }
  1035. { Init }
  1036. if (
  1037. assigned(def.typesym) and
  1038. (st.symtabletype=globalsymtable)
  1039. ) or
  1040. def.needs_inittable or
  1041. (ds_init_table_used in def.defstates) then
  1042. RTTIWriter.write_rtti(def,initrtti);
  1043. { RTTI }
  1044. if (
  1045. assigned(def.typesym) and
  1046. (st.symtabletype=globalsymtable)
  1047. ) or
  1048. (ds_rtti_table_used in def.defstates) then
  1049. RTTIWriter.write_rtti(def,fullrtti);
  1050. end;
  1051. end;
  1052. end.