ptype.pas 72 KB

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