ptype.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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,u16bit,
  1045. s8bit,s16bit,s32bit,
  1046. {$ifdef cpu64bitaddr}
  1047. u32bit,s64bit,
  1048. {$endif cpu64bitaddr}
  1049. pasbool8,pasbool16,pasbool32,pasbool64,
  1050. bool8bit,bool16bit,bool32bit,bool64bit,
  1051. uwidechar] then
  1052. begin
  1053. lowval:=torddef(def).low;
  1054. highval:=torddef(def).high;
  1055. indexdef:=def;
  1056. end
  1057. else
  1058. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1059. end;
  1060. else
  1061. Message(sym_e_error_in_type_def);
  1062. end;
  1063. end;
  1064. var
  1065. old_current_genericdef,
  1066. old_current_specializedef: tstoreddef;
  1067. first,
  1068. old_parse_generic: boolean;
  1069. begin
  1070. old_current_genericdef:=current_genericdef;
  1071. old_current_specializedef:=current_specializedef;
  1072. old_parse_generic:=parse_generic;
  1073. current_genericdef:=nil;
  1074. current_specializedef:=nil;
  1075. first:=true;
  1076. arrdef:=tarraydef.create(0,0,s32inttype);
  1077. consume(_ARRAY);
  1078. { usage of specialized type inside its generic template }
  1079. if assigned(genericdef) then
  1080. current_specializedef:=arrdef
  1081. { reject declaration of generic class inside generic class }
  1082. else if assigned(genericlist) then
  1083. current_genericdef:=arrdef;
  1084. symtablestack.push(arrdef.symtable);
  1085. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1086. { there are two possibilties for the following to be true:
  1087. * the array declaration itself is generic
  1088. * the array is declared inside a generic
  1089. in both cases we need "parse_generic" and "current_genericdef"
  1090. so that e.g. specializations of another generic inside the
  1091. current generic can be used (either inline ones or "type" ones) }
  1092. parse_generic:=(df_generic in arrdef.defoptions) or old_parse_generic;
  1093. if parse_generic and not assigned(current_genericdef) then
  1094. current_genericdef:=old_current_genericdef;
  1095. { open array? }
  1096. if try_to_consume(_LECKKLAMMER) then
  1097. begin
  1098. { defaults }
  1099. indexdef:=generrordef;
  1100. { use defaults which don't overflow the compiler }
  1101. lowval:=0;
  1102. highval:=0;
  1103. repeat
  1104. { read the expression and check it, check apart if the
  1105. declaration is an enum declaration because that needs to
  1106. be parsed by readtype (PFV) }
  1107. if token=_LKLAMMER then
  1108. begin
  1109. read_anon_type(hdef,true);
  1110. setdefdecl(hdef);
  1111. end
  1112. else
  1113. begin
  1114. pt:=expr(true);
  1115. if pt.nodetype=typen then
  1116. setdefdecl(pt.resultdef)
  1117. else
  1118. begin
  1119. if pt.nodetype=rangen then
  1120. begin
  1121. { pure ordconstn expressions can be checked for
  1122. generics as well, but don't give an error in case
  1123. of parsing a generic if that isn't yet the case }
  1124. if (trangenode(pt).left.nodetype=ordconstn) and
  1125. (trangenode(pt).right.nodetype=ordconstn) then
  1126. begin
  1127. { make both the same type or give an error. This is not
  1128. done when both are integer values, because typecasting
  1129. between -3200..3200 will result in a signed-unsigned
  1130. conflict and give a range check error (PFV) }
  1131. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1132. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1133. lowval:=tordconstnode(trangenode(pt).left).value;
  1134. highval:=tordconstnode(trangenode(pt).right).value;
  1135. if highval<lowval then
  1136. begin
  1137. Message(parser_e_array_lower_less_than_upper_bound);
  1138. highval:=lowval;
  1139. end
  1140. else if (lowval<int64(low(asizeint))) or
  1141. (highval>high(asizeint)) then
  1142. begin
  1143. Message(parser_e_array_range_out_of_bounds);
  1144. lowval :=0;
  1145. highval:=0;
  1146. end;
  1147. if is_integer(trangenode(pt).left.resultdef) then
  1148. range_to_type(lowval,highval,indexdef)
  1149. else
  1150. indexdef:=trangenode(pt).left.resultdef;
  1151. end
  1152. else
  1153. if not parse_generic then
  1154. Message(type_e_cant_eval_constant_expr);
  1155. end
  1156. else
  1157. Message(sym_e_error_in_type_def)
  1158. end;
  1159. pt.free;
  1160. end;
  1161. { if we are not at the first dimension, add the new arrray
  1162. as element of the existing array, otherwise modify the existing array }
  1163. if not(first) then
  1164. begin
  1165. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1166. { push new symtable }
  1167. symtablestack.pop(arrdef.symtable);
  1168. arrdef:=tarraydef(arrdef.elementdef);
  1169. symtablestack.push(arrdef.symtable);
  1170. end
  1171. else
  1172. begin
  1173. arrdef.lowrange:=lowval.svalue;
  1174. arrdef.highrange:=highval.svalue;
  1175. arrdef.rangedef:=indexdef;
  1176. def:=arrdef;
  1177. first:=false;
  1178. end;
  1179. if is_packed then
  1180. include(arrdef.arrayoptions,ado_IsBitPacked);
  1181. if token=_COMMA then
  1182. consume(_COMMA)
  1183. else
  1184. break;
  1185. until false;
  1186. consume(_RECKKLAMMER);
  1187. end
  1188. else
  1189. begin
  1190. if is_packed then
  1191. Message(parser_e_packed_dynamic_open_array);
  1192. arrdef.lowrange:=0;
  1193. arrdef.highrange:=-1;
  1194. arrdef.rangedef:=s32inttype;
  1195. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1196. def:=arrdef;
  1197. end;
  1198. consume(_OF);
  1199. read_anon_type(tt2,true);
  1200. { set element type of the last array definition }
  1201. if assigned(arrdef) then
  1202. begin
  1203. symtablestack.pop(arrdef.symtable);
  1204. arrdef.elementdef:=tt2;
  1205. if is_packed and
  1206. is_managed_type(tt2) then
  1207. Message(type_e_no_packed_inittable);
  1208. end;
  1209. { restore old state }
  1210. parse_generic:=old_parse_generic;
  1211. current_genericdef:=old_current_genericdef;
  1212. current_specializedef:=old_current_specializedef;
  1213. end;
  1214. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1215. var
  1216. is_func:boolean;
  1217. pd:tabstractprocdef;
  1218. newtype:ttypesym;
  1219. old_current_genericdef,
  1220. old_current_specializedef: tstoreddef;
  1221. old_parse_generic: boolean;
  1222. begin
  1223. old_current_genericdef:=current_genericdef;
  1224. old_current_specializedef:=current_specializedef;
  1225. old_parse_generic:=parse_generic;
  1226. current_genericdef:=nil;
  1227. current_specializedef:=nil;
  1228. is_func:=(token=_FUNCTION);
  1229. consume(token);
  1230. pd:=tprocvardef.create(normal_function_level);
  1231. { usage of specialized type inside its generic template }
  1232. if assigned(genericdef) then
  1233. current_specializedef:=pd
  1234. { reject declaration of generic class inside generic class }
  1235. else if assigned(genericlist) then
  1236. current_genericdef:=pd;
  1237. symtablestack.push(pd.parast);
  1238. insert_generic_parameter_types(pd,genericdef,genericlist);
  1239. { there are two possibilties for the following to be true:
  1240. * the procvar declaration itself is generic
  1241. * the procvar is declared inside a generic
  1242. in both cases we need "parse_generic" and "current_genericdef"
  1243. so that e.g. specializations of another generic inside the
  1244. current generic can be used (either inline ones or "type" ones) }
  1245. parse_generic:=(df_generic in pd.defoptions) or old_parse_generic;
  1246. if parse_generic and not assigned(current_genericdef) then
  1247. current_genericdef:=old_current_genericdef;
  1248. { don't allow to add defs to the symtable - use it for type param search only }
  1249. tparasymtable(pd.parast).readonly:=true;
  1250. if token=_LKLAMMER then
  1251. parse_parameter_dec(pd);
  1252. if is_func then
  1253. begin
  1254. consume(_COLON);
  1255. single_type(pd.returndef,[]);
  1256. end;
  1257. if try_to_consume(_OF) then
  1258. begin
  1259. consume(_OBJECT);
  1260. include(pd.procoptions,po_methodpointer);
  1261. end
  1262. else if (m_nested_procvars in current_settings.modeswitches) and
  1263. try_to_consume(_IS) then
  1264. begin
  1265. consume(_NESTED);
  1266. pd.parast.symtablelevel:=normal_function_level+1;
  1267. pd.check_mark_as_nested;
  1268. end;
  1269. symtablestack.pop(pd.parast);
  1270. tparasymtable(pd.parast).readonly:=false;
  1271. result:=pd;
  1272. { possible proc directives }
  1273. if parseprocvardir then
  1274. begin
  1275. if check_proc_directive(true) then
  1276. begin
  1277. newtype:=ttypesym.create('unnamed',result);
  1278. parse_var_proc_directives(tsym(newtype));
  1279. newtype.typedef:=nil;
  1280. result.typesym:=nil;
  1281. newtype.free;
  1282. end;
  1283. { Add implicit hidden parameters and function result }
  1284. handle_calling_convention(pd);
  1285. end;
  1286. { restore old state }
  1287. parse_generic:=old_parse_generic;
  1288. current_genericdef:=old_current_genericdef;
  1289. current_specializedef:=old_current_specializedef;
  1290. end;
  1291. const
  1292. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1293. SingleTypeOptionsIsDelphi:array[Boolean] of TSingleTypeOptions = ([],[stoAllowSpecialization]);
  1294. var
  1295. p : tnode;
  1296. hdef : tdef;
  1297. enumdupmsg, first, is_specialize : boolean;
  1298. oldlocalswitches : tlocalswitches;
  1299. bitpacking: boolean;
  1300. stitem: psymtablestackitem;
  1301. sym: tsym;
  1302. st: tsymtable;
  1303. begin
  1304. def:=nil;
  1305. if assigned(newsym) then
  1306. name:=newsym.RealName
  1307. else
  1308. name:='';
  1309. case token of
  1310. _STRING,_FILE:
  1311. begin
  1312. single_type(def,[stoAllowTypeDef]);
  1313. end;
  1314. _LKLAMMER:
  1315. begin
  1316. consume(_LKLAMMER);
  1317. first:=true;
  1318. { allow negativ value_str }
  1319. l:=int64(-1);
  1320. enumdupmsg:=false;
  1321. { check that we are not adding an enum from specialization
  1322. we can't just use current_specializedef because of inner types
  1323. like specialize array of record }
  1324. is_specialize:=false;
  1325. stitem:=symtablestack.stack;
  1326. while assigned(stitem) do
  1327. begin
  1328. { check records, classes and arrays because they can be specialized }
  1329. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1330. begin
  1331. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1332. stitem:=stitem^.next;
  1333. end
  1334. else
  1335. break;
  1336. end;
  1337. if not is_specialize then
  1338. aktenumdef:=tenumdef.create
  1339. else
  1340. aktenumdef:=nil;
  1341. repeat
  1342. { if it is a specialization then search the first enum member
  1343. and get the member owner instead of just created enumdef }
  1344. if not assigned(aktenumdef) then
  1345. begin
  1346. searchsym(pattern,sym,st);
  1347. if sym.typ=enumsym then
  1348. aktenumdef:=tenumsym(sym).definition
  1349. else
  1350. internalerror(201101021);
  1351. end;
  1352. s:=orgpattern;
  1353. defpos:=current_tokenpos;
  1354. consume(_ID);
  1355. { only allow assigning of specific numbers under fpc mode }
  1356. if not(m_tp7 in current_settings.modeswitches) and
  1357. (
  1358. { in fpc mode also allow := to be compatible
  1359. with previous 1.0.x versions }
  1360. ((m_fpc in current_settings.modeswitches) and
  1361. try_to_consume(_ASSIGNMENT)) or
  1362. try_to_consume(_EQ)
  1363. ) then
  1364. begin
  1365. oldlocalswitches:=current_settings.localswitches;
  1366. include(current_settings.localswitches,cs_allow_enum_calc);
  1367. p:=comp_expr(true,false);
  1368. current_settings.localswitches:=oldlocalswitches;
  1369. if (p.nodetype=ordconstn) then
  1370. begin
  1371. { we expect an integer or an enum of the
  1372. same type }
  1373. if is_integer(p.resultdef) or
  1374. is_char(p.resultdef) or
  1375. equal_defs(p.resultdef,aktenumdef) then
  1376. v:=tordconstnode(p).value
  1377. else
  1378. IncompatibleTypes(p.resultdef,s32inttype);
  1379. end
  1380. else
  1381. Message(parser_e_illegal_expression);
  1382. p.free;
  1383. { please leave that a note, allows type save }
  1384. { declarations in the win32 units ! }
  1385. if (not first) and (v<=l) and (not enumdupmsg) then
  1386. begin
  1387. Message(parser_n_duplicate_enum);
  1388. enumdupmsg:=true;
  1389. end;
  1390. l:=v;
  1391. end
  1392. else
  1393. inc(l.svalue);
  1394. first:=false;
  1395. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1396. if not is_specialize then
  1397. begin
  1398. storepos:=current_tokenpos;
  1399. current_tokenpos:=defpos;
  1400. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1401. if not (cs_scopedenums in current_settings.localswitches) then
  1402. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1403. current_tokenpos:=storepos;
  1404. end;
  1405. until not try_to_consume(_COMMA);
  1406. def:=aktenumdef;
  1407. consume(_RKLAMMER);
  1408. {$ifdef jvm}
  1409. jvm_maybe_create_enum_class(name,def);
  1410. {$endif}
  1411. end;
  1412. _ARRAY:
  1413. begin
  1414. array_dec(false,genericdef,genericlist);
  1415. end;
  1416. _SET:
  1417. begin
  1418. set_dec;
  1419. end;
  1420. _CARET:
  1421. begin
  1422. consume(_CARET);
  1423. single_type(tt2,
  1424. SingleTypeOptionsInTypeBlock[block_type=bt_type]+
  1425. SingleTypeOptionsIsDelphi[m_delphi in current_settings.modeswitches]
  1426. );
  1427. { in case of e.g. var or const sections we need to especially
  1428. check that we don't use a generic dummy symbol }
  1429. if (block_type<>bt_type) and
  1430. (tt2.typ=undefineddef) and
  1431. assigned(tt2.typesym) and
  1432. (sp_generic_dummy in tt2.typesym.symoptions) then
  1433. Message(parser_e_no_generics_as_types);
  1434. { don't use getpointerdef() here, since this is a type
  1435. declaration (-> must create new typedef) }
  1436. def:=tpointerdef.create(tt2);
  1437. if tt2.typ=forwarddef then
  1438. current_module.checkforwarddefs.add(def);
  1439. end;
  1440. _RECORD:
  1441. begin
  1442. consume(token);
  1443. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1444. begin
  1445. consume(_HELPER);
  1446. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1447. end
  1448. else
  1449. def:=record_dec(name,genericdef,genericlist);
  1450. end;
  1451. _PACKED,
  1452. _BITPACKED:
  1453. begin
  1454. bitpacking :=
  1455. (cs_bitpacking in current_settings.localswitches) or
  1456. (token = _BITPACKED);
  1457. consume(token);
  1458. if token=_ARRAY then
  1459. array_dec(bitpacking,genericdef,genericlist)
  1460. else if token=_SET then
  1461. set_dec
  1462. else if token=_FILE then
  1463. single_type(def,[stoAllowTypeDef])
  1464. else
  1465. begin
  1466. oldpackrecords:=current_settings.packrecords;
  1467. if (not bitpacking) or
  1468. (token in [_CLASS,_OBJECT]) then
  1469. current_settings.packrecords:=1
  1470. else
  1471. current_settings.packrecords:=bit_alignment;
  1472. case token of
  1473. _CLASS :
  1474. begin
  1475. consume(_CLASS);
  1476. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1477. end;
  1478. _OBJECT :
  1479. begin
  1480. consume(_OBJECT);
  1481. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1482. end;
  1483. else begin
  1484. consume(_RECORD);
  1485. def:=record_dec(name,genericdef,genericlist);
  1486. end;
  1487. end;
  1488. current_settings.packrecords:=oldpackrecords;
  1489. end;
  1490. end;
  1491. _DISPINTERFACE :
  1492. begin
  1493. { need extra check here since interface is a keyword
  1494. in all pascal modes }
  1495. if not(m_class in current_settings.modeswitches) then
  1496. Message(parser_f_need_objfpc_or_delphi_mode);
  1497. consume(token);
  1498. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1499. end;
  1500. _CLASS :
  1501. begin
  1502. consume(token);
  1503. { Delphi only allows class of in type blocks }
  1504. if (token=_OF) and
  1505. (
  1506. not(m_delphi in current_settings.modeswitches) or
  1507. (block_type=bt_type)
  1508. ) then
  1509. begin
  1510. consume(_OF);
  1511. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1512. if is_class(hdef) or
  1513. is_objcclass(hdef) or
  1514. is_javaclass(hdef) then
  1515. def:=tclassrefdef.create(hdef)
  1516. else
  1517. if hdef.typ=forwarddef then
  1518. begin
  1519. def:=tclassrefdef.create(hdef);
  1520. current_module.checkforwarddefs.add(def);
  1521. end
  1522. else
  1523. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1524. end
  1525. else
  1526. if (idtoken=_HELPER) then
  1527. begin
  1528. consume(_HELPER);
  1529. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1530. end
  1531. else
  1532. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1533. end;
  1534. _CPPCLASS :
  1535. begin
  1536. consume(token);
  1537. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1538. end;
  1539. _OBJCCLASS :
  1540. begin
  1541. if not(m_objectivec1 in current_settings.modeswitches) then
  1542. Message(parser_f_need_objc);
  1543. consume(token);
  1544. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1545. end;
  1546. _INTERFACE :
  1547. begin
  1548. { need extra check here since interface is a keyword
  1549. in all pascal modes }
  1550. if not(m_class in current_settings.modeswitches) then
  1551. Message(parser_f_need_objfpc_or_delphi_mode);
  1552. consume(token);
  1553. case current_settings.interfacetype of
  1554. it_interfacecom:
  1555. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1556. it_interfacecorba:
  1557. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1558. it_interfacejava:
  1559. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1560. else
  1561. internalerror(2010122612);
  1562. end;
  1563. end;
  1564. _OBJCPROTOCOL :
  1565. begin
  1566. if not(m_objectivec1 in current_settings.modeswitches) then
  1567. Message(parser_f_need_objc);
  1568. consume(token);
  1569. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1570. end;
  1571. _OBJCCATEGORY :
  1572. begin
  1573. if not(m_objectivec1 in current_settings.modeswitches) then
  1574. Message(parser_f_need_objc);
  1575. consume(token);
  1576. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1577. end;
  1578. _OBJECT :
  1579. begin
  1580. consume(token);
  1581. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1582. end;
  1583. _PROCEDURE,
  1584. _FUNCTION:
  1585. begin
  1586. def:=procvar_dec(genericdef,genericlist);
  1587. {$ifdef jvm}
  1588. jvm_create_procvar_class(name,def);
  1589. {$endif}
  1590. end;
  1591. else
  1592. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1593. begin
  1594. consume(_KLAMMERAFFE);
  1595. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1596. def:=tpointerdef.create(tt2);
  1597. if tt2.typ=forwarddef then
  1598. current_module.checkforwarddefs.add(def);
  1599. end
  1600. else
  1601. if hadtypetoken and
  1602. { don't allow "type helper" in mode delphi and require modeswitch class }
  1603. ([m_delphi,m_class]*current_settings.modeswitches=[m_class]) and
  1604. (token=_ID) and (idtoken=_HELPER) then
  1605. begin
  1606. consume(_HELPER);
  1607. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1608. end
  1609. else
  1610. expr_type;
  1611. end;
  1612. if def=nil then
  1613. def:=generrordef;
  1614. end;
  1615. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1616. begin
  1617. read_named_type(def,nil,nil,nil,parseprocvardir,false);
  1618. end;
  1619. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1620. var
  1621. sstate: tscannerstate;
  1622. pd: tprocdef;
  1623. begin
  1624. replace_scanner('tcinit_routine',sstate);
  1625. { the typed constant initialization code is called from the class
  1626. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1627. know yet whether that will be necessary, because there may be
  1628. typed constants inside method bodies -> always force the addition
  1629. of a class constructor.
  1630. We cannot directly add the typed constant initialisations to the
  1631. class constructor, because when it's parsed not all method bodies
  1632. are necessarily already parsed }
  1633. pd:=def.find_procdef_bytype(potype_class_constructor);
  1634. { the class constructor }
  1635. if not assigned(pd) then
  1636. begin
  1637. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1638. pd.synthetickind:=tsk_empty
  1639. else
  1640. internalerror(2011040206);
  1641. end;
  1642. { the initialisation helper }
  1643. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1644. pd.synthetickind:=tsk_tcinit
  1645. else
  1646. internalerror(2011040207);
  1647. restore_scanner(sstate);
  1648. end;
  1649. end.