ptype.pas 85 KB

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