ptype.pas 78 KB

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