ptype.pas 72 KB

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