ptype.pas 69 KB

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