ptype.pas 88 KB

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