ptype.pas 69 KB

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