ptype.pas 66 KB

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