ptype.pas 74 KB

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