ptype.pas 67 KB

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