ptype.pas 73 KB

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