ptype.pas 85 KB

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