ptype.pas 75 KB

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