ptype.pas 73 KB

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