ptype.pas 82 KB

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