ptype.pas 79 KB

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