ptype.pas 73 KB

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