ptype.pas 67 KB

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