ptype.pas 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. end
  1220. else
  1221. Message(sym_e_error_in_type_def)
  1222. end;
  1223. pt.free;
  1224. end;
  1225. { if we are not at the first dimension, add the new arrray
  1226. as element of the existing array, otherwise modify the existing array }
  1227. if not(first) then
  1228. begin
  1229. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1230. { push new symtable }
  1231. symtablestack.pop(arrdef.symtable);
  1232. arrdef:=tarraydef(arrdef.elementdef);
  1233. symtablestack.push(arrdef.symtable);
  1234. end
  1235. else
  1236. begin
  1237. arrdef.lowrange:=lowval.svalue;
  1238. arrdef.highrange:=highval.svalue;
  1239. arrdef.rangedef:=indexdef;
  1240. def:=arrdef;
  1241. first:=false;
  1242. end;
  1243. if is_packed then
  1244. include(arrdef.arrayoptions,ado_IsBitPacked);
  1245. if token=_COMMA then
  1246. consume(_COMMA)
  1247. else
  1248. break;
  1249. until false;
  1250. consume(_RECKKLAMMER);
  1251. end
  1252. else
  1253. begin
  1254. if is_packed then
  1255. Message(parser_e_packed_dynamic_open_array);
  1256. arrdef.lowrange:=0;
  1257. arrdef.highrange:=-1;
  1258. arrdef.rangedef:=s32inttype;
  1259. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1260. def:=arrdef;
  1261. end;
  1262. consume(_OF);
  1263. read_anon_type(tt2,true);
  1264. { set element type of the last array definition }
  1265. if assigned(arrdef) then
  1266. begin
  1267. symtablestack.pop(arrdef.symtable);
  1268. arrdef.elementdef:=tt2;
  1269. if is_packed and
  1270. is_managed_type(tt2) then
  1271. Message(type_e_no_packed_inittable);
  1272. end;
  1273. { restore old state }
  1274. parse_generic:=old_parse_generic;
  1275. current_genericdef:=old_current_genericdef;
  1276. current_specializedef:=old_current_specializedef;
  1277. end;
  1278. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1279. var
  1280. is_func:boolean;
  1281. pd:tabstractprocdef;
  1282. newtype:ttypesym;
  1283. old_current_genericdef,
  1284. old_current_specializedef: tstoreddef;
  1285. old_parse_generic: boolean;
  1286. begin
  1287. old_current_genericdef:=current_genericdef;
  1288. old_current_specializedef:=current_specializedef;
  1289. old_parse_generic:=parse_generic;
  1290. current_genericdef:=nil;
  1291. current_specializedef:=nil;
  1292. is_func:=(token=_FUNCTION);
  1293. consume(token);
  1294. pd:=cprocvardef.create(normal_function_level);
  1295. { usage of specialized type inside its generic template }
  1296. if assigned(genericdef) then
  1297. current_specializedef:=pd
  1298. { reject declaration of generic class inside generic class }
  1299. else if assigned(genericlist) then
  1300. current_genericdef:=pd;
  1301. symtablestack.push(pd.parast);
  1302. insert_generic_parameter_types(pd,genericdef,genericlist);
  1303. { there are two possibilties for the following to be true:
  1304. * the procvar declaration itself is generic
  1305. * the procvar is declared inside a generic
  1306. in both cases we need "parse_generic" and "current_genericdef"
  1307. so that e.g. specializations of another generic inside the
  1308. current generic can be used (either inline ones or "type" ones) }
  1309. parse_generic:=(df_generic in pd.defoptions) or old_parse_generic;
  1310. if parse_generic and not assigned(current_genericdef) then
  1311. current_genericdef:=old_current_genericdef;
  1312. { don't allow to add defs to the symtable - use it for type param search only }
  1313. tparasymtable(pd.parast).readonly:=true;
  1314. if token=_LKLAMMER then
  1315. parse_parameter_dec(pd);
  1316. if is_func then
  1317. begin
  1318. consume(_COLON);
  1319. single_type(pd.returndef,[]);
  1320. end;
  1321. if try_to_consume(_OF) then
  1322. begin
  1323. consume(_OBJECT);
  1324. include(pd.procoptions,po_methodpointer);
  1325. end
  1326. else if (m_nested_procvars in current_settings.modeswitches) and
  1327. try_to_consume(_IS) then
  1328. begin
  1329. consume(_NESTED);
  1330. pd.parast.symtablelevel:=normal_function_level+1;
  1331. pd.check_mark_as_nested;
  1332. end;
  1333. symtablestack.pop(pd.parast);
  1334. tparasymtable(pd.parast).readonly:=false;
  1335. result:=pd;
  1336. { possible proc directives }
  1337. if parseprocvardir then
  1338. begin
  1339. if check_proc_directive(true) then
  1340. begin
  1341. newtype:=ctypesym.create('unnamed',result);
  1342. parse_var_proc_directives(tsym(newtype));
  1343. newtype.typedef:=nil;
  1344. result.typesym:=nil;
  1345. newtype.free;
  1346. end;
  1347. { Add implicit hidden parameters and function result }
  1348. handle_calling_convention(pd);
  1349. end;
  1350. { restore old state }
  1351. parse_generic:=old_parse_generic;
  1352. current_genericdef:=old_current_genericdef;
  1353. current_specializedef:=old_current_specializedef;
  1354. end;
  1355. const
  1356. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1357. SingleTypeOptionsIsDelphi:array[Boolean] of TSingleTypeOptions = ([],[stoAllowSpecialization]);
  1358. var
  1359. p : tnode;
  1360. hdef : tdef;
  1361. enumdupmsg, first, is_specialize : boolean;
  1362. oldlocalswitches : tlocalswitches;
  1363. bitpacking: boolean;
  1364. stitem: psymtablestackitem;
  1365. sym: tsym;
  1366. st: tsymtable;
  1367. begin
  1368. def:=nil;
  1369. v:=0;
  1370. l:=0;
  1371. if assigned(newsym) then
  1372. name:=newsym.RealName
  1373. else
  1374. name:='';
  1375. case token of
  1376. _STRING,_FILE:
  1377. begin
  1378. single_type(def,[stoAllowTypeDef]);
  1379. end;
  1380. _LKLAMMER:
  1381. begin
  1382. consume(_LKLAMMER);
  1383. first:=true;
  1384. { allow negativ value_str }
  1385. l:=int64(-1);
  1386. enumdupmsg:=false;
  1387. { check that we are not adding an enum from specialization
  1388. we can't just use current_specializedef because of inner types
  1389. like specialize array of record }
  1390. is_specialize:=false;
  1391. stitem:=symtablestack.stack;
  1392. while assigned(stitem) do
  1393. begin
  1394. { check records, classes and arrays because they can be specialized }
  1395. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1396. begin
  1397. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1398. stitem:=stitem^.next;
  1399. end
  1400. else
  1401. break;
  1402. end;
  1403. if not is_specialize then
  1404. aktenumdef:=cenumdef.create
  1405. else
  1406. aktenumdef:=nil;
  1407. repeat
  1408. { if it is a specialization then search the first enum member
  1409. and get the member owner instead of just created enumdef }
  1410. if not assigned(aktenumdef) then
  1411. begin
  1412. searchsym(pattern,sym,st);
  1413. if sym.typ=enumsym then
  1414. aktenumdef:=tenumsym(sym).definition
  1415. else
  1416. internalerror(201101021);
  1417. end;
  1418. s:=orgpattern;
  1419. defpos:=current_tokenpos;
  1420. consume(_ID);
  1421. { only allow assigning of specific numbers under fpc mode }
  1422. if not(m_tp7 in current_settings.modeswitches) and
  1423. (
  1424. { in fpc mode also allow := to be compatible
  1425. with previous 1.0.x versions }
  1426. ((m_fpc in current_settings.modeswitches) and
  1427. try_to_consume(_ASSIGNMENT)) or
  1428. try_to_consume(_EQ)
  1429. ) then
  1430. begin
  1431. oldlocalswitches:=current_settings.localswitches;
  1432. include(current_settings.localswitches,cs_allow_enum_calc);
  1433. p:=comp_expr(true,false);
  1434. current_settings.localswitches:=oldlocalswitches;
  1435. if (p.nodetype=ordconstn) then
  1436. begin
  1437. { we expect an integer or an enum of the
  1438. same type }
  1439. if is_integer(p.resultdef) or
  1440. is_char(p.resultdef) or
  1441. equal_defs(p.resultdef,aktenumdef) then
  1442. v:=tordconstnode(p).value
  1443. else
  1444. IncompatibleTypes(p.resultdef,s32inttype);
  1445. end
  1446. else
  1447. Message(parser_e_illegal_expression);
  1448. p.free;
  1449. { please leave that a note, allows type save }
  1450. { declarations in the win32 units ! }
  1451. if (not first) and (v<=l) and (not enumdupmsg) then
  1452. begin
  1453. Message(parser_n_duplicate_enum);
  1454. enumdupmsg:=true;
  1455. end;
  1456. l:=v;
  1457. end
  1458. else
  1459. inc(l.svalue);
  1460. first:=false;
  1461. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1462. if not is_specialize then
  1463. begin
  1464. storepos:=current_tokenpos;
  1465. current_tokenpos:=defpos;
  1466. tenumsymtable(aktenumdef.symtable).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1467. if not (cs_scopedenums in current_settings.localswitches) then
  1468. tstoredsymtable(aktenumdef.owner).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1469. current_tokenpos:=storepos;
  1470. end;
  1471. until not try_to_consume(_COMMA);
  1472. def:=aktenumdef;
  1473. consume(_RKLAMMER);
  1474. {$ifdef jvm}
  1475. jvm_maybe_create_enum_class(name,def);
  1476. {$endif}
  1477. end;
  1478. _ARRAY:
  1479. begin
  1480. array_dec(false,genericdef,genericlist);
  1481. end;
  1482. _SET:
  1483. begin
  1484. set_dec;
  1485. end;
  1486. _CARET:
  1487. begin
  1488. consume(_CARET);
  1489. single_type(tt2,
  1490. SingleTypeOptionsInTypeBlock[block_type=bt_type]+
  1491. SingleTypeOptionsIsDelphi[m_delphi in current_settings.modeswitches]
  1492. );
  1493. { in case of e.g. var or const sections we need to especially
  1494. check that we don't use a generic dummy symbol }
  1495. if (block_type<>bt_type) and
  1496. (tt2.typ=undefineddef) and
  1497. assigned(tt2.typesym) and
  1498. (sp_generic_dummy in tt2.typesym.symoptions) then
  1499. begin
  1500. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1501. if assigned(sym) and
  1502. not (sp_generic_dummy in sym.symoptions) and
  1503. (sym.typ=typesym) then
  1504. tt2:=ttypesym(sym).typedef
  1505. else
  1506. Message(parser_e_no_generics_as_types);
  1507. end;
  1508. { don't use getpointerdef() here, since this is a type
  1509. declaration (-> must create new typedef) }
  1510. def:=cpointerdef.create(tt2);
  1511. if tt2.typ=forwarddef then
  1512. current_module.checkforwarddefs.add(def);
  1513. end;
  1514. _RECORD:
  1515. begin
  1516. consume(token);
  1517. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1518. begin
  1519. consume(_HELPER);
  1520. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1521. end
  1522. else
  1523. def:=record_dec(name,genericdef,genericlist);
  1524. end;
  1525. _PACKED,
  1526. _BITPACKED:
  1527. begin
  1528. bitpacking :=
  1529. (cs_bitpacking in current_settings.localswitches) or
  1530. (token = _BITPACKED);
  1531. consume(token);
  1532. if token=_ARRAY then
  1533. array_dec(bitpacking,genericdef,genericlist)
  1534. else if token=_SET then
  1535. set_dec
  1536. else if token=_FILE then
  1537. single_type(def,[stoAllowTypeDef])
  1538. else
  1539. begin
  1540. oldpackrecords:=current_settings.packrecords;
  1541. if (not bitpacking) or
  1542. (token in [_CLASS,_OBJECT]) then
  1543. current_settings.packrecords:=1
  1544. else
  1545. current_settings.packrecords:=bit_alignment;
  1546. case token of
  1547. _CLASS :
  1548. begin
  1549. consume(_CLASS);
  1550. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1551. end;
  1552. _OBJECT :
  1553. begin
  1554. consume(_OBJECT);
  1555. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1556. end;
  1557. else begin
  1558. consume(_RECORD);
  1559. def:=record_dec(name,genericdef,genericlist);
  1560. end;
  1561. end;
  1562. current_settings.packrecords:=oldpackrecords;
  1563. end;
  1564. end;
  1565. _DISPINTERFACE :
  1566. begin
  1567. { need extra check here since interface is a keyword
  1568. in all pascal modes }
  1569. if not(m_class in current_settings.modeswitches) then
  1570. Message(parser_f_need_objfpc_or_delphi_mode);
  1571. consume(token);
  1572. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1573. end;
  1574. _CLASS :
  1575. begin
  1576. consume(token);
  1577. { Delphi only allows class of in type blocks }
  1578. if (token=_OF) and
  1579. (
  1580. not(m_delphi in current_settings.modeswitches) or
  1581. (block_type=bt_type)
  1582. ) then
  1583. begin
  1584. consume(_OF);
  1585. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1586. if is_class(hdef) or
  1587. is_objcclass(hdef) or
  1588. is_javaclass(hdef) then
  1589. def:=cclassrefdef.create(hdef)
  1590. else
  1591. if hdef.typ=forwarddef then
  1592. begin
  1593. def:=cclassrefdef.create(hdef);
  1594. current_module.checkforwarddefs.add(def);
  1595. end
  1596. else
  1597. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1598. end
  1599. else
  1600. if (idtoken=_HELPER) then
  1601. begin
  1602. consume(_HELPER);
  1603. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1604. end
  1605. else
  1606. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1607. end;
  1608. _CPPCLASS :
  1609. begin
  1610. consume(token);
  1611. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1612. end;
  1613. _OBJCCLASS :
  1614. begin
  1615. if not(m_objectivec1 in current_settings.modeswitches) then
  1616. Message(parser_f_need_objc);
  1617. consume(token);
  1618. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1619. end;
  1620. _INTERFACE :
  1621. begin
  1622. { need extra check here since interface is a keyword
  1623. in all pascal modes }
  1624. if not(m_class in current_settings.modeswitches) then
  1625. Message(parser_f_need_objfpc_or_delphi_mode);
  1626. consume(token);
  1627. case current_settings.interfacetype of
  1628. it_interfacecom:
  1629. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1630. it_interfacecorba:
  1631. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1632. it_interfacejava:
  1633. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1634. else
  1635. internalerror(2010122612);
  1636. end;
  1637. end;
  1638. _OBJCPROTOCOL :
  1639. begin
  1640. if not(m_objectivec1 in current_settings.modeswitches) then
  1641. Message(parser_f_need_objc);
  1642. consume(token);
  1643. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1644. end;
  1645. _OBJCCATEGORY :
  1646. begin
  1647. if not(m_objectivec1 in current_settings.modeswitches) then
  1648. Message(parser_f_need_objc);
  1649. consume(token);
  1650. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1651. end;
  1652. _OBJECT :
  1653. begin
  1654. consume(token);
  1655. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1656. end;
  1657. _PROCEDURE,
  1658. _FUNCTION:
  1659. begin
  1660. def:=procvar_dec(genericdef,genericlist);
  1661. {$ifdef jvm}
  1662. jvm_create_procvar_class(name,def);
  1663. {$endif}
  1664. end;
  1665. else
  1666. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1667. begin
  1668. consume(_KLAMMERAFFE);
  1669. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1670. def:=cpointerdef.create(tt2);
  1671. if tt2.typ=forwarddef then
  1672. current_module.checkforwarddefs.add(def);
  1673. end
  1674. else
  1675. if hadtypetoken and
  1676. { don't allow "type helper" in mode delphi and require modeswitch typehelpers }
  1677. ([m_delphi,m_type_helpers]*current_settings.modeswitches=[m_type_helpers]) and
  1678. (token=_ID) and (idtoken=_HELPER) then
  1679. begin
  1680. consume(_HELPER);
  1681. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1682. end
  1683. else
  1684. expr_type;
  1685. end;
  1686. if def=nil then
  1687. def:=generrordef;
  1688. end;
  1689. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1690. begin
  1691. read_named_type(def,nil,nil,nil,parseprocvardir,false);
  1692. end;
  1693. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1694. var
  1695. sstate: tscannerstate;
  1696. pd: tprocdef;
  1697. begin
  1698. replace_scanner('tcinit_routine',sstate);
  1699. { the typed constant initialization code is called from the class
  1700. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1701. know yet whether that will be necessary, because there may be
  1702. typed constants inside method bodies -> always force the addition
  1703. of a class constructor.
  1704. We cannot directly add the typed constant initialisations to the
  1705. class constructor, because when it's parsed not all method bodies
  1706. are necessarily already parsed }
  1707. pd:=def.find_procdef_bytype(potype_class_constructor);
  1708. { the class constructor }
  1709. if not assigned(pd) then
  1710. begin
  1711. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1712. pd.synthetickind:=tsk_empty
  1713. else
  1714. internalerror(2011040206);
  1715. end;
  1716. { the initialisation helper }
  1717. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1718. pd.synthetickind:=tsk_tcinit
  1719. else
  1720. internalerror(2011040207);
  1721. restore_scanner(sstate);
  1722. end;
  1723. end.