ptype.pas 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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);
  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. Message(type_e_type_is_not_completly_defined);
  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,'',nil,'');
  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. var
  485. pd : tprocdef;
  486. oldparse_only: boolean;
  487. member_blocktype : tblock_type;
  488. fields_allowed, is_classdef, classfields: boolean;
  489. vdoptions: tvar_dec_options;
  490. begin
  491. { empty record declaration ? }
  492. if (token=_SEMICOLON) then
  493. Exit;
  494. current_structdef.symtable.currentvisibility:=vis_public;
  495. fields_allowed:=true;
  496. is_classdef:=false;
  497. classfields:=false;
  498. member_blocktype:=bt_general;
  499. repeat
  500. case token of
  501. _TYPE :
  502. begin
  503. consume(_TYPE);
  504. member_blocktype:=bt_type;
  505. { local and anonymous records can not have inner types. skip top record symtable }
  506. if (current_structdef.objname^='') or
  507. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) then
  508. Message(parser_e_no_types_in_local_anonymous_records);
  509. end;
  510. _VAR :
  511. begin
  512. consume(_VAR);
  513. fields_allowed:=true;
  514. member_blocktype:=bt_general;
  515. classfields:=is_classdef;
  516. is_classdef:=false;
  517. end;
  518. _CONST:
  519. begin
  520. consume(_CONST);
  521. member_blocktype:=bt_const;
  522. end;
  523. _ID, _CASE, _OPERATOR :
  524. begin
  525. case idtoken of
  526. _PRIVATE :
  527. begin
  528. consume(_PRIVATE);
  529. current_structdef.symtable.currentvisibility:=vis_private;
  530. include(current_structdef.objectoptions,oo_has_private);
  531. fields_allowed:=true;
  532. is_classdef:=false;
  533. classfields:=false;
  534. member_blocktype:=bt_general;
  535. end;
  536. _PROTECTED :
  537. begin
  538. consume(_PROTECTED);
  539. current_structdef.symtable.currentvisibility:=vis_protected;
  540. include(current_structdef.objectoptions,oo_has_protected);
  541. fields_allowed:=true;
  542. is_classdef:=false;
  543. classfields:=false;
  544. member_blocktype:=bt_general;
  545. end;
  546. _PUBLIC :
  547. begin
  548. consume(_PUBLIC);
  549. current_structdef.symtable.currentvisibility:=vis_public;
  550. fields_allowed:=true;
  551. is_classdef:=false;
  552. classfields:=false;
  553. member_blocktype:=bt_general;
  554. end;
  555. _PUBLISHED :
  556. begin
  557. Message(parser_e_no_record_published);
  558. consume(_PUBLISHED);
  559. current_structdef.symtable.currentvisibility:=vis_published;
  560. fields_allowed:=true;
  561. is_classdef:=false;
  562. classfields:=false;
  563. member_blocktype:=bt_general;
  564. end;
  565. _STRICT :
  566. begin
  567. consume(_STRICT);
  568. if token=_ID then
  569. begin
  570. case idtoken of
  571. _PRIVATE:
  572. begin
  573. consume(_PRIVATE);
  574. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  575. include(current_structdef.objectoptions,oo_has_strictprivate);
  576. end;
  577. _PROTECTED:
  578. begin
  579. consume(_PROTECTED);
  580. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  581. include(current_structdef.objectoptions,oo_has_strictprotected);
  582. end;
  583. else
  584. message(parser_e_protected_or_private_expected);
  585. end;
  586. end
  587. else
  588. message(parser_e_protected_or_private_expected);
  589. fields_allowed:=true;
  590. is_classdef:=false;
  591. classfields:=false;
  592. member_blocktype:=bt_general;
  593. end
  594. else
  595. if is_classdef and (idtoken=_OPERATOR) then
  596. begin
  597. pd:=parse_record_method_dec(current_structdef,is_classdef);
  598. fields_allowed:=false;
  599. is_classdef:=false;
  600. end
  601. else
  602. begin
  603. if member_blocktype=bt_general then
  604. begin
  605. if (not fields_allowed)and(idtoken<>_CASE) then
  606. Message(parser_e_field_not_allowed_here);
  607. vdoptions:=[vd_record];
  608. if classfields then
  609. include(vdoptions,vd_class);
  610. read_record_fields(vdoptions,nil);
  611. end
  612. else if member_blocktype=bt_type then
  613. types_dec(true)
  614. else if member_blocktype=bt_const then
  615. consts_dec(true,true)
  616. else
  617. internalerror(201001110);
  618. end;
  619. end;
  620. end;
  621. _PROPERTY :
  622. begin
  623. struct_property_dec(is_classdef);
  624. fields_allowed:=false;
  625. is_classdef:=false;
  626. end;
  627. _CLASS:
  628. begin
  629. is_classdef:=false;
  630. { read class method/field/property }
  631. consume(_CLASS);
  632. { class modifier is only allowed for procedures, functions, }
  633. { constructors, destructors, fields and properties }
  634. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  635. not((token=_ID) and (idtoken=_OPERATOR)) then
  636. Message(parser_e_procedure_or_function_expected);
  637. is_classdef:=true;
  638. end;
  639. _PROCEDURE,
  640. _FUNCTION:
  641. begin
  642. pd:=parse_record_method_dec(current_structdef,is_classdef);
  643. fields_allowed:=false;
  644. is_classdef:=false;
  645. end;
  646. _CONSTRUCTOR :
  647. begin
  648. if not is_classdef then
  649. Message(parser_e_no_constructor_in_records);
  650. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  651. Message(parser_w_constructor_should_be_public);
  652. { only 1 class constructor is allowed }
  653. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  654. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  655. oldparse_only:=parse_only;
  656. parse_only:=true;
  657. if is_classdef then
  658. pd:=class_constructor_head(current_structdef)
  659. else
  660. pd:=constructor_head;
  661. parse_only:=oldparse_only;
  662. fields_allowed:=false;
  663. is_classdef:=false;
  664. end;
  665. _DESTRUCTOR :
  666. begin
  667. if not is_classdef then
  668. Message(parser_e_no_destructor_in_records);
  669. { only 1 class destructor is allowed }
  670. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  671. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  672. oldparse_only:=parse_only;
  673. parse_only:=true;
  674. if is_classdef then
  675. pd:=class_destructor_head(current_structdef)
  676. else
  677. pd:=destructor_head;
  678. parse_only:=oldparse_only;
  679. fields_allowed:=false;
  680. is_classdef:=false;
  681. end;
  682. _END :
  683. begin
  684. {$ifdef jvm}
  685. add_java_default_record_methods_intf(trecorddef(current_structdef));
  686. {$endif}
  687. if target_info.system in systems_typed_constants_node_init then
  688. add_typedconst_init_routine(current_structdef);
  689. consume(_END);
  690. break;
  691. end;
  692. else
  693. consume(_ID); { Give a ident expected message, like tp7 }
  694. end;
  695. until false;
  696. end;
  697. { reads a record declaration }
  698. function record_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  699. var
  700. old_current_structdef: tabstractrecorddef;
  701. old_current_genericdef,
  702. old_current_specializedef: tstoreddef;
  703. old_parse_generic: boolean;
  704. recst: trecordsymtable;
  705. begin
  706. old_current_structdef:=current_structdef;
  707. old_current_genericdef:=current_genericdef;
  708. old_current_specializedef:=current_specializedef;
  709. old_parse_generic:=parse_generic;
  710. current_genericdef:=nil;
  711. current_specializedef:=nil;
  712. { create recdef }
  713. if (n<>'') or
  714. not(target_info.system in systems_jvm) then
  715. begin
  716. recst:=trecordsymtable.create(n,current_settings.packrecords);
  717. { can't use recst.realname^ instead of n, because recst.realname is
  718. nil in case of an empty name }
  719. current_structdef:=trecorddef.create(n,recst);
  720. end
  721. else
  722. begin
  723. { for the JVM target records always need a name, because they are
  724. represented by a class }
  725. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),current_settings.packrecords);
  726. current_structdef:=trecorddef.create(recst.name^,recst);
  727. end;
  728. result:=current_structdef;
  729. { insert in symtablestack }
  730. symtablestack.push(recst);
  731. { usage of specialized type inside its generic template }
  732. if assigned(genericdef) then
  733. current_specializedef:=current_structdef
  734. { reject declaration of generic class inside generic class }
  735. else if assigned(genericlist) then
  736. current_genericdef:=current_structdef;
  737. { nested types of specializations are specializations as well }
  738. if assigned(old_current_structdef) and
  739. (df_specialization in old_current_structdef.defoptions) then
  740. include(current_structdef.defoptions,df_specialization);
  741. if assigned(old_current_structdef) and
  742. (df_generic in old_current_structdef.defoptions) then
  743. begin
  744. include(current_structdef.defoptions,df_generic);
  745. current_genericdef:=current_structdef;
  746. end;
  747. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  748. { when we are parsing a generic already then this is a generic as
  749. well }
  750. if old_parse_generic then
  751. include(current_structdef.defoptions, df_generic);
  752. parse_generic:=(df_generic in current_structdef.defoptions);
  753. { in non-Delphi modes we need a strict private symbol without type
  754. count and type parameters in the name to simply resolving }
  755. maybe_insert_generic_rename_symbol(n,genericlist);
  756. if m_advanced_records in current_settings.modeswitches then
  757. begin
  758. parse_record_members;
  759. end
  760. else
  761. begin
  762. read_record_fields([vd_record],nil);
  763. {$ifdef jvm}
  764. { we need a constructor to create temps, a deep copy helper, ... }
  765. add_java_default_record_methods_intf(trecorddef(current_structdef));
  766. {$endif}
  767. if target_info.system in systems_typed_constants_node_init then
  768. add_typedconst_init_routine(current_structdef);
  769. consume(_END);
  770. end;
  771. { don't keep track of procdefs in a separate list, because the
  772. compiler may add additional procdefs (e.g. property wrappers for
  773. the jvm backend) }
  774. insert_record_hidden_paras(trecorddef(current_structdef));
  775. { make the record size aligned }
  776. recst.addalignmentpadding;
  777. { restore symtable stack }
  778. symtablestack.pop(recst);
  779. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  780. Message(type_e_no_packed_inittable);
  781. { restore old state }
  782. parse_generic:=old_parse_generic;
  783. current_structdef:=old_current_structdef;
  784. current_genericdef:=old_current_genericdef;
  785. current_specializedef:=old_current_specializedef;
  786. end;
  787. { reads a type definition and returns a pointer to it }
  788. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  789. var
  790. pt : tnode;
  791. tt2 : tdef;
  792. aktenumdef : tenumdef;
  793. s : TIDString;
  794. l,v : TConstExprInt;
  795. oldpackrecords : longint;
  796. defpos,storepos : tfileposinfo;
  797. name: TIDString;
  798. procedure expr_type;
  799. var
  800. pt1,pt2 : tnode;
  801. lv,hv : TConstExprInt;
  802. old_block_type : tblock_type;
  803. dospecialize : boolean;
  804. newdef : tdef;
  805. begin
  806. old_block_type:=block_type;
  807. dospecialize:=false;
  808. { use of current parsed object:
  809. classes, objects, records can be used also in themself }
  810. if (token=_ID) then
  811. if try_parse_structdef_nested_type(def,current_structdef,false) then
  812. exit;
  813. { Generate a specialization in FPC mode? }
  814. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  815. { we can't accept a equal in type }
  816. pt1:=comp_expr(false,true);
  817. if not dospecialize and
  818. try_to_consume(_POINTPOINT) then
  819. begin
  820. { get high value of range }
  821. pt2:=comp_expr(false,false);
  822. { make both the same type or give an error. This is not
  823. done when both are integer values, because typecasting
  824. between -3200..3200 will result in a signed-unsigned
  825. conflict and give a range check error (PFV) }
  826. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  827. inserttypeconv(pt1,pt2.resultdef);
  828. { both must be evaluated to constants now }
  829. if (pt1.nodetype=ordconstn) and
  830. (pt2.nodetype=ordconstn) then
  831. begin
  832. lv:=tordconstnode(pt1).value;
  833. hv:=tordconstnode(pt2).value;
  834. { Check bounds }
  835. if hv<lv then
  836. message(parser_e_upper_lower_than_lower)
  837. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  838. message(type_e_cant_eval_constant_expr)
  839. else
  840. begin
  841. { All checks passed, create the new def }
  842. case pt1.resultdef.typ of
  843. enumdef :
  844. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  845. orddef :
  846. begin
  847. if is_char(pt1.resultdef) then
  848. def:=torddef.create(uchar,lv,hv)
  849. else
  850. if is_boolean(pt1.resultdef) then
  851. def:=torddef.create(pasbool8,lv,hv)
  852. else if is_signed(pt1.resultdef) then
  853. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  854. else
  855. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  856. end;
  857. end;
  858. end;
  859. end
  860. else
  861. Message(sym_e_error_in_type_def);
  862. pt2.free;
  863. end
  864. else
  865. begin
  866. { a simple type renaming or generic specialization }
  867. if (pt1.nodetype=typen) then
  868. begin
  869. def:=ttypenode(pt1).resultdef;
  870. { Delphi mode specialization? }
  871. if (m_delphi in current_settings.modeswitches) then
  872. dospecialize:=token=_LSHARPBRACKET
  873. else
  874. { in non-Delphi modes we might get a inline specialization
  875. without "specialize" or "<T>" of the same type we're
  876. currently parsing, so we need to handle that special }
  877. newdef:=nil;
  878. if not dospecialize and
  879. assigned(ttypenode(pt1).typesym) and
  880. (ttypenode(pt1).typesym.typ=typesym) and
  881. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  882. assigned(current_structdef) and
  883. (
  884. (
  885. not (m_delphi in current_settings.modeswitches) and
  886. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  887. (df_generic in current_structdef.defoptions) and
  888. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  889. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  890. ) or (
  891. { this could be a nested specialization which uses
  892. the type name of a surrounding generic to
  893. reference the specialization of said surrounding
  894. class }
  895. (df_specialization in current_structdef.defoptions) and
  896. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  897. )
  898. )
  899. then
  900. begin
  901. if assigned(newdef) then
  902. def:=newdef
  903. else
  904. def:=current_structdef;
  905. if assigned(def) then
  906. { handle nested types }
  907. post_comp_expr_gendef(def)
  908. else
  909. def:=generrordef;
  910. end;
  911. if dospecialize then
  912. begin
  913. generate_specialization(def,false,name,nil,'');
  914. { handle nested types }
  915. if assigned(def) then
  916. post_comp_expr_gendef(def);
  917. end
  918. else
  919. begin
  920. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  921. begin
  922. def:=current_specializedef
  923. end
  924. else if (def=current_genericdef) then
  925. begin
  926. def:=current_genericdef
  927. end
  928. else if (df_generic in def.defoptions) and
  929. { TODO : check once nested generics are allowed }
  930. not
  931. (
  932. parse_generic and
  933. (current_genericdef.typ in [recorddef,objectdef]) and
  934. (def.typ in [recorddef,objectdef]) and
  935. (
  936. { if both defs belong to the same generic (e.g. both are
  937. subtypes) then we must allow the usage }
  938. defs_belong_to_same_generic(def,current_genericdef) or
  939. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  940. declarations inside a generic }
  941. (
  942. (ttypenode(pt1).typesym<>nil) and
  943. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  944. )
  945. )
  946. )
  947. then
  948. begin
  949. Message(parser_e_no_generics_as_types);
  950. def:=generrordef;
  951. end
  952. else if is_classhelper(def) then
  953. begin
  954. Message(parser_e_no_category_as_types);
  955. def:=generrordef
  956. end
  957. end;
  958. end
  959. else
  960. Message(sym_e_error_in_type_def);
  961. end;
  962. pt1.free;
  963. block_type:=old_block_type;
  964. end;
  965. procedure set_dec;
  966. begin
  967. consume(_SET);
  968. consume(_OF);
  969. read_anon_type(tt2,true);
  970. if assigned(tt2) then
  971. begin
  972. case tt2.typ of
  973. { don't forget that min can be negativ PM }
  974. enumdef :
  975. if (tenumdef(tt2).min>=0) and
  976. (tenumdef(tt2).max<=255) then
  977. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  978. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  979. else
  980. Message(sym_e_ill_type_decl_set);
  981. orddef :
  982. begin
  983. if (torddef(tt2).ordtype<>uvoid) and
  984. (torddef(tt2).ordtype<>uwidechar) and
  985. (torddef(tt2).low>=0) then
  986. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  987. if Torddef(tt2).high>int64(high(byte)) then
  988. message(sym_e_ill_type_decl_set)
  989. else
  990. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  991. else
  992. Message(sym_e_ill_type_decl_set);
  993. end;
  994. else
  995. Message(sym_e_ill_type_decl_set);
  996. end;
  997. end
  998. else
  999. def:=generrordef;
  1000. end;
  1001. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  1002. var
  1003. lowval,
  1004. highval : TConstExprInt;
  1005. indexdef : tdef;
  1006. hdef : tdef;
  1007. arrdef : tarraydef;
  1008. procedure setdefdecl(def:tdef);
  1009. begin
  1010. case def.typ of
  1011. enumdef :
  1012. begin
  1013. lowval:=tenumdef(def).min;
  1014. highval:=tenumdef(def).max;
  1015. if (m_fpc in current_settings.modeswitches) and
  1016. (tenumdef(def).has_jumps) then
  1017. Message(type_e_array_index_enums_with_assign_not_possible);
  1018. indexdef:=def;
  1019. end;
  1020. orddef :
  1021. begin
  1022. if torddef(def).ordtype in [uchar,
  1023. u8bit,u16bit,
  1024. s8bit,s16bit,s32bit,
  1025. {$ifdef cpu64bitaddr}
  1026. u32bit,s64bit,
  1027. {$endif cpu64bitaddr}
  1028. pasbool8,pasbool16,pasbool32,pasbool64,
  1029. bool8bit,bool16bit,bool32bit,bool64bit,
  1030. uwidechar] then
  1031. begin
  1032. lowval:=torddef(def).low;
  1033. highval:=torddef(def).high;
  1034. indexdef:=def;
  1035. end
  1036. else
  1037. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1038. end;
  1039. else
  1040. Message(sym_e_error_in_type_def);
  1041. end;
  1042. end;
  1043. var
  1044. old_current_genericdef,
  1045. old_current_specializedef: tstoreddef;
  1046. first,
  1047. old_parse_generic: boolean;
  1048. begin
  1049. old_current_genericdef:=current_genericdef;
  1050. old_current_specializedef:=current_specializedef;
  1051. old_parse_generic:=parse_generic;
  1052. current_genericdef:=nil;
  1053. current_specializedef:=nil;
  1054. first:=true;
  1055. arrdef:=tarraydef.create(0,0,s32inttype);
  1056. consume(_ARRAY);
  1057. { usage of specialized type inside its generic template }
  1058. if assigned(genericdef) then
  1059. current_specializedef:=arrdef
  1060. { reject declaration of generic class inside generic class }
  1061. else if assigned(genericlist) then
  1062. current_genericdef:=arrdef;
  1063. symtablestack.push(arrdef.symtable);
  1064. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1065. { there are two possibilties for the following to be true:
  1066. * the array declaration itself is generic
  1067. * the array is declared inside a generic
  1068. in both cases we need "parse_generic" and "current_genericdef"
  1069. so that e.g. specializations of another generic inside the
  1070. current generic can be used (either inline ones or "type" ones) }
  1071. parse_generic:=(df_generic in arrdef.defoptions) or old_parse_generic;
  1072. if parse_generic and not assigned(current_genericdef) then
  1073. current_genericdef:=old_current_genericdef;
  1074. { open array? }
  1075. if try_to_consume(_LECKKLAMMER) then
  1076. begin
  1077. { defaults }
  1078. indexdef:=generrordef;
  1079. { use defaults which don't overflow the compiler }
  1080. lowval:=0;
  1081. highval:=0;
  1082. repeat
  1083. { read the expression and check it, check apart if the
  1084. declaration is an enum declaration because that needs to
  1085. be parsed by readtype (PFV) }
  1086. if token=_LKLAMMER then
  1087. begin
  1088. read_anon_type(hdef,true);
  1089. setdefdecl(hdef);
  1090. end
  1091. else
  1092. begin
  1093. pt:=expr(true);
  1094. if pt.nodetype=typen then
  1095. setdefdecl(pt.resultdef)
  1096. else
  1097. begin
  1098. if pt.nodetype=rangen then
  1099. begin
  1100. { pure ordconstn expressions can be checked for
  1101. generics as well, but don't give an error in case
  1102. of parsing a generic if that isn't yet the case }
  1103. if (trangenode(pt).left.nodetype=ordconstn) and
  1104. (trangenode(pt).right.nodetype=ordconstn) then
  1105. begin
  1106. { make both the same type or give an error. This is not
  1107. done when both are integer values, because typecasting
  1108. between -3200..3200 will result in a signed-unsigned
  1109. conflict and give a range check error (PFV) }
  1110. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1111. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1112. lowval:=tordconstnode(trangenode(pt).left).value;
  1113. highval:=tordconstnode(trangenode(pt).right).value;
  1114. if highval<lowval then
  1115. begin
  1116. Message(parser_e_array_lower_less_than_upper_bound);
  1117. highval:=lowval;
  1118. end
  1119. else if (lowval<int64(low(asizeint))) or
  1120. (highval>high(asizeint)) then
  1121. begin
  1122. Message(parser_e_array_range_out_of_bounds);
  1123. lowval :=0;
  1124. highval:=0;
  1125. end;
  1126. if is_integer(trangenode(pt).left.resultdef) then
  1127. range_to_type(lowval,highval,indexdef)
  1128. else
  1129. indexdef:=trangenode(pt).left.resultdef;
  1130. end
  1131. else
  1132. if not parse_generic then
  1133. Message(type_e_cant_eval_constant_expr);
  1134. end
  1135. else
  1136. Message(sym_e_error_in_type_def)
  1137. end;
  1138. pt.free;
  1139. end;
  1140. { if we are not at the first dimension, add the new arrray
  1141. as element of the existing array, otherwise modify the existing array }
  1142. if not(first) then
  1143. begin
  1144. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1145. { push new symtable }
  1146. symtablestack.pop(arrdef.symtable);
  1147. arrdef:=tarraydef(arrdef.elementdef);
  1148. symtablestack.push(arrdef.symtable);
  1149. end
  1150. else
  1151. begin
  1152. arrdef.lowrange:=lowval.svalue;
  1153. arrdef.highrange:=highval.svalue;
  1154. arrdef.rangedef:=indexdef;
  1155. def:=arrdef;
  1156. first:=false;
  1157. end;
  1158. if is_packed then
  1159. include(arrdef.arrayoptions,ado_IsBitPacked);
  1160. if token=_COMMA then
  1161. consume(_COMMA)
  1162. else
  1163. break;
  1164. until false;
  1165. consume(_RECKKLAMMER);
  1166. end
  1167. else
  1168. begin
  1169. if is_packed then
  1170. Message(parser_e_packed_dynamic_open_array);
  1171. arrdef.lowrange:=0;
  1172. arrdef.highrange:=-1;
  1173. arrdef.rangedef:=s32inttype;
  1174. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1175. def:=arrdef;
  1176. end;
  1177. consume(_OF);
  1178. read_anon_type(tt2,true);
  1179. { set element type of the last array definition }
  1180. if assigned(arrdef) then
  1181. begin
  1182. symtablestack.pop(arrdef.symtable);
  1183. arrdef.elementdef:=tt2;
  1184. if is_packed and
  1185. is_managed_type(tt2) then
  1186. Message(type_e_no_packed_inittable);
  1187. end;
  1188. { restore old state }
  1189. parse_generic:=old_parse_generic;
  1190. current_genericdef:=old_current_genericdef;
  1191. current_specializedef:=old_current_specializedef;
  1192. end;
  1193. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1194. var
  1195. is_func:boolean;
  1196. pd:tabstractprocdef;
  1197. newtype:ttypesym;
  1198. old_current_genericdef,
  1199. old_current_specializedef: tstoreddef;
  1200. old_parse_generic: boolean;
  1201. begin
  1202. old_current_genericdef:=current_genericdef;
  1203. old_current_specializedef:=current_specializedef;
  1204. old_parse_generic:=parse_generic;
  1205. current_genericdef:=nil;
  1206. current_specializedef:=nil;
  1207. is_func:=(token=_FUNCTION);
  1208. consume(token);
  1209. pd:=tprocvardef.create(normal_function_level);
  1210. { usage of specialized type inside its generic template }
  1211. if assigned(genericdef) then
  1212. current_specializedef:=pd
  1213. { reject declaration of generic class inside generic class }
  1214. else if assigned(genericlist) then
  1215. current_genericdef:=pd;
  1216. symtablestack.push(pd.parast);
  1217. insert_generic_parameter_types(pd,genericdef,genericlist);
  1218. { there are two possibilties for the following to be true:
  1219. * the procvar declaration itself is generic
  1220. * the procvar is declared inside a generic
  1221. in both cases we need "parse_generic" and "current_genericdef"
  1222. so that e.g. specializations of another generic inside the
  1223. current generic can be used (either inline ones or "type" ones) }
  1224. parse_generic:=(df_generic in pd.defoptions) or old_parse_generic;
  1225. if parse_generic and not assigned(current_genericdef) then
  1226. current_genericdef:=old_current_genericdef;
  1227. { don't allow to add defs to the symtable - use it for type param search only }
  1228. tparasymtable(pd.parast).readonly:=true;
  1229. if token=_LKLAMMER then
  1230. parse_parameter_dec(pd);
  1231. if is_func then
  1232. begin
  1233. consume(_COLON);
  1234. single_type(pd.returndef,[]);
  1235. end;
  1236. if try_to_consume(_OF) then
  1237. begin
  1238. consume(_OBJECT);
  1239. include(pd.procoptions,po_methodpointer);
  1240. end
  1241. else if (m_nested_procvars in current_settings.modeswitches) and
  1242. try_to_consume(_IS) then
  1243. begin
  1244. consume(_NESTED);
  1245. pd.parast.symtablelevel:=normal_function_level+1;
  1246. pd.check_mark_as_nested;
  1247. end;
  1248. symtablestack.pop(pd.parast);
  1249. tparasymtable(pd.parast).readonly:=false;
  1250. result:=pd;
  1251. { possible proc directives }
  1252. if parseprocvardir then
  1253. begin
  1254. if check_proc_directive(true) then
  1255. begin
  1256. newtype:=ttypesym.create('unnamed',result);
  1257. parse_var_proc_directives(tsym(newtype));
  1258. newtype.typedef:=nil;
  1259. result.typesym:=nil;
  1260. newtype.free;
  1261. end;
  1262. { Add implicit hidden parameters and function result }
  1263. handle_calling_convention(pd);
  1264. end;
  1265. { restore old state }
  1266. parse_generic:=old_parse_generic;
  1267. current_genericdef:=old_current_genericdef;
  1268. current_specializedef:=old_current_specializedef;
  1269. end;
  1270. const
  1271. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1272. SingleTypeOptionsIsDelphi:array[Boolean] of TSingleTypeOptions = ([],[stoAllowSpecialization]);
  1273. var
  1274. p : tnode;
  1275. hdef : tdef;
  1276. enumdupmsg, first, is_specialize : boolean;
  1277. oldlocalswitches : tlocalswitches;
  1278. bitpacking: boolean;
  1279. stitem: psymtablestackitem;
  1280. sym: tsym;
  1281. st: tsymtable;
  1282. begin
  1283. def:=nil;
  1284. if assigned(newsym) then
  1285. name:=newsym.RealName
  1286. else
  1287. name:='';
  1288. case token of
  1289. _STRING,_FILE:
  1290. begin
  1291. single_type(def,[stoAllowTypeDef]);
  1292. end;
  1293. _LKLAMMER:
  1294. begin
  1295. consume(_LKLAMMER);
  1296. first:=true;
  1297. { allow negativ value_str }
  1298. l:=int64(-1);
  1299. enumdupmsg:=false;
  1300. { check that we are not adding an enum from specialization
  1301. we can't just use current_specializedef because of inner types
  1302. like specialize array of record }
  1303. is_specialize:=false;
  1304. stitem:=symtablestack.stack;
  1305. while assigned(stitem) do
  1306. begin
  1307. { check records, classes and arrays because they can be specialized }
  1308. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1309. begin
  1310. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1311. stitem:=stitem^.next;
  1312. end
  1313. else
  1314. break;
  1315. end;
  1316. if not is_specialize then
  1317. aktenumdef:=tenumdef.create
  1318. else
  1319. aktenumdef:=nil;
  1320. repeat
  1321. { if it is a specialization then search the first enum member
  1322. and get the member owner instead of just created enumdef }
  1323. if not assigned(aktenumdef) then
  1324. begin
  1325. searchsym(pattern,sym,st);
  1326. if sym.typ=enumsym then
  1327. aktenumdef:=tenumsym(sym).definition
  1328. else
  1329. internalerror(201101021);
  1330. end;
  1331. s:=orgpattern;
  1332. defpos:=current_tokenpos;
  1333. consume(_ID);
  1334. { only allow assigning of specific numbers under fpc mode }
  1335. if not(m_tp7 in current_settings.modeswitches) and
  1336. (
  1337. { in fpc mode also allow := to be compatible
  1338. with previous 1.0.x versions }
  1339. ((m_fpc in current_settings.modeswitches) and
  1340. try_to_consume(_ASSIGNMENT)) or
  1341. try_to_consume(_EQ)
  1342. ) then
  1343. begin
  1344. oldlocalswitches:=current_settings.localswitches;
  1345. include(current_settings.localswitches,cs_allow_enum_calc);
  1346. p:=comp_expr(true,false);
  1347. current_settings.localswitches:=oldlocalswitches;
  1348. if (p.nodetype=ordconstn) then
  1349. begin
  1350. { we expect an integer or an enum of the
  1351. same type }
  1352. if is_integer(p.resultdef) or
  1353. is_char(p.resultdef) or
  1354. equal_defs(p.resultdef,aktenumdef) then
  1355. v:=tordconstnode(p).value
  1356. else
  1357. IncompatibleTypes(p.resultdef,s32inttype);
  1358. end
  1359. else
  1360. Message(parser_e_illegal_expression);
  1361. p.free;
  1362. { please leave that a note, allows type save }
  1363. { declarations in the win32 units ! }
  1364. if (not first) and (v<=l) and (not enumdupmsg) then
  1365. begin
  1366. Message(parser_n_duplicate_enum);
  1367. enumdupmsg:=true;
  1368. end;
  1369. l:=v;
  1370. end
  1371. else
  1372. inc(l.svalue);
  1373. first:=false;
  1374. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1375. if not is_specialize then
  1376. begin
  1377. storepos:=current_tokenpos;
  1378. current_tokenpos:=defpos;
  1379. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1380. if not (cs_scopedenums in current_settings.localswitches) then
  1381. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1382. current_tokenpos:=storepos;
  1383. end;
  1384. until not try_to_consume(_COMMA);
  1385. def:=aktenumdef;
  1386. consume(_RKLAMMER);
  1387. {$ifdef jvm}
  1388. jvm_maybe_create_enum_class(name,def);
  1389. {$endif}
  1390. end;
  1391. _ARRAY:
  1392. begin
  1393. array_dec(false,genericdef,genericlist);
  1394. end;
  1395. _SET:
  1396. begin
  1397. set_dec;
  1398. end;
  1399. _CARET:
  1400. begin
  1401. consume(_CARET);
  1402. single_type(tt2,
  1403. SingleTypeOptionsInTypeBlock[block_type=bt_type]+
  1404. SingleTypeOptionsIsDelphi[m_delphi in current_settings.modeswitches]
  1405. );
  1406. { in case of e.g. var or const sections we need to especially
  1407. check that we don't use a generic dummy symbol }
  1408. if (block_type<>bt_type) and
  1409. (tt2.typ=undefineddef) and
  1410. assigned(tt2.typesym) and
  1411. (sp_generic_dummy in tt2.typesym.symoptions) then
  1412. Message(parser_e_no_generics_as_types);
  1413. { don't use getpointerdef() here, since this is a type
  1414. declaration (-> must create new typedef) }
  1415. def:=tpointerdef.create(tt2);
  1416. if tt2.typ=forwarddef then
  1417. current_module.checkforwarddefs.add(def);
  1418. end;
  1419. _RECORD:
  1420. begin
  1421. consume(token);
  1422. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1423. begin
  1424. consume(_HELPER);
  1425. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1426. end
  1427. else
  1428. def:=record_dec(name,genericdef,genericlist);
  1429. end;
  1430. _PACKED,
  1431. _BITPACKED:
  1432. begin
  1433. bitpacking :=
  1434. (cs_bitpacking in current_settings.localswitches) or
  1435. (token = _BITPACKED);
  1436. consume(token);
  1437. if token=_ARRAY then
  1438. array_dec(bitpacking,genericdef,genericlist)
  1439. else if token=_SET then
  1440. set_dec
  1441. else if token=_FILE then
  1442. single_type(def,[stoAllowTypeDef])
  1443. else
  1444. begin
  1445. oldpackrecords:=current_settings.packrecords;
  1446. if (not bitpacking) or
  1447. (token in [_CLASS,_OBJECT]) then
  1448. current_settings.packrecords:=1
  1449. else
  1450. current_settings.packrecords:=bit_alignment;
  1451. case token of
  1452. _CLASS :
  1453. begin
  1454. consume(_CLASS);
  1455. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1456. end;
  1457. _OBJECT :
  1458. begin
  1459. consume(_OBJECT);
  1460. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1461. end;
  1462. else begin
  1463. consume(_RECORD);
  1464. def:=record_dec(name,genericdef,genericlist);
  1465. end;
  1466. end;
  1467. current_settings.packrecords:=oldpackrecords;
  1468. end;
  1469. end;
  1470. _DISPINTERFACE :
  1471. begin
  1472. { need extra check here since interface is a keyword
  1473. in all pascal modes }
  1474. if not(m_class in current_settings.modeswitches) then
  1475. Message(parser_f_need_objfpc_or_delphi_mode);
  1476. consume(token);
  1477. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1478. end;
  1479. _CLASS :
  1480. begin
  1481. consume(token);
  1482. { Delphi only allows class of in type blocks }
  1483. if (token=_OF) and
  1484. (
  1485. not(m_delphi in current_settings.modeswitches) or
  1486. (block_type=bt_type)
  1487. ) then
  1488. begin
  1489. consume(_OF);
  1490. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1491. if is_class(hdef) or
  1492. is_objcclass(hdef) or
  1493. is_javaclass(hdef) then
  1494. def:=tclassrefdef.create(hdef)
  1495. else
  1496. if hdef.typ=forwarddef then
  1497. begin
  1498. def:=tclassrefdef.create(hdef);
  1499. current_module.checkforwarddefs.add(def);
  1500. end
  1501. else
  1502. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1503. end
  1504. else
  1505. if (idtoken=_HELPER) then
  1506. begin
  1507. consume(_HELPER);
  1508. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1509. end
  1510. else
  1511. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1512. end;
  1513. _CPPCLASS :
  1514. begin
  1515. consume(token);
  1516. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1517. end;
  1518. _OBJCCLASS :
  1519. begin
  1520. if not(m_objectivec1 in current_settings.modeswitches) then
  1521. Message(parser_f_need_objc);
  1522. consume(token);
  1523. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1524. end;
  1525. _INTERFACE :
  1526. begin
  1527. { need extra check here since interface is a keyword
  1528. in all pascal modes }
  1529. if not(m_class in current_settings.modeswitches) then
  1530. Message(parser_f_need_objfpc_or_delphi_mode);
  1531. consume(token);
  1532. case current_settings.interfacetype of
  1533. it_interfacecom:
  1534. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1535. it_interfacecorba:
  1536. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1537. it_interfacejava:
  1538. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1539. else
  1540. internalerror(2010122612);
  1541. end;
  1542. end;
  1543. _OBJCPROTOCOL :
  1544. begin
  1545. if not(m_objectivec1 in current_settings.modeswitches) then
  1546. Message(parser_f_need_objc);
  1547. consume(token);
  1548. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1549. end;
  1550. _OBJCCATEGORY :
  1551. begin
  1552. if not(m_objectivec1 in current_settings.modeswitches) then
  1553. Message(parser_f_need_objc);
  1554. consume(token);
  1555. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1556. end;
  1557. _OBJECT :
  1558. begin
  1559. consume(token);
  1560. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1561. end;
  1562. _PROCEDURE,
  1563. _FUNCTION:
  1564. begin
  1565. def:=procvar_dec(genericdef,genericlist);
  1566. {$ifdef jvm}
  1567. jvm_create_procvar_class(name,def);
  1568. {$endif}
  1569. end;
  1570. else
  1571. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1572. begin
  1573. consume(_KLAMMERAFFE);
  1574. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1575. def:=tpointerdef.create(tt2);
  1576. if tt2.typ=forwarddef then
  1577. current_module.checkforwarddefs.add(def);
  1578. end
  1579. else
  1580. expr_type;
  1581. end;
  1582. if def=nil then
  1583. def:=generrordef;
  1584. end;
  1585. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1586. begin
  1587. read_named_type(def,nil,nil,nil,parseprocvardir);
  1588. end;
  1589. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  1590. var
  1591. i : longint;
  1592. def : tdef;
  1593. vmtwriter : TVMTWriter;
  1594. begin
  1595. {$ifdef jvm}
  1596. { no Delphi-style RTTI }
  1597. exit;
  1598. {$endif jvm}
  1599. for i:=0 to st.DefList.Count-1 do
  1600. begin
  1601. def:=tdef(st.DefList[i]);
  1602. case def.typ of
  1603. recorddef :
  1604. write_persistent_type_info(trecorddef(def).symtable,is_global);
  1605. objectdef :
  1606. begin
  1607. { Skip generics and forward defs }
  1608. if (df_generic in def.defoptions) or
  1609. (oo_is_forward in tobjectdef(def).objectoptions) then
  1610. continue;
  1611. write_persistent_type_info(tobjectdef(def).symtable,is_global);
  1612. { Write also VMT if not done yet }
  1613. if not(ds_vmt_written in def.defstates) then
  1614. begin
  1615. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1616. if is_interface(tobjectdef(def)) then
  1617. vmtwriter.writeinterfaceids;
  1618. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1619. vmtwriter.writevmt;
  1620. vmtwriter.free;
  1621. include(def.defstates,ds_vmt_written);
  1622. end;
  1623. end;
  1624. procdef :
  1625. begin
  1626. if assigned(tprocdef(def).localst) and
  1627. (tprocdef(def).localst.symtabletype=localsymtable) then
  1628. write_persistent_type_info(tprocdef(def).localst,false);
  1629. if assigned(tprocdef(def).parast) then
  1630. write_persistent_type_info(tprocdef(def).parast,false);
  1631. end;
  1632. end;
  1633. { generate always persistent tables for types in the interface so it can
  1634. be reused in other units and give always the same pointer location. }
  1635. { Init }
  1636. if (
  1637. assigned(def.typesym) and
  1638. is_global and
  1639. not is_objc_class_or_protocol(def)
  1640. ) or
  1641. is_managed_type(def) or
  1642. (ds_init_table_used in def.defstates) then
  1643. RTTIWriter.write_rtti(def,initrtti);
  1644. { RTTI }
  1645. if (
  1646. assigned(def.typesym) and
  1647. is_global and
  1648. not is_objc_class_or_protocol(def)
  1649. ) or
  1650. (ds_rtti_table_used in def.defstates) then
  1651. RTTIWriter.write_rtti(def,fullrtti);
  1652. end;
  1653. end;
  1654. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1655. var
  1656. sstate: tscannerstate;
  1657. pd: tprocdef;
  1658. begin
  1659. replace_scanner('tcinit_routine',sstate);
  1660. { the typed constant initialization code is called from the class
  1661. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1662. know yet whether that will be necessary, because there may be
  1663. typed constants inside method bodies -> always force the addition
  1664. of a class constructor.
  1665. We cannot directly add the typed constant initialisations to the
  1666. class constructor, because when it's parsed not all method bodies
  1667. are necessarily already parsed }
  1668. pd:=def.find_procdef_bytype(potype_class_constructor);
  1669. { the class constructor }
  1670. if not assigned(pd) then
  1671. begin
  1672. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1673. pd.synthetickind:=tsk_empty
  1674. else
  1675. internalerror(2011040206);
  1676. end;
  1677. { the initialisation helper }
  1678. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1679. pd.synthetickind:=tsk_tcinit
  1680. else
  1681. internalerror(2011040207);
  1682. restore_scanner(sstate);
  1683. end;
  1684. end.