ptype.pas 87 KB

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