ptype.pas 93 KB

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