ptype.pas 68 KB

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