ptype.pas 90 KB

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