ptype.pas 86 KB

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