ptype.pas 74 KB

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