2
0

ptype.pas 43 KB

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