ptype.pas 70 KB

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