ptype.pas 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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:tfphashobjectlist;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. tstoreddef(ttypesym(srsym).typedef).is_generic 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 tstoreddef(def).is_generic and
  465. not
  466. (
  467. parse_generic and
  468. (
  469. { if this is a generic parameter than it has already been checked that this is
  470. a valid usage of a generic }
  471. (sp_generic_para in srsym.symoptions) or
  472. (
  473. (current_genericdef.typ in [recorddef,objectdef]) and
  474. (
  475. { if both defs belong to the same generic (e.g. both are
  476. subtypes) then we must allow the usage }
  477. defs_belong_to_same_generic(def,current_genericdef) or
  478. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  479. declarations inside a generic }
  480. sym_is_owned_by(srsym,tabstractrecorddef(current_genericdef).symtable)
  481. )
  482. )
  483. )
  484. )
  485. then
  486. begin
  487. srsym:=resolve_generic_dummysym(srsym.name);
  488. if assigned(srsym) and
  489. not (sp_generic_dummy in srsym.symoptions) and
  490. (srsym.typ=typesym) then
  491. def:=ttypesym(srsym).typedef
  492. else
  493. begin
  494. Message(parser_e_no_generics_as_types);
  495. def:=generrordef;
  496. end;
  497. end
  498. else if (def.typ=undefineddef) and
  499. (sp_generic_dummy in srsym.symoptions) and
  500. parse_generic and
  501. (current_genericdef.typ in [recorddef,objectdef]) and
  502. (Pos(upper(srsym.realname),tabstractrecorddef(current_genericdef).objname^)=1) then
  503. begin
  504. if m_delphi in current_settings.modeswitches then
  505. begin
  506. srsym:=resolve_generic_dummysym(srsym.name);
  507. if assigned(srsym) and
  508. not (sp_generic_dummy in srsym.symoptions) and
  509. (srsym.typ=typesym) then
  510. def:=ttypesym(srsym).typedef
  511. else
  512. begin
  513. Message(parser_e_no_generics_as_types);
  514. def:=generrordef;
  515. end;
  516. end
  517. else
  518. def:=current_genericdef;
  519. end
  520. else if is_classhelper(def) and
  521. not (stoParseClassParent in options) then
  522. begin
  523. Message(parser_e_no_category_as_types);
  524. def:=generrordef
  525. end
  526. end;
  527. end;
  528. procedure parse_record_members(recsym:tsym);
  529. function IsAnonOrLocal: Boolean;
  530. begin
  531. result:=(current_structdef.objname^='') or
  532. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]);
  533. end;
  534. var
  535. olddef : tdef;
  536. procedure set_typesym;
  537. begin
  538. if not assigned(recsym) then
  539. exit;
  540. if ttypesym(recsym).typedef=current_structdef then
  541. exit;
  542. ttypesym(recsym).typedef:=current_structdef;
  543. current_structdef.typesym:=recsym;
  544. end;
  545. procedure reset_typesym;
  546. begin
  547. if not assigned(recsym) then
  548. exit;
  549. if ttypesym(recsym).typedef<>current_structdef then
  550. exit;
  551. ttypesym(recsym).typedef:=olddef;
  552. current_structdef.typesym:=nil;
  553. end;
  554. var
  555. pd : tprocdef;
  556. oldparse_only: boolean;
  557. member_blocktype : tblock_type;
  558. fields_allowed, is_classdef, classfields: boolean;
  559. vdoptions: tvar_dec_options;
  560. begin
  561. { empty record declaration ? }
  562. if (token=_SEMICOLON) then
  563. Exit;
  564. { the correct typesym<->def relationship is needed for example when
  565. parsing parameters that are specializations of the record or when
  566. using nested constants and such }
  567. if assigned(recsym) then
  568. olddef:=ttypesym(recsym).typedef
  569. else
  570. olddef:=nil;
  571. set_typesym;
  572. current_structdef.symtable.currentvisibility:=vis_public;
  573. fields_allowed:=true;
  574. is_classdef:=false;
  575. classfields:=false;
  576. member_blocktype:=bt_general;
  577. repeat
  578. case token of
  579. _TYPE :
  580. begin
  581. consume(_TYPE);
  582. member_blocktype:=bt_type;
  583. { local and anonymous records can not have inner types. skip top record symtable }
  584. if IsAnonOrLocal then
  585. Message(parser_e_no_types_in_local_anonymous_records);
  586. end;
  587. _VAR :
  588. begin
  589. consume(_VAR);
  590. fields_allowed:=true;
  591. member_blocktype:=bt_general;
  592. classfields:=is_classdef;
  593. is_classdef:=false;
  594. end;
  595. _CONST:
  596. begin
  597. consume(_CONST);
  598. member_blocktype:=bt_const;
  599. { local and anonymous records can not have constants. skip top record symtable }
  600. if IsAnonOrLocal then
  601. Message(parser_e_no_consts_in_local_anonymous_records);
  602. end;
  603. _ID, _CASE, _OPERATOR :
  604. begin
  605. case idtoken of
  606. _PRIVATE :
  607. begin
  608. consume(_PRIVATE);
  609. current_structdef.symtable.currentvisibility:=vis_private;
  610. include(current_structdef.objectoptions,oo_has_private);
  611. fields_allowed:=true;
  612. is_classdef:=false;
  613. classfields:=false;
  614. member_blocktype:=bt_general;
  615. end;
  616. _PROTECTED :
  617. begin
  618. Message1(parser_e_not_allowed_in_record,tokeninfo^[_PROTECTED].str);
  619. consume(_PROTECTED);
  620. current_structdef.symtable.currentvisibility:=vis_protected;
  621. include(current_structdef.objectoptions,oo_has_protected);
  622. fields_allowed:=true;
  623. is_classdef:=false;
  624. classfields:=false;
  625. member_blocktype:=bt_general;
  626. end;
  627. _PUBLIC :
  628. begin
  629. consume(_PUBLIC);
  630. current_structdef.symtable.currentvisibility:=vis_public;
  631. fields_allowed:=true;
  632. is_classdef:=false;
  633. classfields:=false;
  634. member_blocktype:=bt_general;
  635. end;
  636. _PUBLISHED :
  637. begin
  638. Message(parser_e_no_record_published);
  639. consume(_PUBLISHED);
  640. current_structdef.symtable.currentvisibility:=vis_published;
  641. fields_allowed:=true;
  642. is_classdef:=false;
  643. classfields:=false;
  644. member_blocktype:=bt_general;
  645. end;
  646. _STRICT :
  647. begin
  648. consume(_STRICT);
  649. if token=_ID then
  650. begin
  651. case idtoken of
  652. _PRIVATE:
  653. begin
  654. consume(_PRIVATE);
  655. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  656. include(current_structdef.objectoptions,oo_has_strictprivate);
  657. end;
  658. _PROTECTED:
  659. begin
  660. { "strict protected" is not allowed for records }
  661. Message1(parser_e_not_allowed_in_record,tokeninfo^[_STRICT].str+' '+tokeninfo^[_PROTECTED].str);
  662. consume(_PROTECTED);
  663. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  664. include(current_structdef.objectoptions,oo_has_strictprotected);
  665. end;
  666. else
  667. message(parser_e_protected_or_private_expected);
  668. end;
  669. end
  670. else
  671. message(parser_e_protected_or_private_expected);
  672. fields_allowed:=true;
  673. is_classdef:=false;
  674. classfields:=false;
  675. member_blocktype:=bt_general;
  676. end
  677. else
  678. if is_classdef and (idtoken=_OPERATOR) then
  679. begin
  680. pd:=parse_record_method_dec(current_structdef,is_classdef);
  681. fields_allowed:=false;
  682. is_classdef:=false;
  683. end
  684. else
  685. begin
  686. if member_blocktype=bt_general then
  687. begin
  688. if (not fields_allowed)and(idtoken<>_CASE) then
  689. Message(parser_e_field_not_allowed_here);
  690. vdoptions:=[vd_record];
  691. if classfields then
  692. include(vdoptions,vd_class);
  693. read_record_fields(vdoptions,nil,nil);
  694. end
  695. else if member_blocktype=bt_type then
  696. types_dec(true)
  697. else if member_blocktype=bt_const then
  698. consts_dec(true,true)
  699. else
  700. internalerror(201001110);
  701. end;
  702. end;
  703. end;
  704. _PROPERTY :
  705. begin
  706. if IsAnonOrLocal then
  707. Message(parser_e_no_properties_in_local_anonymous_records);
  708. struct_property_dec(is_classdef);
  709. fields_allowed:=false;
  710. is_classdef:=false;
  711. end;
  712. _CLASS:
  713. begin
  714. is_classdef:=false;
  715. { read class method/field/property }
  716. consume(_CLASS);
  717. { class modifier is only allowed for procedures, functions, }
  718. { constructors, destructors, fields and properties }
  719. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  720. not((token=_ID) and (idtoken=_OPERATOR)) then
  721. Message(parser_e_procedure_or_function_expected);
  722. if IsAnonOrLocal then
  723. Message(parser_e_no_class_in_local_anonymous_records);
  724. is_classdef:=true;
  725. end;
  726. _PROCEDURE,
  727. _FUNCTION:
  728. begin
  729. if IsAnonOrLocal then
  730. Message(parser_e_no_methods_in_local_anonymous_records);
  731. pd:=parse_record_method_dec(current_structdef,is_classdef);
  732. fields_allowed:=false;
  733. is_classdef:=false;
  734. end;
  735. _CONSTRUCTOR :
  736. begin
  737. if IsAnonOrLocal then
  738. Message(parser_e_no_methods_in_local_anonymous_records);
  739. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  740. Message(parser_w_constructor_should_be_public);
  741. { only 1 class constructor is allowed }
  742. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  743. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  744. oldparse_only:=parse_only;
  745. parse_only:=true;
  746. if is_classdef then
  747. pd:=class_constructor_head(current_structdef)
  748. else
  749. begin
  750. pd:=constructor_head;
  751. if pd.minparacount = 0 then
  752. MessagePos(pd.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  753. end;
  754. parse_only:=oldparse_only;
  755. fields_allowed:=false;
  756. is_classdef:=false;
  757. end;
  758. _DESTRUCTOR :
  759. begin
  760. if IsAnonOrLocal then
  761. Message(parser_e_no_methods_in_local_anonymous_records);
  762. if not is_classdef then
  763. Message(parser_e_no_destructor_in_records);
  764. { only 1 class destructor is allowed }
  765. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  766. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  767. oldparse_only:=parse_only;
  768. parse_only:=true;
  769. if is_classdef then
  770. pd:=class_destructor_head(current_structdef)
  771. else
  772. pd:=destructor_head;
  773. parse_only:=oldparse_only;
  774. fields_allowed:=false;
  775. is_classdef:=false;
  776. end;
  777. _END :
  778. begin
  779. {$ifdef jvm}
  780. add_java_default_record_methods_intf(trecorddef(current_structdef));
  781. {$endif}
  782. if target_info.system in systems_typed_constants_node_init then
  783. add_typedconst_init_routine(current_structdef);
  784. consume(_END);
  785. break;
  786. end;
  787. else
  788. consume(_ID); { Give a ident expected message, like tp7 }
  789. end;
  790. until false;
  791. reset_typesym;
  792. end;
  793. { reads a record declaration }
  794. function record_dec(const n:tidstring;recsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  795. var
  796. old_current_structdef: tabstractrecorddef;
  797. old_current_genericdef,
  798. old_current_specializedef: tstoreddef;
  799. old_parse_generic: boolean;
  800. recst: trecordsymtable;
  801. begin
  802. old_current_structdef:=current_structdef;
  803. old_current_genericdef:=current_genericdef;
  804. old_current_specializedef:=current_specializedef;
  805. old_parse_generic:=parse_generic;
  806. current_genericdef:=nil;
  807. current_specializedef:=nil;
  808. { create recdef }
  809. if (n<>'') or
  810. not(target_info.system in systems_jvm) then
  811. begin
  812. recst:=trecordsymtable.create(n,current_settings.packrecords);
  813. { can't use recst.realname^ instead of n, because recst.realname is
  814. nil in case of an empty name }
  815. current_structdef:=crecorddef.create(n,recst);
  816. end
  817. else
  818. begin
  819. { for the JVM target records always need a name, because they are
  820. represented by a class }
  821. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),current_settings.packrecords);
  822. current_structdef:=crecorddef.create(recst.name^,recst);
  823. end;
  824. result:=current_structdef;
  825. { insert in symtablestack }
  826. symtablestack.push(recst);
  827. { usage of specialized type inside its generic template }
  828. if assigned(genericdef) then
  829. current_specializedef:=current_structdef
  830. { reject declaration of generic class inside generic class }
  831. else if assigned(genericlist) then
  832. current_genericdef:=current_structdef;
  833. { nested types of specializations are specializations as well }
  834. if assigned(old_current_structdef) and
  835. (df_specialization in old_current_structdef.defoptions) then
  836. include(current_structdef.defoptions,df_specialization);
  837. if assigned(old_current_structdef) and
  838. (df_generic in old_current_structdef.defoptions) then
  839. begin
  840. include(current_structdef.defoptions,df_generic);
  841. current_genericdef:=current_structdef;
  842. end;
  843. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  844. { when we are parsing a generic already then this is a generic as
  845. well }
  846. if old_parse_generic then
  847. include(current_structdef.defoptions, df_generic);
  848. parse_generic:=(df_generic in current_structdef.defoptions);
  849. { in non-Delphi modes we need a strict private symbol without type
  850. count and type parameters in the name to simply resolving }
  851. maybe_insert_generic_rename_symbol(n,genericlist);
  852. if m_advanced_records in current_settings.modeswitches then
  853. begin
  854. parse_record_members(recsym);
  855. end
  856. else
  857. begin
  858. read_record_fields([vd_record],nil,nil);
  859. {$ifdef jvm}
  860. { we need a constructor to create temps, a deep copy helper, ... }
  861. add_java_default_record_methods_intf(trecorddef(current_structdef));
  862. {$endif}
  863. if target_info.system in systems_typed_constants_node_init then
  864. add_typedconst_init_routine(current_structdef);
  865. consume(_END);
  866. end;
  867. { make the record size aligned (has to be done before inserting the
  868. parameters, because that may depend on the record's size) }
  869. recst.addalignmentpadding;
  870. { don't keep track of procdefs in a separate list, because the
  871. compiler may add additional procdefs (e.g. property wrappers for
  872. the jvm backend) }
  873. insert_record_hidden_paras(trecorddef(current_structdef));
  874. { restore symtable stack }
  875. symtablestack.pop(recst);
  876. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  877. Message(type_e_no_packed_inittable);
  878. { restore old state }
  879. parse_generic:=old_parse_generic;
  880. current_structdef:=old_current_structdef;
  881. current_genericdef:=old_current_genericdef;
  882. current_specializedef:=old_current_specializedef;
  883. end;
  884. { reads a type definition and returns a pointer to it }
  885. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;hadtypetoken:boolean);
  886. var
  887. pt : tnode;
  888. tt2 : tdef;
  889. aktenumdef : tenumdef;
  890. s : TIDString;
  891. l,v : TConstExprInt;
  892. oldpackrecords : longint;
  893. defpos,storepos : tfileposinfo;
  894. name: TIDString;
  895. procedure expr_type;
  896. var
  897. pt1,pt2 : tnode;
  898. lv,hv : TConstExprInt;
  899. old_block_type : tblock_type;
  900. dospecialize : boolean;
  901. newdef : tdef;
  902. sym : tsym;
  903. genstr : string;
  904. gencount : longint;
  905. begin
  906. old_block_type:=block_type;
  907. dospecialize:=false;
  908. { use of current parsed object:
  909. classes, objects, records can be used also in themself }
  910. if (token=_ID) then
  911. if try_parse_structdef_nested_type(def,current_structdef,false) then
  912. exit;
  913. { Generate a specialization in FPC mode? }
  914. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  915. { we can't accept a equal in type }
  916. pt1:=comp_expr(false,true);
  917. if not dospecialize and
  918. try_to_consume(_POINTPOINT) then
  919. begin
  920. { get high value of range }
  921. pt2:=comp_expr(false,false);
  922. { make both the same type or give an error. This is not
  923. done when both are integer values, because typecasting
  924. between -3200..3200 will result in a signed-unsigned
  925. conflict and give a range check error (PFV) }
  926. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  927. inserttypeconv(pt1,pt2.resultdef);
  928. { both must be evaluated to constants now }
  929. if (pt1.nodetype=ordconstn) and
  930. (pt2.nodetype=ordconstn) then
  931. begin
  932. lv:=tordconstnode(pt1).value;
  933. hv:=tordconstnode(pt2).value;
  934. { Check bounds }
  935. if hv<lv then
  936. message(parser_e_upper_lower_than_lower)
  937. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  938. message(type_e_cant_eval_constant_expr)
  939. else
  940. begin
  941. { All checks passed, create the new def }
  942. case pt1.resultdef.typ of
  943. enumdef :
  944. def:=cenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  945. orddef :
  946. begin
  947. if is_char(pt1.resultdef) then
  948. def:=corddef.create(uchar,lv,hv)
  949. else
  950. if is_boolean(pt1.resultdef) then
  951. def:=corddef.create(pasbool8,lv,hv)
  952. else if is_signed(pt1.resultdef) then
  953. def:=corddef.create(range_to_basetype(lv,hv),lv,hv)
  954. else
  955. def:=corddef.create(range_to_basetype(lv,hv),lv,hv);
  956. end;
  957. end;
  958. end;
  959. end
  960. else
  961. Message(sym_e_error_in_type_def);
  962. pt2.free;
  963. end
  964. else
  965. begin
  966. { a simple type renaming or generic specialization }
  967. if (pt1.nodetype=typen) then
  968. begin
  969. def:=ttypenode(pt1).resultdef;
  970. { Delphi mode specialization? }
  971. if (m_delphi in current_settings.modeswitches) then
  972. dospecialize:=token=_LSHARPBRACKET
  973. else
  974. { in non-Delphi modes we might get a inline specialization
  975. without "specialize" or "<T>" of the same type we're
  976. currently parsing, so we need to handle that special }
  977. newdef:=nil;
  978. if not dospecialize and
  979. assigned(ttypenode(pt1).typesym) and
  980. (ttypenode(pt1).typesym.typ=typesym) and
  981. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  982. assigned(current_structdef) and
  983. (
  984. (
  985. not (m_delphi in current_settings.modeswitches) and
  986. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  987. (df_generic in current_structdef.defoptions) and
  988. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  989. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  990. ) or (
  991. { this could be a nested specialization which uses
  992. the type name of a surrounding generic to
  993. reference the specialization of said surrounding
  994. class }
  995. (df_specialization in current_structdef.defoptions) and
  996. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  997. )
  998. )
  999. then
  1000. begin
  1001. if assigned(newdef) then
  1002. def:=newdef
  1003. else
  1004. def:=current_structdef;
  1005. if assigned(def) then
  1006. { handle nested types }
  1007. post_comp_expr_gendef(def)
  1008. else
  1009. def:=generrordef;
  1010. end;
  1011. if dospecialize then
  1012. begin
  1013. generate_specialization(def,false,name);
  1014. { handle nested types }
  1015. if assigned(def) then
  1016. post_comp_expr_gendef(def);
  1017. end
  1018. else
  1019. begin
  1020. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1021. begin
  1022. def:=current_specializedef
  1023. end
  1024. else if (def=current_genericdef) then
  1025. begin
  1026. def:=current_genericdef
  1027. end
  1028. else if tstoreddef(def).is_generic and
  1029. { TODO : check once nested generics are allowed }
  1030. not
  1031. (
  1032. parse_generic and
  1033. (current_genericdef.typ in [recorddef,objectdef]) and
  1034. (def.typ in [recorddef,objectdef]) and
  1035. (
  1036. { if both defs belong to the same generic (e.g. both are
  1037. subtypes) then we must allow the usage }
  1038. defs_belong_to_same_generic(def,current_genericdef) or
  1039. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  1040. declarations inside a generic }
  1041. (
  1042. (ttypenode(pt1).typesym<>nil) and
  1043. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  1044. )
  1045. )
  1046. )
  1047. then
  1048. begin
  1049. if assigned(def.typesym) then
  1050. begin
  1051. if ttypesym(def.typesym).typedef.typ<>undefineddef then
  1052. { non-Delphi modes... }
  1053. split_generic_name(def.typesym.name,genstr,gencount)
  1054. else
  1055. genstr:=def.typesym.name;
  1056. sym:=resolve_generic_dummysym(genstr);
  1057. end
  1058. else
  1059. sym:=nil;
  1060. if assigned(sym) and
  1061. not (sp_generic_dummy in sym.symoptions) and
  1062. (sym.typ=typesym) then
  1063. def:=ttypesym(sym).typedef
  1064. else
  1065. begin
  1066. Message(parser_e_no_generics_as_types);
  1067. def:=generrordef;
  1068. end;
  1069. end
  1070. else if is_classhelper(def) then
  1071. begin
  1072. Message(parser_e_no_category_as_types);
  1073. def:=generrordef
  1074. end
  1075. end;
  1076. end
  1077. else
  1078. Message(sym_e_error_in_type_def);
  1079. end;
  1080. pt1.free;
  1081. block_type:=old_block_type;
  1082. end;
  1083. procedure set_dec;
  1084. begin
  1085. consume(_SET);
  1086. consume(_OF);
  1087. read_anon_type(tt2,true);
  1088. if assigned(tt2) then
  1089. begin
  1090. case tt2.typ of
  1091. { don't forget that min can be negativ PM }
  1092. enumdef :
  1093. if (tenumdef(tt2).min>=0) and
  1094. (tenumdef(tt2).max<=255) then
  1095. // !! def:=csetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  1096. def:=csetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  1097. else
  1098. Message(sym_e_ill_type_decl_set);
  1099. orddef :
  1100. begin
  1101. if (torddef(tt2).ordtype<>uvoid) and
  1102. (torddef(tt2).ordtype<>uwidechar) and
  1103. (torddef(tt2).low>=0) then
  1104. // !! def:=csetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  1105. if Torddef(tt2).high>int64(high(byte)) then
  1106. message(sym_e_ill_type_decl_set)
  1107. else
  1108. def:=csetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  1109. else
  1110. Message(sym_e_ill_type_decl_set);
  1111. end;
  1112. else
  1113. Message(sym_e_ill_type_decl_set);
  1114. end;
  1115. end
  1116. else
  1117. def:=generrordef;
  1118. end;
  1119. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1120. var
  1121. lowval,
  1122. highval : TConstExprInt;
  1123. indexdef : tdef;
  1124. hdef : tdef;
  1125. arrdef : tarraydef;
  1126. procedure setdefdecl(def:tdef);
  1127. begin
  1128. case def.typ of
  1129. enumdef :
  1130. begin
  1131. lowval:=tenumdef(def).min;
  1132. highval:=tenumdef(def).max;
  1133. if (m_fpc in current_settings.modeswitches) and
  1134. (tenumdef(def).has_jumps) then
  1135. Message(type_e_array_index_enums_with_assign_not_possible);
  1136. indexdef:=def;
  1137. end;
  1138. orddef :
  1139. begin
  1140. if torddef(def).ordtype in [uchar,
  1141. u8bit,
  1142. s8bit,s16bit,
  1143. {$if defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1144. u16bit,s32bit,
  1145. {$endif defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1146. {$ifdef cpu64bitaddr}
  1147. u32bit,s64bit,
  1148. {$endif cpu64bitaddr}
  1149. pasbool8,pasbool16,pasbool32,pasbool64,
  1150. bool8bit,bool16bit,bool32bit,bool64bit,
  1151. uwidechar] then
  1152. begin
  1153. lowval:=torddef(def).low;
  1154. highval:=torddef(def).high;
  1155. indexdef:=def;
  1156. end
  1157. else
  1158. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1159. end;
  1160. else
  1161. Message(sym_e_error_in_type_def);
  1162. end;
  1163. end;
  1164. var
  1165. old_current_genericdef,
  1166. old_current_specializedef: tstoreddef;
  1167. first,
  1168. old_parse_generic: boolean;
  1169. begin
  1170. old_current_genericdef:=current_genericdef;
  1171. old_current_specializedef:=current_specializedef;
  1172. old_parse_generic:=parse_generic;
  1173. current_genericdef:=nil;
  1174. current_specializedef:=nil;
  1175. first:=true;
  1176. arrdef:=carraydef.create(0,0,s32inttype);
  1177. consume(_ARRAY);
  1178. { usage of specialized type inside its generic template }
  1179. if assigned(genericdef) then
  1180. current_specializedef:=arrdef
  1181. { reject declaration of generic class inside generic class }
  1182. else if assigned(genericlist) then
  1183. current_genericdef:=arrdef;
  1184. symtablestack.push(arrdef.symtable);
  1185. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1186. { there are two possibilties for the following to be true:
  1187. * the array declaration itself is generic
  1188. * the array is declared inside a generic
  1189. in both cases we need "parse_generic" and "current_genericdef"
  1190. so that e.g. specializations of another generic inside the
  1191. current generic can be used (either inline ones or "type" ones) }
  1192. parse_generic:=(df_generic in arrdef.defoptions) or old_parse_generic;
  1193. if parse_generic and not assigned(current_genericdef) then
  1194. current_genericdef:=old_current_genericdef;
  1195. { open array? }
  1196. if try_to_consume(_LECKKLAMMER) then
  1197. begin
  1198. { defaults }
  1199. indexdef:=generrordef;
  1200. { use defaults which don't overflow the compiler }
  1201. lowval:=0;
  1202. highval:=0;
  1203. repeat
  1204. { read the expression and check it, check apart if the
  1205. declaration is an enum declaration because that needs to
  1206. be parsed by readtype (PFV) }
  1207. if token=_LKLAMMER then
  1208. begin
  1209. read_anon_type(hdef,true);
  1210. setdefdecl(hdef);
  1211. end
  1212. else
  1213. begin
  1214. pt:=expr(true);
  1215. if pt.nodetype=typen then
  1216. setdefdecl(pt.resultdef)
  1217. else
  1218. begin
  1219. if pt.nodetype=rangen then
  1220. begin
  1221. { pure ordconstn expressions can be checked for
  1222. generics as well, but don't give an error in case
  1223. of parsing a generic if that isn't yet the case }
  1224. if (trangenode(pt).left.nodetype=ordconstn) and
  1225. (trangenode(pt).right.nodetype=ordconstn) then
  1226. begin
  1227. { make both the same type or give an error. This is not
  1228. done when both are integer values, because typecasting
  1229. between -3200..3200 will result in a signed-unsigned
  1230. conflict and give a range check error (PFV) }
  1231. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1232. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1233. lowval:=tordconstnode(trangenode(pt).left).value;
  1234. highval:=tordconstnode(trangenode(pt).right).value;
  1235. if highval<lowval then
  1236. begin
  1237. Message(parser_e_array_lower_less_than_upper_bound);
  1238. highval:=lowval;
  1239. end
  1240. else if (lowval<int64(low(asizeint))) or
  1241. (highval>high(asizeint)) then
  1242. begin
  1243. Message(parser_e_array_range_out_of_bounds);
  1244. lowval :=0;
  1245. highval:=0;
  1246. end;
  1247. if is_integer(trangenode(pt).left.resultdef) then
  1248. range_to_type(lowval,highval,indexdef)
  1249. else
  1250. indexdef:=trangenode(pt).left.resultdef;
  1251. end
  1252. else
  1253. if not parse_generic then
  1254. Message(type_e_cant_eval_constant_expr)
  1255. else
  1256. { we need a valid range for debug information }
  1257. range_to_type(lowval,highval,indexdef);
  1258. end
  1259. else
  1260. Message(sym_e_error_in_type_def)
  1261. end;
  1262. pt.free;
  1263. end;
  1264. { if we are not at the first dimension, add the new arrray
  1265. as element of the existing array, otherwise modify the existing array }
  1266. if not(first) then
  1267. begin
  1268. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1269. { push new symtable }
  1270. symtablestack.pop(arrdef.symtable);
  1271. arrdef:=tarraydef(arrdef.elementdef);
  1272. symtablestack.push(arrdef.symtable);
  1273. end
  1274. else
  1275. begin
  1276. arrdef.lowrange:=lowval.svalue;
  1277. arrdef.highrange:=highval.svalue;
  1278. arrdef.rangedef:=indexdef;
  1279. def:=arrdef;
  1280. first:=false;
  1281. end;
  1282. if is_packed then
  1283. include(arrdef.arrayoptions,ado_IsBitPacked);
  1284. if token=_COMMA then
  1285. consume(_COMMA)
  1286. else
  1287. break;
  1288. until false;
  1289. consume(_RECKKLAMMER);
  1290. end
  1291. else
  1292. begin
  1293. if is_packed then
  1294. Message(parser_e_packed_dynamic_open_array);
  1295. arrdef.lowrange:=0;
  1296. arrdef.highrange:=-1;
  1297. arrdef.rangedef:=s32inttype;
  1298. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1299. def:=arrdef;
  1300. end;
  1301. consume(_OF);
  1302. read_anon_type(tt2,true);
  1303. { set element type of the last array definition }
  1304. if assigned(arrdef) then
  1305. begin
  1306. symtablestack.pop(arrdef.symtable);
  1307. arrdef.elementdef:=tt2;
  1308. if is_packed and
  1309. is_managed_type(tt2) then
  1310. Message(type_e_no_packed_inittable);
  1311. end;
  1312. { restore old state }
  1313. parse_generic:=old_parse_generic;
  1314. current_genericdef:=old_current_genericdef;
  1315. current_specializedef:=old_current_specializedef;
  1316. end;
  1317. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  1318. var
  1319. is_func:boolean;
  1320. pd:tabstractprocdef;
  1321. newtype:ttypesym;
  1322. old_current_genericdef,
  1323. old_current_specializedef: tstoreddef;
  1324. old_parse_generic: boolean;
  1325. begin
  1326. old_current_genericdef:=current_genericdef;
  1327. old_current_specializedef:=current_specializedef;
  1328. old_parse_generic:=parse_generic;
  1329. current_genericdef:=nil;
  1330. current_specializedef:=nil;
  1331. is_func:=(token=_FUNCTION);
  1332. consume(token);
  1333. pd:=cprocvardef.create(normal_function_level);
  1334. { usage of specialized type inside its generic template }
  1335. if assigned(genericdef) then
  1336. current_specializedef:=pd
  1337. { reject declaration of generic class inside generic class }
  1338. else if assigned(genericlist) then
  1339. current_genericdef:=pd;
  1340. symtablestack.push(pd.parast);
  1341. insert_generic_parameter_types(pd,genericdef,genericlist);
  1342. { there are two possibilties for the following to be true:
  1343. * the procvar declaration itself is generic
  1344. * the procvar is declared inside a generic
  1345. in both cases we need "parse_generic" and "current_genericdef"
  1346. so that e.g. specializations of another generic inside the
  1347. current generic can be used (either inline ones or "type" ones) }
  1348. parse_generic:=(df_generic in pd.defoptions) or old_parse_generic;
  1349. if parse_generic and not assigned(current_genericdef) then
  1350. current_genericdef:=old_current_genericdef;
  1351. { don't allow to add defs to the symtable - use it for type param search only }
  1352. tparasymtable(pd.parast).readonly:=true;
  1353. if token=_LKLAMMER then
  1354. parse_parameter_dec(pd);
  1355. if is_func then
  1356. begin
  1357. consume(_COLON);
  1358. single_type(pd.returndef,[]);
  1359. end;
  1360. if try_to_consume(_OF) then
  1361. begin
  1362. consume(_OBJECT);
  1363. include(pd.procoptions,po_methodpointer);
  1364. end
  1365. else if (m_nested_procvars in current_settings.modeswitches) and
  1366. try_to_consume(_IS) then
  1367. begin
  1368. consume(_NESTED);
  1369. pd.parast.symtablelevel:=normal_function_level+1;
  1370. pd.check_mark_as_nested;
  1371. end;
  1372. symtablestack.pop(pd.parast);
  1373. tparasymtable(pd.parast).readonly:=false;
  1374. result:=pd;
  1375. { possible proc directives }
  1376. if parseprocvardir then
  1377. begin
  1378. if check_proc_directive(true) then
  1379. begin
  1380. newtype:=ctypesym.create('unnamed',result);
  1381. parse_var_proc_directives(tsym(newtype));
  1382. newtype.typedef:=nil;
  1383. result.typesym:=nil;
  1384. newtype.free;
  1385. end;
  1386. { Add implicit hidden parameters and function result }
  1387. handle_calling_convention(pd);
  1388. end;
  1389. { restore old state }
  1390. parse_generic:=old_parse_generic;
  1391. current_genericdef:=old_current_genericdef;
  1392. current_specializedef:=old_current_specializedef;
  1393. end;
  1394. const
  1395. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1396. SingleTypeOptionsIsDelphi:array[Boolean] of TSingleTypeOptions = ([],[stoAllowSpecialization]);
  1397. var
  1398. p : tnode;
  1399. hdef : tdef;
  1400. enumdupmsg, first, is_specialize : boolean;
  1401. oldlocalswitches : tlocalswitches;
  1402. bitpacking: boolean;
  1403. stitem: psymtablestackitem;
  1404. sym: tsym;
  1405. st: tsymtable;
  1406. begin
  1407. def:=nil;
  1408. v:=0;
  1409. l:=0;
  1410. if assigned(newsym) then
  1411. name:=newsym.RealName
  1412. else
  1413. name:='';
  1414. case token of
  1415. _STRING,_FILE:
  1416. begin
  1417. single_type(def,[stoAllowTypeDef]);
  1418. end;
  1419. _LKLAMMER:
  1420. begin
  1421. consume(_LKLAMMER);
  1422. first:=true;
  1423. { allow negativ value_str }
  1424. l:=int64(-1);
  1425. enumdupmsg:=false;
  1426. { check that we are not adding an enum from specialization
  1427. we can't just use current_specializedef because of inner types
  1428. like specialize array of record }
  1429. is_specialize:=false;
  1430. stitem:=symtablestack.stack;
  1431. while assigned(stitem) do
  1432. begin
  1433. { check records, classes and arrays because they can be specialized }
  1434. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1435. begin
  1436. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1437. stitem:=stitem^.next;
  1438. end
  1439. else
  1440. break;
  1441. end;
  1442. if not is_specialize then
  1443. aktenumdef:=cenumdef.create
  1444. else
  1445. aktenumdef:=nil;
  1446. repeat
  1447. { if it is a specialization then search the first enum member
  1448. and get the member owner instead of just created enumdef }
  1449. if not assigned(aktenumdef) then
  1450. begin
  1451. searchsym(pattern,sym,st);
  1452. if sym.typ=enumsym then
  1453. aktenumdef:=tenumsym(sym).definition
  1454. else
  1455. internalerror(201101021);
  1456. end;
  1457. s:=orgpattern;
  1458. defpos:=current_tokenpos;
  1459. consume(_ID);
  1460. { only allow assigning of specific numbers under fpc mode }
  1461. if not(m_tp7 in current_settings.modeswitches) and
  1462. (
  1463. { in fpc mode also allow := to be compatible
  1464. with previous 1.0.x versions }
  1465. ((m_fpc in current_settings.modeswitches) and
  1466. try_to_consume(_ASSIGNMENT)) or
  1467. try_to_consume(_EQ)
  1468. ) then
  1469. begin
  1470. oldlocalswitches:=current_settings.localswitches;
  1471. include(current_settings.localswitches,cs_allow_enum_calc);
  1472. p:=comp_expr(true,false);
  1473. current_settings.localswitches:=oldlocalswitches;
  1474. if (p.nodetype=ordconstn) then
  1475. begin
  1476. { we expect an integer or an enum of the
  1477. same type }
  1478. if is_integer(p.resultdef) or
  1479. is_char(p.resultdef) or
  1480. equal_defs(p.resultdef,aktenumdef) then
  1481. v:=tordconstnode(p).value
  1482. else
  1483. IncompatibleTypes(p.resultdef,s32inttype);
  1484. end
  1485. else
  1486. Message(parser_e_illegal_expression);
  1487. p.free;
  1488. { please leave that a note, allows type save }
  1489. { declarations in the win32 units ! }
  1490. if (not first) and (v<=l) and (not enumdupmsg) then
  1491. begin
  1492. Message(parser_n_duplicate_enum);
  1493. enumdupmsg:=true;
  1494. end;
  1495. l:=v;
  1496. end
  1497. else
  1498. inc(l.svalue);
  1499. first:=false;
  1500. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1501. if not is_specialize then
  1502. begin
  1503. storepos:=current_tokenpos;
  1504. current_tokenpos:=defpos;
  1505. tenumsymtable(aktenumdef.symtable).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1506. if not (cs_scopedenums in current_settings.localswitches) then
  1507. tstoredsymtable(aktenumdef.owner).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1508. current_tokenpos:=storepos;
  1509. end;
  1510. until not try_to_consume(_COMMA);
  1511. def:=aktenumdef;
  1512. consume(_RKLAMMER);
  1513. {$ifdef jvm}
  1514. jvm_maybe_create_enum_class(name,def);
  1515. {$endif}
  1516. end;
  1517. _ARRAY:
  1518. begin
  1519. array_dec(false,genericdef,genericlist);
  1520. end;
  1521. _SET:
  1522. begin
  1523. set_dec;
  1524. end;
  1525. _CARET:
  1526. begin
  1527. consume(_CARET);
  1528. single_type(tt2,
  1529. SingleTypeOptionsInTypeBlock[block_type=bt_type]+
  1530. SingleTypeOptionsIsDelphi[m_delphi in current_settings.modeswitches]
  1531. );
  1532. { in case of e.g. var or const sections we need to especially
  1533. check that we don't use a generic dummy symbol }
  1534. if (block_type<>bt_type) and
  1535. (tt2.typ=undefineddef) and
  1536. assigned(tt2.typesym) and
  1537. (sp_generic_dummy in tt2.typesym.symoptions) then
  1538. begin
  1539. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1540. if assigned(sym) and
  1541. not (sp_generic_dummy in sym.symoptions) and
  1542. (sym.typ=typesym) then
  1543. tt2:=ttypesym(sym).typedef
  1544. else
  1545. Message(parser_e_no_generics_as_types);
  1546. end;
  1547. { don't use getpointerdef() here, since this is a type
  1548. declaration (-> must create new typedef) }
  1549. def:=cpointerdef.create(tt2);
  1550. if tt2.typ=forwarddef then
  1551. current_module.checkforwarddefs.add(def);
  1552. end;
  1553. _RECORD:
  1554. begin
  1555. consume(token);
  1556. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1557. begin
  1558. consume(_HELPER);
  1559. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1560. end
  1561. else
  1562. def:=record_dec(name,newsym,genericdef,genericlist);
  1563. end;
  1564. _PACKED,
  1565. _BITPACKED:
  1566. begin
  1567. bitpacking :=
  1568. (cs_bitpacking in current_settings.localswitches) or
  1569. (token = _BITPACKED);
  1570. consume(token);
  1571. if token=_ARRAY then
  1572. array_dec(bitpacking,genericdef,genericlist)
  1573. else if token=_SET then
  1574. set_dec
  1575. else if token=_FILE then
  1576. single_type(def,[stoAllowTypeDef])
  1577. else
  1578. begin
  1579. oldpackrecords:=current_settings.packrecords;
  1580. if (not bitpacking) or
  1581. (token in [_CLASS,_OBJECT]) then
  1582. current_settings.packrecords:=1
  1583. else
  1584. current_settings.packrecords:=bit_alignment;
  1585. case token of
  1586. _CLASS :
  1587. begin
  1588. consume(_CLASS);
  1589. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1590. end;
  1591. _OBJECT :
  1592. begin
  1593. consume(_OBJECT);
  1594. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1595. end;
  1596. else begin
  1597. consume(_RECORD);
  1598. def:=record_dec(name,newsym,genericdef,genericlist);
  1599. end;
  1600. end;
  1601. current_settings.packrecords:=oldpackrecords;
  1602. end;
  1603. end;
  1604. _DISPINTERFACE :
  1605. begin
  1606. { need extra check here since interface is a keyword
  1607. in all pascal modes }
  1608. if not(m_class in current_settings.modeswitches) then
  1609. Message(parser_f_need_objfpc_or_delphi_mode);
  1610. consume(token);
  1611. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1612. end;
  1613. _CLASS :
  1614. begin
  1615. consume(token);
  1616. { Delphi only allows class of in type blocks }
  1617. if (token=_OF) and
  1618. (
  1619. not(m_delphi in current_settings.modeswitches) or
  1620. (block_type=bt_type)
  1621. ) then
  1622. begin
  1623. consume(_OF);
  1624. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1625. if is_class(hdef) or
  1626. is_objcclass(hdef) or
  1627. is_javaclass(hdef) then
  1628. def:=cclassrefdef.create(hdef)
  1629. else
  1630. if hdef.typ=forwarddef then
  1631. begin
  1632. def:=cclassrefdef.create(hdef);
  1633. current_module.checkforwarddefs.add(def);
  1634. end
  1635. else
  1636. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1637. end
  1638. else
  1639. if (idtoken=_HELPER) then
  1640. begin
  1641. consume(_HELPER);
  1642. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1643. end
  1644. else
  1645. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1646. end;
  1647. _CPPCLASS :
  1648. begin
  1649. consume(token);
  1650. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1651. end;
  1652. _OBJCCLASS :
  1653. begin
  1654. if not(m_objectivec1 in current_settings.modeswitches) then
  1655. Message(parser_f_need_objc);
  1656. consume(token);
  1657. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1658. end;
  1659. _INTERFACE :
  1660. begin
  1661. { need extra check here since interface is a keyword
  1662. in all pascal modes }
  1663. if not(m_class in current_settings.modeswitches) then
  1664. Message(parser_f_need_objfpc_or_delphi_mode);
  1665. consume(token);
  1666. case current_settings.interfacetype of
  1667. it_interfacecom:
  1668. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1669. it_interfacecorba:
  1670. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1671. it_interfacejava:
  1672. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1673. else
  1674. internalerror(2010122612);
  1675. end;
  1676. end;
  1677. _OBJCPROTOCOL :
  1678. begin
  1679. if not(m_objectivec1 in current_settings.modeswitches) then
  1680. Message(parser_f_need_objc);
  1681. consume(token);
  1682. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1683. end;
  1684. _OBJCCATEGORY :
  1685. begin
  1686. if not(m_objectivec1 in current_settings.modeswitches) then
  1687. Message(parser_f_need_objc);
  1688. consume(token);
  1689. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1690. end;
  1691. _OBJECT :
  1692. begin
  1693. consume(token);
  1694. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1695. end;
  1696. _PROCEDURE,
  1697. _FUNCTION:
  1698. begin
  1699. def:=procvar_dec(genericdef,genericlist);
  1700. {$ifdef jvm}
  1701. jvm_create_procvar_class(name,def);
  1702. {$endif}
  1703. end;
  1704. else
  1705. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1706. begin
  1707. consume(_KLAMMERAFFE);
  1708. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1709. def:=cpointerdef.create(tt2);
  1710. if tt2.typ=forwarddef then
  1711. current_module.checkforwarddefs.add(def);
  1712. end
  1713. else
  1714. if hadtypetoken and
  1715. { don't allow "type helper" in mode delphi and require modeswitch typehelpers }
  1716. ([m_delphi,m_type_helpers]*current_settings.modeswitches=[m_type_helpers]) and
  1717. (token=_ID) and (idtoken=_HELPER) then
  1718. begin
  1719. consume(_HELPER);
  1720. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1721. end
  1722. else
  1723. expr_type;
  1724. end;
  1725. if def=nil then
  1726. def:=generrordef;
  1727. end;
  1728. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1729. begin
  1730. read_named_type(def,nil,nil,nil,parseprocvardir,false);
  1731. end;
  1732. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1733. var
  1734. sstate: tscannerstate;
  1735. pd: tprocdef;
  1736. begin
  1737. replace_scanner('tcinit_routine',sstate);
  1738. { the typed constant initialization code is called from the class
  1739. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1740. know yet whether that will be necessary, because there may be
  1741. typed constants inside method bodies -> always force the addition
  1742. of a class constructor.
  1743. We cannot directly add the typed constant initialisations to the
  1744. class constructor, because when it's parsed not all method bodies
  1745. are necessarily already parsed }
  1746. pd:=def.find_procdef_bytype(potype_class_constructor);
  1747. { the class constructor }
  1748. if not assigned(pd) then
  1749. begin
  1750. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1751. pd.synthetickind:=tsk_empty
  1752. else
  1753. internalerror(2011040206);
  1754. end;
  1755. { the initialisation helper }
  1756. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1757. pd.synthetickind:=tsk_tcinit
  1758. else
  1759. internalerror(2011040207);
  1760. restore_scanner(sstate);
  1761. end;
  1762. end.