ptype.pas 78 KB

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