ptype.pas 87 KB

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