ptype.pas 41 KB

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