ptype.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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. procedure resolve_forward_types;
  24. { reads a type identifier }
  25. procedure id_type(var def : tdef;isforwarddef:boolean);
  26. { reads a string, file type or a type identifier }
  27. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  28. { reads any type declaration, where the resulting type will get name as type identifier }
  29. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  30. { reads any type declaration }
  31. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  32. { generate persistent type information like VMT, RTTI and inittables }
  33. procedure write_persistent_type_info(st:tsymtable);
  34. implementation
  35. uses
  36. { common }
  37. cutils,
  38. { global }
  39. globals,tokens,verbose,constexp,
  40. systems,
  41. { target }
  42. paramgr,procinfo,
  43. { symtable }
  44. symconst,symsym,symtable,
  45. defutil,defcmp,
  46. { modules }
  47. fmodule,
  48. { pass 1 }
  49. node,ncgrtti,nobj,
  50. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  51. { parser }
  52. scanner,
  53. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl;
  54. procedure resolve_forward_types;
  55. var
  56. i: longint;
  57. hpd,
  58. def : tdef;
  59. srsym : tsym;
  60. srsymtable : TSymtable;
  61. hs : string;
  62. begin
  63. for i:=0 to current_module.checkforwarddefs.Count-1 do
  64. begin
  65. def:=tdef(current_module.checkforwarddefs[i]);
  66. case def.typ of
  67. pointerdef,
  68. classrefdef :
  69. begin
  70. { classrefdef inherits from pointerdef }
  71. hpd:=tabstractpointerdef(def).pointeddef;
  72. { still a forward def ? }
  73. if hpd.typ=forwarddef then
  74. begin
  75. { try to resolve the forward }
  76. if not assigned(tforwarddef(hpd).tosymname) then
  77. internalerror(200211201);
  78. hs:=tforwarddef(hpd).tosymname^;
  79. searchsym(upper(hs),srsym,srsymtable);
  80. { we don't need the forwarddef anymore, dispose it }
  81. hpd.free;
  82. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  83. { was a type sym found ? }
  84. if assigned(srsym) and
  85. (srsym.typ=typesym) then
  86. begin
  87. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  88. { avoid wrong unused warnings web bug 801 PM }
  89. inc(ttypesym(srsym).refs);
  90. { we need a class type for classrefdef }
  91. if (def.typ=classrefdef) and
  92. not(is_class(ttypesym(srsym).typedef)) and
  93. not(is_objcclass(ttypesym(srsym).typedef)) then
  94. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  95. end
  96. else
  97. begin
  98. Message1(sym_e_forward_type_not_resolved,hs);
  99. { try to recover }
  100. tabstractpointerdef(def).pointeddef:=generrordef;
  101. end;
  102. end;
  103. end;
  104. objectdef :
  105. begin
  106. { give an error as the implementation may follow in an
  107. other type block which is allowed by FPC modes }
  108. if not(m_fpc in current_settings.modeswitches) and
  109. (oo_is_forward in tobjectdef(def).objectoptions) then
  110. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  111. end;
  112. else
  113. internalerror(200811071);
  114. end;
  115. end;
  116. current_module.checkforwarddefs.clear;
  117. end;
  118. procedure generate_specialization(var tt:tdef);
  119. var
  120. st : TSymtable;
  121. srsym : tsym;
  122. pt2 : tnode;
  123. first,
  124. err : boolean;
  125. i : longint;
  126. sym : tsym;
  127. genericdef : tstoreddef;
  128. generictype : ttypesym;
  129. generictypelist : TFPObjectList;
  130. oldsymtablestack : tsymtablestack;
  131. hmodule : tmodule;
  132. pu : tused_unit;
  133. uspecializename,
  134. specializename : string;
  135. vmtbuilder : TVMTBuilder;
  136. onlyparsepara : boolean;
  137. specializest : tsymtable;
  138. item: psymtablestackitem;
  139. begin
  140. { retrieve generic def that we are going to replace }
  141. genericdef:=tstoreddef(tt);
  142. tt:=nil;
  143. onlyparsepara:=false;
  144. if not(df_generic in genericdef.defoptions) then
  145. begin
  146. Message(parser_e_special_onlygenerics);
  147. tt:=generrordef;
  148. onlyparsepara:=true;
  149. end;
  150. { only need to record the tokens, then we don't know the type yet ... }
  151. if parse_generic then
  152. begin
  153. { ... but we have to insert a def into the symtable else the deflist
  154. of generic and specialization might not be equally sized which
  155. is later assumed }
  156. tt:=tundefineddef.create;
  157. onlyparsepara:=true;
  158. end;
  159. { Only parse the parameters for recovery or
  160. for recording in genericbuf }
  161. if onlyparsepara then
  162. begin
  163. consume(_LSHARPBRACKET);
  164. repeat
  165. pt2:=factor(false,true);
  166. pt2.free;
  167. until not try_to_consume(_COMMA);
  168. consume(_RSHARPBRACKET);
  169. exit;
  170. end;
  171. consume(_LSHARPBRACKET);
  172. { Parse generic parameters, for each undefineddef in the symtable of
  173. the genericdef we need to have a new def }
  174. err:=false;
  175. first:=true;
  176. generictypelist:=TFPObjectList.create(false);
  177. case genericdef.typ of
  178. procdef:
  179. st:=genericdef.GetSymtable(gs_para);
  180. objectdef,
  181. recorddef:
  182. st:=genericdef.GetSymtable(gs_record);
  183. arraydef:
  184. st:=tarraydef(genericdef).symtable;
  185. else
  186. internalerror(200511182);
  187. end;
  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,true);
  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. message(type_e_generics_cannot_reference_itself)
  210. else
  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_structdef) and
  227. (current_structdef.objname^=uspecializename) then
  228. tt:=current_structdef;
  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. item:=oldsymtablestack.stack;
  271. while assigned(item) and (item^.symtable.symtablelevel>main_program_level) do
  272. item:=item^.next;
  273. if assigned(item) and (item^.symtable<>symtablestack.top) then
  274. symtablestack.push(item^.symtable);
  275. { Reparse the original type definition }
  276. if not err then
  277. begin
  278. { First a new typesym so we can reuse this specialization and
  279. references to this specialization can be handled }
  280. srsym:=ttypesym.create(specializename,generrordef);
  281. specializest.insert(srsym);
  282. if not assigned(genericdef.generictokenbuf) then
  283. internalerror(200511171);
  284. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  285. read_named_type(tt,specializename,genericdef,generictypelist,false);
  286. ttypesym(srsym).typedef:=tt;
  287. tt.typesym:=srsym;
  288. { Consume the semicolon if it is also recorded }
  289. try_to_consume(_SEMICOLON);
  290. { Build VMT indexes for classes }
  291. if (tt.typ=objectdef) then
  292. begin
  293. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  294. vmtbuilder.generate_vmt;
  295. vmtbuilder.free;
  296. end;
  297. end;
  298. { Restore symtablestack }
  299. symtablestack.free;
  300. symtablestack:=oldsymtablestack;
  301. end
  302. else
  303. begin
  304. { There is comment few lines before ie 200512115
  305. saying "We are parsing the same objectdef, the def index numbers
  306. are the same". This is wrong (index numbers are not same)
  307. in case there is specialization (S2 in this case) inside
  308. specialized generic (G2 in this case) which is equal to
  309. some previous specialization (S1 in this case). In that case,
  310. new symbol is not added to currently specialized type
  311. (S in this case) for that specializations (S2 in this case),
  312. and this results in that specialization and generic definition
  313. don't have same number of elements in their object symbol tables.
  314. This patch adds undefined def to ensure that those
  315. two symbol tables will have same number of elements.
  316. }
  317. tundefineddef.create;
  318. end;
  319. generictypelist.free;
  320. consume(_RSHARPBRACKET);
  321. end;
  322. procedure id_type(var def : tdef;isforwarddef:boolean);
  323. { reads a type definition }
  324. { to a appropriating tdef, s gets the name of }
  325. { the type to allow name mangling }
  326. var
  327. is_unit_specific : boolean;
  328. pos : tfileposinfo;
  329. srsym : tsym;
  330. srsymtable : TSymtable;
  331. s,sorg : TIDString;
  332. t : ttoken;
  333. structdef : tabstractrecorddef;
  334. begin
  335. s:=pattern;
  336. sorg:=orgpattern;
  337. pos:=current_tokenpos;
  338. { use of current parsed object:
  339. - classes can be used also in classes
  340. - objects can be parameters }
  341. structdef:=current_structdef;
  342. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  343. begin
  344. if (structdef.objname^=pattern) then
  345. begin
  346. consume(_ID);
  347. def:=structdef;
  348. exit;
  349. end;
  350. structdef:=tabstractrecorddef(structdef.owner.defowner);
  351. end;
  352. { Use the special searchsym_type that ignores records and parameters }
  353. searchsym_type(s,srsym,srsymtable);
  354. { handle unit specification like System.Writeln }
  355. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  356. consume(t);
  357. { Types are first defined with an error def before assigning
  358. the real type so check if it's an errordef. if so then
  359. give an error. Only check for typesyms in the current symbol
  360. table as forwarddef are not resolved directly }
  361. if assigned(srsym) and
  362. (srsym.typ=typesym) and
  363. (ttypesym(srsym).typedef.typ=errordef) then
  364. begin
  365. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  366. def:=generrordef;
  367. exit;
  368. end;
  369. { are we parsing a possible forward def ? }
  370. if isforwarddef and
  371. not(is_unit_specific) then
  372. begin
  373. def:=tforwarddef.create(sorg,pos);
  374. exit;
  375. end;
  376. { unknown sym ? }
  377. if not assigned(srsym) then
  378. begin
  379. Message1(sym_e_id_not_found,sorg);
  380. def:=generrordef;
  381. exit;
  382. end;
  383. { type sym ? }
  384. if (srsym.typ<>typesym) then
  385. begin
  386. Message(type_e_type_id_expected);
  387. def:=generrordef;
  388. exit;
  389. end;
  390. { Give an error when referring to an errordef }
  391. if (ttypesym(srsym).typedef.typ=errordef) then
  392. begin
  393. Message(sym_e_error_in_type_def);
  394. def:=generrordef;
  395. exit;
  396. end;
  397. def:=ttypesym(srsym).typedef;
  398. end;
  399. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  400. var
  401. t2 : tdef;
  402. dospecialize,
  403. again : boolean;
  404. begin
  405. dospecialize:=false;
  406. repeat
  407. again:=false;
  408. case token of
  409. _STRING:
  410. string_dec(def,allowtypedef);
  411. _FILE:
  412. begin
  413. consume(_FILE);
  414. if (token=_OF) then
  415. begin
  416. if not(allowtypedef) then
  417. Message(parser_e_no_local_para_def);
  418. consume(_OF);
  419. single_type(t2,false,false);
  420. if is_managed_type(t2) then
  421. Message(parser_e_no_refcounted_typed_file);
  422. def:=tfiledef.createtyped(t2);
  423. end
  424. else
  425. def:=cfiletype;
  426. end;
  427. _ID:
  428. begin
  429. if try_to_consume(_SPECIALIZE) then
  430. begin
  431. if not(allowtypedef) then
  432. begin
  433. Message(parser_e_no_local_para_def);
  434. { try to recover }
  435. while token<>_SEMICOLON do
  436. consume(token);
  437. def:=generrordef;
  438. end
  439. else
  440. begin
  441. dospecialize:=true;
  442. again:=true;
  443. end;
  444. end
  445. else
  446. begin
  447. id_type(def,isforwarddef);
  448. { handle types inside classes, e.g. TNode.TLongint }
  449. while (token=_POINT) do
  450. begin
  451. if parse_generic then
  452. begin
  453. consume(_POINT);
  454. consume(_ID);
  455. end
  456. else if is_class_or_object(def) or is_record(def) then
  457. begin
  458. symtablestack.push(tabstractrecorddef(def).symtable);
  459. consume(_POINT);
  460. id_type(t2,isforwarddef);
  461. symtablestack.pop(tabstractrecorddef(def).symtable);
  462. def:=t2;
  463. end
  464. else
  465. break;
  466. end;
  467. end;
  468. end;
  469. else
  470. begin
  471. message(type_e_type_id_expected);
  472. def:=generrordef;
  473. end;
  474. end;
  475. until not again;
  476. if dospecialize then
  477. generate_specialization(def)
  478. else
  479. begin
  480. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  481. begin
  482. def:=current_specializedef
  483. end
  484. else if (def=current_genericdef) then
  485. begin
  486. def:=current_genericdef
  487. end
  488. else if (df_generic in def.defoptions) then
  489. begin
  490. Message(parser_e_no_generics_as_types);
  491. def:=generrordef;
  492. end
  493. else if is_objccategory(def) then
  494. begin
  495. Message(parser_e_no_category_as_types);
  496. def:=generrordef
  497. end
  498. end;
  499. end;
  500. procedure parse_record_members;
  501. procedure maybe_parse_hint_directives(pd:tprocdef);
  502. var
  503. dummysymoptions : tsymoptions;
  504. deprecatedmsg : pshortstring;
  505. begin
  506. dummysymoptions:=[];
  507. deprecatedmsg:=nil;
  508. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  509. Consume(_SEMICOLON);
  510. if assigned(pd) then
  511. begin
  512. pd.symoptions:=pd.symoptions+dummysymoptions;
  513. pd.deprecatedmsg:=deprecatedmsg;
  514. end
  515. else
  516. stringdispose(deprecatedmsg);
  517. end;
  518. var
  519. pd : tprocdef;
  520. oldparse_only: boolean;
  521. member_blocktype : tblock_type;
  522. fields_allowed, is_classdef, classfields: boolean;
  523. vdoptions: tvar_dec_options;
  524. begin
  525. { empty record declaration ? }
  526. if (token=_SEMICOLON) then
  527. Exit;
  528. current_structdef.symtable.currentvisibility:=vis_public;
  529. fields_allowed:=true;
  530. is_classdef:=false;
  531. classfields:=false;
  532. member_blocktype:=bt_general;
  533. repeat
  534. case token of
  535. _TYPE :
  536. begin
  537. consume(_TYPE);
  538. member_blocktype:=bt_type;
  539. end;
  540. _VAR :
  541. begin
  542. consume(_VAR);
  543. fields_allowed:=true;
  544. member_blocktype:=bt_general;
  545. classfields:=is_classdef;
  546. is_classdef:=false;
  547. end;
  548. _CONST:
  549. begin
  550. consume(_CONST);
  551. member_blocktype:=bt_const;
  552. end;
  553. _ID, _CASE, _OPERATOR :
  554. begin
  555. case idtoken of
  556. _PRIVATE :
  557. begin
  558. consume(_PRIVATE);
  559. current_structdef.symtable.currentvisibility:=vis_private;
  560. include(current_structdef.objectoptions,oo_has_private);
  561. fields_allowed:=true;
  562. is_classdef:=false;
  563. classfields:=false;
  564. member_blocktype:=bt_general;
  565. end;
  566. _PROTECTED :
  567. begin
  568. consume(_PROTECTED);
  569. current_structdef.symtable.currentvisibility:=vis_protected;
  570. include(current_structdef.objectoptions,oo_has_protected);
  571. fields_allowed:=true;
  572. is_classdef:=false;
  573. classfields:=false;
  574. member_blocktype:=bt_general;
  575. end;
  576. _PUBLIC :
  577. begin
  578. consume(_PUBLIC);
  579. current_structdef.symtable.currentvisibility:=vis_public;
  580. fields_allowed:=true;
  581. is_classdef:=false;
  582. classfields:=false;
  583. member_blocktype:=bt_general;
  584. end;
  585. _PUBLISHED :
  586. begin
  587. Message(parser_e_no_record_published);
  588. consume(_PUBLISHED);
  589. current_structdef.symtable.currentvisibility:=vis_published;
  590. fields_allowed:=true;
  591. is_classdef:=false;
  592. classfields:=false;
  593. member_blocktype:=bt_general;
  594. end;
  595. _STRICT :
  596. begin
  597. consume(_STRICT);
  598. if token=_ID then
  599. begin
  600. case idtoken of
  601. _PRIVATE:
  602. begin
  603. consume(_PRIVATE);
  604. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  605. include(current_structdef.objectoptions,oo_has_strictprivate);
  606. end;
  607. _PROTECTED:
  608. begin
  609. consume(_PROTECTED);
  610. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  611. include(current_structdef.objectoptions,oo_has_strictprotected);
  612. end;
  613. else
  614. message(parser_e_protected_or_private_expected);
  615. end;
  616. end
  617. else
  618. message(parser_e_protected_or_private_expected);
  619. fields_allowed:=true;
  620. is_classdef:=false;
  621. classfields:=false;
  622. member_blocktype:=bt_general;
  623. end
  624. else
  625. if is_classdef and (idtoken=_OPERATOR) then
  626. begin
  627. oldparse_only:=parse_only;
  628. parse_only:=true;
  629. pd:=parse_proc_dec(is_classdef,current_structdef);
  630. { this is for error recovery as well as forward }
  631. { interface mappings, i.e. mapping to a method }
  632. { which isn't declared yet }
  633. if assigned(pd) then
  634. begin
  635. parse_record_proc_directives(pd);
  636. handle_calling_convention(pd);
  637. { add definition to procsym }
  638. proc_add_definition(pd);
  639. end;
  640. maybe_parse_hint_directives(pd);
  641. parse_only:=oldparse_only;
  642. fields_allowed:=false;
  643. is_classdef:=false;
  644. end
  645. else
  646. begin
  647. if member_blocktype=bt_general then
  648. begin
  649. if (not fields_allowed) then
  650. Message(parser_e_field_not_allowed_here);
  651. vdoptions:=[vd_record];
  652. if classfields then
  653. include(vdoptions,vd_class);
  654. read_record_fields(vdoptions);
  655. end
  656. else if member_blocktype=bt_type then
  657. types_dec(true)
  658. else if member_blocktype=bt_const then
  659. consts_dec(true)
  660. else
  661. internalerror(201001110);
  662. end;
  663. end;
  664. end;
  665. _PROPERTY :
  666. begin
  667. struct_property_dec(is_classdef);
  668. fields_allowed:=false;
  669. is_classdef:=false;
  670. end;
  671. _CLASS:
  672. begin
  673. is_classdef:=false;
  674. { read class method }
  675. if try_to_consume(_CLASS) then
  676. begin
  677. { class modifier is only allowed for procedures, functions, }
  678. { constructors, destructors, fields and properties }
  679. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  680. not((token=_ID) and (idtoken=_OPERATOR)) then
  681. Message(parser_e_procedure_or_function_expected);
  682. is_classdef:=true;
  683. end;
  684. end;
  685. _PROCEDURE,
  686. _FUNCTION:
  687. begin
  688. oldparse_only:=parse_only;
  689. parse_only:=true;
  690. pd:=parse_proc_dec(is_classdef,current_structdef);
  691. { this is for error recovery as well as forward }
  692. { interface mappings, i.e. mapping to a method }
  693. { which isn't declared yet }
  694. if assigned(pd) then
  695. begin
  696. parse_record_proc_directives(pd);
  697. { since records have no inheritance don't allow non static
  698. class methods. delphi do so. }
  699. if is_classdef and not (po_staticmethod in pd.procoptions) then
  700. MessagePos(pd.fileinfo, parser_e_class_methods_only_static_in_records);
  701. handle_calling_convention(pd);
  702. { add definition to procsym }
  703. proc_add_definition(pd);
  704. end;
  705. maybe_parse_hint_directives(pd);
  706. parse_only:=oldparse_only;
  707. fields_allowed:=false;
  708. is_classdef:=false;
  709. end;
  710. _CONSTRUCTOR :
  711. begin
  712. if not is_classdef then
  713. Message(parser_e_no_constructor_in_records);
  714. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  715. Message(parser_w_constructor_should_be_public);
  716. { only 1 class constructor is allowed }
  717. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  718. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  719. oldparse_only:=parse_only;
  720. parse_only:=true;
  721. if is_classdef then
  722. pd:=class_constructor_head
  723. else
  724. pd:=constructor_head;
  725. parse_record_proc_directives(pd);
  726. handle_calling_convention(pd);
  727. { add definition to procsym }
  728. proc_add_definition(pd);
  729. maybe_parse_hint_directives(pd);
  730. parse_only:=oldparse_only;
  731. fields_allowed:=false;
  732. is_classdef:=false;
  733. end;
  734. _DESTRUCTOR :
  735. begin
  736. if not is_classdef then
  737. Message(parser_e_no_destructor_in_records);
  738. { only 1 class destructor is allowed }
  739. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  740. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  741. oldparse_only:=parse_only;
  742. parse_only:=true;
  743. if is_classdef then
  744. pd:=class_destructor_head
  745. else
  746. pd:=destructor_head;
  747. parse_record_proc_directives(pd);
  748. handle_calling_convention(pd);
  749. { add definition to procsym }
  750. proc_add_definition(pd);
  751. maybe_parse_hint_directives(pd);
  752. parse_only:=oldparse_only;
  753. fields_allowed:=false;
  754. is_classdef:=false;
  755. end;
  756. _END :
  757. begin
  758. consume(_END);
  759. break;
  760. end;
  761. else
  762. consume(_ID); { Give a ident expected message, like tp7 }
  763. end;
  764. until false;
  765. end;
  766. { reads a record declaration }
  767. function record_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  768. var
  769. old_current_structdef: tabstractrecorddef;
  770. old_current_genericdef,
  771. old_current_specializedef: tstoreddef;
  772. old_parse_generic: boolean;
  773. recst: trecordsymtable;
  774. begin
  775. old_current_structdef:=current_structdef;
  776. old_current_genericdef:=current_genericdef;
  777. old_current_specializedef:=current_specializedef;
  778. old_parse_generic:=parse_generic;
  779. current_genericdef:=nil;
  780. current_specializedef:=nil;
  781. { create recdef }
  782. recst:=trecordsymtable.create(n,current_settings.packrecords);
  783. current_structdef:=trecorddef.create(n,recst);
  784. result:=current_structdef;
  785. { insert in symtablestack }
  786. symtablestack.push(recst);
  787. { parse record }
  788. consume(_RECORD);
  789. { usage of specialized type inside its generic template }
  790. if assigned(genericdef) then
  791. current_specializedef:=current_structdef
  792. { reject declaration of generic class inside generic class }
  793. else if assigned(genericlist) then
  794. current_genericdef:=current_structdef;
  795. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  796. parse_generic:=(df_generic in current_structdef.defoptions);
  797. if m_advanced_records in current_settings.modeswitches then
  798. parse_record_members
  799. else
  800. begin
  801. read_record_fields([vd_record]);
  802. consume(_END);
  803. end;
  804. { make the record size aligned }
  805. recst.addalignmentpadding;
  806. { restore symtable stack }
  807. symtablestack.pop(recst);
  808. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  809. Message(type_e_no_packed_inittable);
  810. { restore old state }
  811. parse_generic:=old_parse_generic;
  812. current_structdef:=old_current_structdef;
  813. current_genericdef:=old_current_genericdef;
  814. current_specializedef:=old_current_specializedef;
  815. end;
  816. { reads a type definition and returns a pointer to it }
  817. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  818. var
  819. pt : tnode;
  820. tt2 : tdef;
  821. aktenumdef : tenumdef;
  822. s : TIDString;
  823. l,v : TConstExprInt;
  824. oldpackrecords : longint;
  825. defpos,storepos : tfileposinfo;
  826. procedure expr_type;
  827. var
  828. pt1,pt2 : tnode;
  829. lv,hv : TConstExprInt;
  830. old_block_type : tblock_type;
  831. dospecialize : boolean;
  832. structdef: tabstractrecorddef;
  833. begin
  834. old_block_type:=block_type;
  835. dospecialize:=false;
  836. { use of current parsed object:
  837. - classes can be used also in classes
  838. - objects can be parameters }
  839. if (token=_ID) then
  840. begin
  841. structdef:=current_structdef;
  842. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  843. begin
  844. if (structdef.objname^=pattern) then
  845. begin
  846. consume(_ID);
  847. def:=structdef;
  848. exit;
  849. end;
  850. structdef:=tabstractrecorddef(structdef.owner.defowner);
  851. end;
  852. end;
  853. { Generate a specialization in FPC mode? }
  854. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  855. { we can't accept a equal in type }
  856. pt1:=comp_expr(false,true);
  857. if not dospecialize and
  858. try_to_consume(_POINTPOINT) then
  859. begin
  860. { get high value of range }
  861. pt2:=comp_expr(false,false);
  862. { make both the same type or give an error. This is not
  863. done when both are integer values, because typecasting
  864. between -3200..3200 will result in a signed-unsigned
  865. conflict and give a range check error (PFV) }
  866. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  867. inserttypeconv(pt1,pt2.resultdef);
  868. { both must be evaluated to constants now }
  869. if (pt1.nodetype=ordconstn) and
  870. (pt2.nodetype=ordconstn) then
  871. begin
  872. lv:=tordconstnode(pt1).value;
  873. hv:=tordconstnode(pt2).value;
  874. { Check bounds }
  875. if hv<lv then
  876. message(parser_e_upper_lower_than_lower)
  877. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  878. message(type_e_cant_eval_constant_expr)
  879. else
  880. begin
  881. { All checks passed, create the new def }
  882. case pt1.resultdef.typ of
  883. enumdef :
  884. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  885. orddef :
  886. begin
  887. if is_char(pt1.resultdef) then
  888. def:=torddef.create(uchar,lv,hv)
  889. else
  890. if is_boolean(pt1.resultdef) then
  891. def:=torddef.create(pasbool,lv,hv)
  892. else if is_signed(pt1.resultdef) then
  893. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  894. else
  895. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  896. end;
  897. end;
  898. end;
  899. end
  900. else
  901. Message(sym_e_error_in_type_def);
  902. pt2.free;
  903. end
  904. else
  905. begin
  906. { a simple type renaming or generic specialization }
  907. if (pt1.nodetype=typen) then
  908. begin
  909. def:=ttypenode(pt1).resultdef;
  910. { Delphi mode specialization? }
  911. if (m_delphi in current_settings.modeswitches) then
  912. dospecialize:=token=_LSHARPBRACKET;
  913. if dospecialize then
  914. generate_specialization(def)
  915. else
  916. begin
  917. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  918. begin
  919. def:=current_specializedef
  920. end
  921. else if (def=current_genericdef) then
  922. begin
  923. def:=current_genericdef
  924. end
  925. else if (df_generic in def.defoptions) then
  926. begin
  927. Message(parser_e_no_generics_as_types);
  928. def:=generrordef;
  929. end
  930. else if is_objccategory(def) then
  931. begin
  932. Message(parser_e_no_category_as_types);
  933. def:=generrordef
  934. end
  935. end;
  936. end
  937. else
  938. Message(sym_e_error_in_type_def);
  939. end;
  940. pt1.free;
  941. block_type:=old_block_type;
  942. end;
  943. procedure set_dec;
  944. begin
  945. consume(_SET);
  946. consume(_OF);
  947. read_anon_type(tt2,true);
  948. if assigned(tt2) then
  949. begin
  950. case tt2.typ of
  951. { don't forget that min can be negativ PM }
  952. enumdef :
  953. if (tenumdef(tt2).min>=0) and
  954. (tenumdef(tt2).max<=255) then
  955. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  956. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  957. else
  958. Message(sym_e_ill_type_decl_set);
  959. orddef :
  960. begin
  961. if (torddef(tt2).ordtype<>uvoid) and
  962. (torddef(tt2).ordtype<>uwidechar) and
  963. (torddef(tt2).low>=0) then
  964. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  965. if Torddef(tt2).high>int64(high(byte)) then
  966. message(sym_e_ill_type_decl_set)
  967. else
  968. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  969. else
  970. Message(sym_e_ill_type_decl_set);
  971. end;
  972. else
  973. Message(sym_e_ill_type_decl_set);
  974. end;
  975. end
  976. else
  977. def:=generrordef;
  978. end;
  979. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  980. var
  981. lowval,
  982. highval : TConstExprInt;
  983. indexdef : tdef;
  984. hdef : tdef;
  985. arrdef : tarraydef;
  986. procedure setdefdecl(def:tdef);
  987. begin
  988. case def.typ of
  989. enumdef :
  990. begin
  991. lowval:=tenumdef(def).min;
  992. highval:=tenumdef(def).max;
  993. if (m_fpc in current_settings.modeswitches) and
  994. (tenumdef(def).has_jumps) then
  995. Message(type_e_array_index_enums_with_assign_not_possible);
  996. indexdef:=def;
  997. end;
  998. orddef :
  999. begin
  1000. if torddef(def).ordtype in [uchar,
  1001. u8bit,u16bit,
  1002. s8bit,s16bit,s32bit,
  1003. {$ifdef cpu64bitaddr}
  1004. u32bit,s64bit,
  1005. {$endif cpu64bitaddr}
  1006. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  1007. uwidechar] then
  1008. begin
  1009. lowval:=torddef(def).low;
  1010. highval:=torddef(def).high;
  1011. indexdef:=def;
  1012. end
  1013. else
  1014. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1015. end;
  1016. else
  1017. Message(sym_e_error_in_type_def);
  1018. end;
  1019. end;
  1020. var
  1021. old_current_genericdef,
  1022. old_current_specializedef: tstoreddef;
  1023. old_parse_generic: boolean;
  1024. begin
  1025. old_current_genericdef:=current_genericdef;
  1026. old_current_specializedef:=current_specializedef;
  1027. old_parse_generic:=parse_generic;
  1028. current_genericdef:=nil;
  1029. current_specializedef:=nil;
  1030. arrdef:=nil;
  1031. consume(_ARRAY);
  1032. { open array? }
  1033. if try_to_consume(_LECKKLAMMER) then
  1034. begin
  1035. { defaults }
  1036. indexdef:=generrordef;
  1037. { use defaults which don't overflow the compiler }
  1038. lowval:=0;
  1039. highval:=0;
  1040. repeat
  1041. { read the expression and check it, check apart if the
  1042. declaration is an enum declaration because that needs to
  1043. be parsed by readtype (PFV) }
  1044. if token=_LKLAMMER then
  1045. begin
  1046. read_anon_type(hdef,true);
  1047. setdefdecl(hdef);
  1048. end
  1049. else
  1050. begin
  1051. pt:=expr(true);
  1052. if pt.nodetype=typen then
  1053. setdefdecl(pt.resultdef)
  1054. else
  1055. begin
  1056. if (pt.nodetype=rangen) then
  1057. begin
  1058. if (trangenode(pt).left.nodetype=ordconstn) and
  1059. (trangenode(pt).right.nodetype=ordconstn) then
  1060. begin
  1061. { make both the same type or give an error. This is not
  1062. done when both are integer values, because typecasting
  1063. between -3200..3200 will result in a signed-unsigned
  1064. conflict and give a range check error (PFV) }
  1065. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1066. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1067. lowval:=tordconstnode(trangenode(pt).left).value;
  1068. highval:=tordconstnode(trangenode(pt).right).value;
  1069. if highval<lowval then
  1070. begin
  1071. Message(parser_e_array_lower_less_than_upper_bound);
  1072. highval:=lowval;
  1073. end
  1074. else if (lowval<int64(low(aint))) or
  1075. (highval > high(aint)) then
  1076. begin
  1077. Message(parser_e_array_range_out_of_bounds);
  1078. lowval :=0;
  1079. highval:=0;
  1080. end;
  1081. if is_integer(trangenode(pt).left.resultdef) then
  1082. range_to_type(lowval,highval,indexdef)
  1083. else
  1084. indexdef:=trangenode(pt).left.resultdef;
  1085. end
  1086. else
  1087. Message(type_e_cant_eval_constant_expr);
  1088. end
  1089. else
  1090. Message(sym_e_error_in_type_def)
  1091. end;
  1092. pt.free;
  1093. end;
  1094. { if the array is already created add the new arrray
  1095. as element of the existing array, otherwise create a new array }
  1096. if assigned(arrdef) then
  1097. begin
  1098. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1099. arrdef:=tarraydef(arrdef.elementdef);
  1100. end
  1101. else
  1102. begin
  1103. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1104. def:=arrdef;
  1105. end;
  1106. if is_packed then
  1107. include(arrdef.arrayoptions,ado_IsBitPacked);
  1108. if token=_COMMA then
  1109. consume(_COMMA)
  1110. else
  1111. break;
  1112. until false;
  1113. consume(_RECKKLAMMER);
  1114. end
  1115. else
  1116. begin
  1117. if is_packed then
  1118. Message(parser_e_packed_dynamic_open_array);
  1119. arrdef:=tarraydef.create(0,-1,s32inttype);
  1120. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1121. def:=arrdef;
  1122. end;
  1123. if assigned(arrdef) then
  1124. begin
  1125. { usage of specialized type inside its generic template }
  1126. if assigned(genericdef) then
  1127. current_specializedef:=arrdef
  1128. { reject declaration of generic class inside generic class }
  1129. else if assigned(genericlist) then
  1130. current_genericdef:=arrdef;
  1131. symtablestack.push(arrdef.symtable);
  1132. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1133. parse_generic:=(df_generic in arrdef.defoptions);
  1134. end;
  1135. consume(_OF);
  1136. read_anon_type(tt2,true);
  1137. { set element type of the last array definition }
  1138. if assigned(arrdef) then
  1139. begin
  1140. symtablestack.pop(arrdef.symtable);
  1141. arrdef.elementdef:=tt2;
  1142. if is_packed and
  1143. is_managed_type(tt2) then
  1144. Message(type_e_no_packed_inittable);
  1145. end;
  1146. { restore old state }
  1147. parse_generic:=old_parse_generic;
  1148. current_genericdef:=old_current_genericdef;
  1149. current_specializedef:=old_current_specializedef;
  1150. end;
  1151. var
  1152. p : tnode;
  1153. hdef : tdef;
  1154. pd : tabstractprocdef;
  1155. is_func,
  1156. enumdupmsg, first, is_specialize : boolean;
  1157. newtype : ttypesym;
  1158. oldlocalswitches : tlocalswitches;
  1159. bitpacking: boolean;
  1160. stitem: psymtablestackitem;
  1161. sym: tsym;
  1162. st: tsymtable;
  1163. begin
  1164. def:=nil;
  1165. case token of
  1166. _STRING,_FILE:
  1167. begin
  1168. single_type(def,false,true);
  1169. end;
  1170. _LKLAMMER:
  1171. begin
  1172. consume(_LKLAMMER);
  1173. first:=true;
  1174. { allow negativ value_str }
  1175. l:=int64(-1);
  1176. enumdupmsg:=false;
  1177. { check that we are not adding an enum from specialization
  1178. we can't just use current_specializedef because of inner types
  1179. like specialize array of record }
  1180. is_specialize:=false;
  1181. stitem:=symtablestack.stack;
  1182. while assigned(stitem) do
  1183. begin
  1184. { check records, classes and arrays because they can be specialized }
  1185. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1186. begin
  1187. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1188. stitem:=stitem^.next;
  1189. end
  1190. else
  1191. break;
  1192. end;
  1193. if not is_specialize then
  1194. aktenumdef:=tenumdef.create
  1195. else
  1196. aktenumdef:=nil;
  1197. repeat
  1198. { if it is a specialization then search the first enum member
  1199. and get the member owner instead of just created enumdef }
  1200. if not assigned(aktenumdef) then
  1201. begin
  1202. searchsym(pattern,sym,st);
  1203. if sym.typ=enumsym then
  1204. aktenumdef:=tenumsym(sym).definition
  1205. else
  1206. internalerror(201101021);
  1207. end;
  1208. s:=orgpattern;
  1209. defpos:=current_tokenpos;
  1210. consume(_ID);
  1211. { only allow assigning of specific numbers under fpc mode }
  1212. if not(m_tp7 in current_settings.modeswitches) and
  1213. (
  1214. { in fpc mode also allow := to be compatible
  1215. with previous 1.0.x versions }
  1216. ((m_fpc in current_settings.modeswitches) and
  1217. try_to_consume(_ASSIGNMENT)) or
  1218. try_to_consume(_EQ)
  1219. ) then
  1220. begin
  1221. oldlocalswitches:=current_settings.localswitches;
  1222. include(current_settings.localswitches,cs_allow_enum_calc);
  1223. p:=comp_expr(true,false);
  1224. current_settings.localswitches:=oldlocalswitches;
  1225. if (p.nodetype=ordconstn) then
  1226. begin
  1227. { we expect an integer or an enum of the
  1228. same type }
  1229. if is_integer(p.resultdef) or
  1230. is_char(p.resultdef) or
  1231. equal_defs(p.resultdef,aktenumdef) then
  1232. v:=tordconstnode(p).value
  1233. else
  1234. IncompatibleTypes(p.resultdef,s32inttype);
  1235. end
  1236. else
  1237. Message(parser_e_illegal_expression);
  1238. p.free;
  1239. { please leave that a note, allows type save }
  1240. { declarations in the win32 units ! }
  1241. if (not first) and (v<=l) and (not enumdupmsg) then
  1242. begin
  1243. Message(parser_n_duplicate_enum);
  1244. enumdupmsg:=true;
  1245. end;
  1246. l:=v;
  1247. end
  1248. else
  1249. inc(l.svalue);
  1250. first:=false;
  1251. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1252. if not is_specialize then
  1253. begin
  1254. storepos:=current_tokenpos;
  1255. current_tokenpos:=defpos;
  1256. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1257. if not (cs_scopedenums in current_settings.localswitches) then
  1258. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1259. current_tokenpos:=storepos;
  1260. end;
  1261. until not try_to_consume(_COMMA);
  1262. def:=aktenumdef;
  1263. consume(_RKLAMMER);
  1264. end;
  1265. _ARRAY:
  1266. begin
  1267. array_dec(false,genericdef,genericlist);
  1268. end;
  1269. _SET:
  1270. begin
  1271. set_dec;
  1272. end;
  1273. _CARET:
  1274. begin
  1275. consume(_CARET);
  1276. single_type(tt2,(block_type=bt_type),false);
  1277. def:=tpointerdef.create(tt2);
  1278. if tt2.typ=forwarddef then
  1279. current_module.checkforwarddefs.add(def);
  1280. end;
  1281. _RECORD:
  1282. begin
  1283. def:=record_dec(name,genericdef,genericlist);
  1284. end;
  1285. _PACKED,
  1286. _BITPACKED:
  1287. begin
  1288. bitpacking :=
  1289. (cs_bitpacking in current_settings.localswitches) or
  1290. (token = _BITPACKED);
  1291. consume(token);
  1292. if token=_ARRAY then
  1293. array_dec(bitpacking,genericdef,genericlist)
  1294. else if token=_SET then
  1295. set_dec
  1296. else if token=_FILE then
  1297. single_type(def,false,true)
  1298. else
  1299. begin
  1300. oldpackrecords:=current_settings.packrecords;
  1301. if (not bitpacking) or
  1302. (token in [_CLASS,_OBJECT]) then
  1303. current_settings.packrecords:=1
  1304. else
  1305. current_settings.packrecords:=bit_alignment;
  1306. case token of
  1307. _CLASS :
  1308. begin
  1309. consume(_CLASS);
  1310. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1311. end;
  1312. _OBJECT :
  1313. begin
  1314. consume(_OBJECT);
  1315. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1316. end;
  1317. else
  1318. def:=record_dec(name,genericdef,genericlist);
  1319. end;
  1320. current_settings.packrecords:=oldpackrecords;
  1321. end;
  1322. end;
  1323. _DISPINTERFACE :
  1324. begin
  1325. { need extra check here since interface is a keyword
  1326. in all pascal modes }
  1327. if not(m_class in current_settings.modeswitches) then
  1328. Message(parser_f_need_objfpc_or_delphi_mode);
  1329. consume(token);
  1330. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  1331. end;
  1332. _CLASS :
  1333. begin
  1334. consume(token);
  1335. { Delphi only allows class of in type blocks }
  1336. if (token=_OF) and
  1337. (
  1338. not(m_delphi in current_settings.modeswitches) or
  1339. (block_type=bt_type)
  1340. ) then
  1341. begin
  1342. consume(_OF);
  1343. single_type(hdef,(block_type=bt_type),false);
  1344. if is_class(hdef) or
  1345. is_objcclass(hdef) then
  1346. def:=tclassrefdef.create(hdef)
  1347. else
  1348. if hdef.typ=forwarddef then
  1349. begin
  1350. def:=tclassrefdef.create(hdef);
  1351. current_module.checkforwarddefs.add(def);
  1352. end
  1353. else
  1354. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1355. end
  1356. else
  1357. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1358. end;
  1359. _CPPCLASS :
  1360. begin
  1361. consume(token);
  1362. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  1363. end;
  1364. _OBJCCLASS :
  1365. begin
  1366. if not(m_objectivec1 in current_settings.modeswitches) then
  1367. Message(parser_f_need_objc);
  1368. consume(token);
  1369. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil);
  1370. end;
  1371. _INTERFACE :
  1372. begin
  1373. { need extra check here since interface is a keyword
  1374. in all pascal modes }
  1375. if not(m_class in current_settings.modeswitches) then
  1376. Message(parser_f_need_objfpc_or_delphi_mode);
  1377. consume(token);
  1378. if current_settings.interfacetype=it_interfacecom then
  1379. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  1380. else {it_interfacecorba}
  1381. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  1382. end;
  1383. _OBJCPROTOCOL :
  1384. begin
  1385. if not(m_objectivec1 in current_settings.modeswitches) then
  1386. Message(parser_f_need_objc);
  1387. consume(token);
  1388. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil);
  1389. end;
  1390. _OBJCCATEGORY :
  1391. begin
  1392. if not(m_objectivec1 in current_settings.modeswitches) then
  1393. Message(parser_f_need_objc);
  1394. consume(token);
  1395. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil);
  1396. end;
  1397. _OBJECT :
  1398. begin
  1399. consume(token);
  1400. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1401. end;
  1402. _PROCEDURE,
  1403. _FUNCTION:
  1404. begin
  1405. is_func:=(token=_FUNCTION);
  1406. consume(token);
  1407. pd:=tprocvardef.create(normal_function_level);
  1408. if token=_LKLAMMER then
  1409. parse_parameter_dec(pd);
  1410. if is_func then
  1411. begin
  1412. consume(_COLON);
  1413. single_type(pd.returndef,false,false);
  1414. end;
  1415. if try_to_consume(_OF) then
  1416. begin
  1417. consume(_OBJECT);
  1418. include(pd.procoptions,po_methodpointer);
  1419. end
  1420. else if (m_nested_procvars in current_settings.modeswitches) and
  1421. try_to_consume(_IS) then
  1422. begin
  1423. consume(_NESTED);
  1424. pd.parast.symtablelevel:=normal_function_level+1;
  1425. pd.check_mark_as_nested;
  1426. end;
  1427. def:=pd;
  1428. { possible proc directives }
  1429. if parseprocvardir then
  1430. begin
  1431. if check_proc_directive(true) then
  1432. begin
  1433. newtype:=ttypesym.create('unnamed',def);
  1434. parse_var_proc_directives(tsym(newtype));
  1435. newtype.typedef:=nil;
  1436. def.typesym:=nil;
  1437. newtype.free;
  1438. end;
  1439. { Add implicit hidden parameters and function result }
  1440. handle_calling_convention(pd);
  1441. end;
  1442. end;
  1443. else
  1444. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1445. begin
  1446. consume(_KLAMMERAFFE);
  1447. single_type(tt2,(block_type=bt_type),false);
  1448. def:=tpointerdef.create(tt2);
  1449. if tt2.typ=forwarddef then
  1450. current_module.checkforwarddefs.add(def);
  1451. end
  1452. else
  1453. expr_type;
  1454. end;
  1455. if def=nil then
  1456. def:=generrordef;
  1457. end;
  1458. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1459. begin
  1460. read_named_type(def,'',nil,nil,parseprocvardir);
  1461. end;
  1462. procedure write_persistent_type_info(st:tsymtable);
  1463. var
  1464. i : longint;
  1465. def : tdef;
  1466. vmtwriter : TVMTWriter;
  1467. begin
  1468. for i:=0 to st.DefList.Count-1 do
  1469. begin
  1470. def:=tdef(st.DefList[i]);
  1471. case def.typ of
  1472. recorddef :
  1473. write_persistent_type_info(trecorddef(def).symtable);
  1474. objectdef :
  1475. begin
  1476. { Skip generics and forward defs }
  1477. if (df_generic in def.defoptions) or
  1478. (oo_is_forward in tobjectdef(def).objectoptions) then
  1479. continue;
  1480. write_persistent_type_info(tobjectdef(def).symtable);
  1481. { Write also VMT if not done yet }
  1482. if not(ds_vmt_written in def.defstates) then
  1483. begin
  1484. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1485. if is_interface(tobjectdef(def)) then
  1486. vmtwriter.writeinterfaceids;
  1487. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1488. vmtwriter.writevmt;
  1489. vmtwriter.free;
  1490. include(def.defstates,ds_vmt_written);
  1491. end;
  1492. end;
  1493. procdef :
  1494. begin
  1495. if assigned(tprocdef(def).localst) and
  1496. (tprocdef(def).localst.symtabletype=localsymtable) then
  1497. write_persistent_type_info(tprocdef(def).localst);
  1498. if assigned(tprocdef(def).parast) then
  1499. write_persistent_type_info(tprocdef(def).parast);
  1500. end;
  1501. end;
  1502. { generate always persistent tables for types in the interface so it can
  1503. be reused in other units and give always the same pointer location. }
  1504. { Init }
  1505. if (
  1506. assigned(def.typesym) and
  1507. (st.symtabletype=globalsymtable) and
  1508. not is_objc_class_or_protocol(def)
  1509. ) or
  1510. is_managed_type(def) or
  1511. (ds_init_table_used in def.defstates) then
  1512. RTTIWriter.write_rtti(def,initrtti);
  1513. { RTTI }
  1514. if (
  1515. assigned(def.typesym) and
  1516. (st.symtabletype=globalsymtable) and
  1517. not is_objc_class_or_protocol(def)
  1518. ) or
  1519. (ds_rtti_table_used in def.defstates) then
  1520. RTTIWriter.write_rtti(def,fullrtti);
  1521. end;
  1522. end;
  1523. end.