ptype.pas 93 KB

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