ptype.pas 70 KB

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