ptype.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. var
  24. { hack, which allows to use the current parsed }
  25. { object type as function argument type }
  26. testcurobject : byte;
  27. procedure resolve_forward_types;
  28. { reads a type identifier }
  29. procedure id_type(var def : tdef;isforwarddef:boolean);
  30. { reads a string, file type or a type identifier }
  31. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  32. { reads any type declaration, where the resulting type will get name as type identifier }
  33. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  34. { reads any type declaration }
  35. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  36. { generate persistent type information like VMT, RTTI and inittables }
  37. procedure write_persistent_type_info(st:tsymtable);
  38. implementation
  39. uses
  40. { common }
  41. cutils,
  42. { global }
  43. globals,tokens,verbose,constexp,
  44. systems,
  45. { target }
  46. paramgr,procinfo,
  47. { symtable }
  48. symconst,symsym,symtable,
  49. defutil,defcmp,
  50. { modules }
  51. fmodule,
  52. { pass 1 }
  53. node,ncgrtti,nobj,
  54. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  55. { parser }
  56. scanner,
  57. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  58. procedure resolve_forward_types;
  59. var
  60. i: longint;
  61. hpd,
  62. def : tdef;
  63. srsym : tsym;
  64. srsymtable : TSymtable;
  65. hs : string;
  66. begin
  67. for i:=0 to current_module.checkforwarddefs.Count-1 do
  68. begin
  69. def:=tdef(current_module.checkforwarddefs[i]);
  70. case def.typ of
  71. pointerdef,
  72. classrefdef :
  73. begin
  74. { classrefdef inherits from pointerdef }
  75. hpd:=tabstractpointerdef(def).pointeddef;
  76. { still a forward def ? }
  77. if hpd.typ=forwarddef then
  78. begin
  79. { try to resolve the forward }
  80. if not assigned(tforwarddef(hpd).tosymname) then
  81. internalerror(200211201);
  82. hs:=tforwarddef(hpd).tosymname^;
  83. searchsym(upper(hs),srsym,srsymtable);
  84. { we don't need the forwarddef anymore, dispose it }
  85. hpd.free;
  86. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  87. { was a type sym found ? }
  88. if assigned(srsym) and
  89. (srsym.typ=typesym) then
  90. begin
  91. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  92. { avoid wrong unused warnings web bug 801 PM }
  93. inc(ttypesym(srsym).refs);
  94. { we need a class type for classrefdef }
  95. if (def.typ=classrefdef) and
  96. not(is_class(ttypesym(srsym).typedef)) and
  97. not(is_objcclass(ttypesym(srsym).typedef)) then
  98. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  99. end
  100. else
  101. begin
  102. Message1(sym_e_forward_type_not_resolved,hs);
  103. { try to recover }
  104. tabstractpointerdef(def).pointeddef:=generrordef;
  105. end;
  106. end;
  107. end;
  108. objectdef :
  109. begin
  110. { give an error as the implementation may follow in an
  111. other type block which is allowed by FPC modes }
  112. if not(m_fpc in current_settings.modeswitches) and
  113. (oo_is_forward in tobjectdef(def).objectoptions) then
  114. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  115. end;
  116. else
  117. internalerror(200811071);
  118. end;
  119. end;
  120. current_module.checkforwarddefs.clear;
  121. end;
  122. procedure generate_specialization(var tt:tdef);
  123. var
  124. st : TSymtable;
  125. srsym : tsym;
  126. pt2 : tnode;
  127. first,
  128. err : boolean;
  129. i : longint;
  130. sym : tsym;
  131. genericdef : tstoreddef;
  132. generictype : ttypesym;
  133. generictypelist : TFPObjectList;
  134. oldsymtablestack : tsymtablestack;
  135. hmodule : tmodule;
  136. pu : tused_unit;
  137. uspecializename,
  138. specializename : string;
  139. vmtbuilder : TVMTBuilder;
  140. onlyparsepara : boolean;
  141. specializest : tsymtable;
  142. begin
  143. { retrieve generic def that we are going to replace }
  144. genericdef:=tstoreddef(tt);
  145. tt:=nil;
  146. onlyparsepara:=false;
  147. if not(df_generic in genericdef.defoptions) then
  148. begin
  149. Message(parser_e_special_onlygenerics);
  150. tt:=generrordef;
  151. onlyparsepara:=true;
  152. end;
  153. { only need to record the tokens, then we don't know the type yet ... }
  154. if parse_generic then
  155. begin
  156. { ... but we have to insert a def into the symtable else the deflist
  157. of generic and specialization might not be equally sized which
  158. is later assumed }
  159. tt:=tundefineddef.create;
  160. onlyparsepara:=true;
  161. end;
  162. { Only parse the parameters for recovery or
  163. for recording in genericbuf }
  164. if onlyparsepara then
  165. begin
  166. consume(_LSHARPBRACKET);
  167. repeat
  168. pt2:=factor(false);
  169. pt2.free;
  170. until not try_to_consume(_COMMA);
  171. consume(_RSHARPBRACKET);
  172. exit;
  173. end;
  174. consume(_LSHARPBRACKET);
  175. { Parse generic parameters, for each undefineddef in the symtable of
  176. the genericdef we need to have a new def }
  177. err:=false;
  178. first:=true;
  179. generictypelist:=TFPObjectList.create(false);
  180. case genericdef.typ of
  181. procdef :
  182. st:=genericdef.GetSymtable(gs_para);
  183. objectdef,
  184. recorddef :
  185. st:=genericdef.GetSymtable(gs_record);
  186. end;
  187. if not assigned(st) then
  188. internalerror(200511182);
  189. { Parse type parameters }
  190. if not assigned(genericdef.typesym) then
  191. internalerror(200710173);
  192. specializename:=genericdef.typesym.realname;
  193. for i:=0 to st.SymList.Count-1 do
  194. begin
  195. sym:=tsym(st.SymList[i]);
  196. if (sp_generic_para in sym.symoptions) then
  197. begin
  198. if not first then
  199. consume(_COMMA)
  200. else
  201. first:=false;
  202. pt2:=factor(false);
  203. if pt2.nodetype=typen then
  204. begin
  205. if df_generic in pt2.resultdef.defoptions then
  206. Message(parser_e_no_generics_as_params);
  207. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  208. generictypelist.add(generictype);
  209. if not assigned(pt2.resultdef.typesym) then
  210. internalerror(200710172);
  211. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  212. end
  213. else
  214. begin
  215. Message(type_e_type_id_expected);
  216. err:=true;
  217. end;
  218. pt2.free;
  219. end;
  220. end;
  221. uspecializename:=upper(specializename);
  222. { force correct error location if too much type parameters are passed }
  223. if token<>_RSHARPBRACKET then
  224. consume(_RSHARPBRACKET);
  225. { Special case if we are referencing the current defined object }
  226. if assigned(current_objectdef) and
  227. (current_objectdef.objname^=uspecializename) then
  228. tt:=current_objectdef;
  229. { for units specializations can already be needed in the interface, therefor we
  230. will use the global symtable. Programs don't have a globalsymtable and there we
  231. use the localsymtable }
  232. if current_module.is_unit then
  233. specializest:=current_module.globalsymtable
  234. else
  235. specializest:=current_module.localsymtable;
  236. { Can we reuse an already specialized type? }
  237. if not assigned(tt) then
  238. begin
  239. srsym:=tsym(specializest.find(uspecializename));
  240. if assigned(srsym) then
  241. begin
  242. if srsym.typ<>typesym then
  243. internalerror(200710171);
  244. tt:=ttypesym(srsym).typedef;
  245. end;
  246. end;
  247. if not assigned(tt) then
  248. begin
  249. { Setup symtablestack at definition time
  250. to get types right, however this is not perfect, we should probably record
  251. the resolved symbols }
  252. oldsymtablestack:=symtablestack;
  253. symtablestack:=tsymtablestack.create;
  254. if not assigned(genericdef) then
  255. internalerror(200705151);
  256. hmodule:=find_module_from_symtable(genericdef.owner);
  257. if hmodule=nil then
  258. internalerror(200705152);
  259. pu:=tused_unit(hmodule.used_units.first);
  260. while assigned(pu) do
  261. begin
  262. if not assigned(pu.u.globalsymtable) then
  263. internalerror(200705153);
  264. symtablestack.push(pu.u.globalsymtable);
  265. pu:=tused_unit(pu.next);
  266. end;
  267. if assigned(hmodule.globalsymtable) then
  268. symtablestack.push(hmodule.globalsymtable);
  269. { hacky, but necessary to insert the newly generated class properly }
  270. symtablestack.push(oldsymtablestack.top);
  271. { Reparse the original type definition }
  272. if not err then
  273. begin
  274. { First a new typesym so we can reuse this specialization and
  275. references to this specialization can be handled }
  276. srsym:=ttypesym.create(specializename,generrordef);
  277. specializest.insert(srsym);
  278. if not assigned(genericdef.generictokenbuf) then
  279. internalerror(200511171);
  280. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  281. read_named_type(tt,specializename,genericdef,generictypelist,false);
  282. ttypesym(srsym).typedef:=tt;
  283. tt.typesym:=srsym;
  284. { Consume the semicolon if it is also recorded }
  285. try_to_consume(_SEMICOLON);
  286. { Build VMT indexes for classes }
  287. if (tt.typ=objectdef) then
  288. begin
  289. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  290. vmtbuilder.generate_vmt;
  291. vmtbuilder.free;
  292. end;
  293. end;
  294. { Restore symtablestack }
  295. symtablestack.free;
  296. symtablestack:=oldsymtablestack;
  297. end;
  298. generictypelist.free;
  299. consume(_RSHARPBRACKET);
  300. end;
  301. procedure id_type(var def : tdef;isforwarddef:boolean);
  302. { reads a type definition }
  303. { to a appropriating tdef, s gets the name of }
  304. { the type to allow name mangling }
  305. var
  306. is_unit_specific : boolean;
  307. pos : tfileposinfo;
  308. srsym : tsym;
  309. srsymtable : TSymtable;
  310. s,sorg : TIDString;
  311. t : ttoken;
  312. begin
  313. s:=pattern;
  314. sorg:=orgpattern;
  315. pos:=current_tokenpos;
  316. { use of current parsed object:
  317. - classes can be used also in classes
  318. - objects can be parameters }
  319. if assigned(current_objectdef) and
  320. (current_objectdef.objname^=pattern) and
  321. (
  322. (testcurobject=2) or
  323. is_class_or_interface_or_objc(current_objectdef)
  324. )then
  325. begin
  326. consume(_ID);
  327. def:=current_objectdef;
  328. exit;
  329. end;
  330. { Use the special searchsym_type that ignores records,objects and
  331. parameters }
  332. searchsym_type(s,srsym,srsymtable);
  333. { handle unit specification like System.Writeln }
  334. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  335. consume(t);
  336. { Types are first defined with an error def before assigning
  337. the real type so check if it's an errordef. if so then
  338. give an error. Only check for typesyms in the current symbol
  339. table as forwarddef are not resolved directly }
  340. if assigned(srsym) and
  341. (srsym.typ=typesym) and
  342. (ttypesym(srsym).typedef.typ=errordef) then
  343. begin
  344. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  345. def:=generrordef;
  346. exit;
  347. end;
  348. { are we parsing a possible forward def ? }
  349. if isforwarddef and
  350. not(is_unit_specific) then
  351. begin
  352. def:=tforwarddef.create(sorg,pos);
  353. exit;
  354. end;
  355. { unknown sym ? }
  356. if not assigned(srsym) then
  357. begin
  358. Message1(sym_e_id_not_found,sorg);
  359. def:=generrordef;
  360. exit;
  361. end;
  362. { type sym ? }
  363. if (srsym.typ<>typesym) then
  364. begin
  365. Message(type_e_type_id_expected);
  366. def:=generrordef;
  367. exit;
  368. end;
  369. { Give an error when referring to an errordef }
  370. if (ttypesym(srsym).typedef.typ=errordef) then
  371. begin
  372. Message(sym_e_error_in_type_def);
  373. def:=generrordef;
  374. exit;
  375. end;
  376. def:=ttypesym(srsym).typedef;
  377. end;
  378. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  379. var
  380. t2 : tdef;
  381. dospecialize,
  382. again : boolean;
  383. begin
  384. dospecialize:=false;
  385. repeat
  386. again:=false;
  387. case token of
  388. _STRING:
  389. string_dec(def,allowtypedef);
  390. _FILE:
  391. begin
  392. consume(_FILE);
  393. if (token=_OF) then
  394. begin
  395. if not(allowtypedef) then
  396. Message(parser_e_no_local_para_def);
  397. consume(_OF);
  398. single_type(t2,false,false);
  399. def:=tfiledef.createtyped(t2);
  400. end
  401. else
  402. def:=cfiletype;
  403. end;
  404. _ID:
  405. begin
  406. if try_to_consume(_SPECIALIZE) then
  407. begin
  408. dospecialize:=true;
  409. again:=true;
  410. end
  411. else
  412. begin
  413. id_type(def,isforwarddef);
  414. { handle types inside classes for generics, e.g. TNode.TLongint }
  415. while (token=_POINT) do
  416. begin
  417. if parse_generic then
  418. begin
  419. consume(_POINT);
  420. consume(_ID);
  421. end
  422. else if ((def.typ=objectdef) and (df_specialization in def.defoptions)) then
  423. begin
  424. symtablestack.push(tobjectdef(def).symtable);
  425. consume(_POINT);
  426. id_type(t2,isforwarddef);
  427. symtablestack.pop(tobjectdef(def).symtable);
  428. def:=t2;
  429. end
  430. else
  431. break;
  432. end;
  433. end;
  434. end;
  435. else
  436. begin
  437. message(type_e_type_id_expected);
  438. def:=generrordef;
  439. end;
  440. end;
  441. until not again;
  442. if dospecialize then
  443. generate_specialization(def)
  444. else
  445. begin
  446. if (df_generic in def.defoptions) then
  447. begin
  448. Message(parser_e_no_generics_as_types);
  449. def:=generrordef;
  450. end
  451. else if is_objccategory(def) then
  452. begin
  453. Message(parser_e_no_category_as_types);
  454. def:=generrordef
  455. end
  456. end;
  457. end;
  458. { reads a record declaration }
  459. function record_dec : tdef;
  460. var
  461. recst : trecordsymtable;
  462. begin
  463. { create recdef }
  464. recst:=trecordsymtable.create(current_settings.packrecords);
  465. record_dec:=trecorddef.create(recst);
  466. { insert in symtablestack }
  467. symtablestack.push(recst);
  468. { parse record }
  469. consume(_RECORD);
  470. read_record_fields([vd_record]);
  471. consume(_END);
  472. { make the record size aligned }
  473. recst.addalignmentpadding;
  474. { restore symtable stack }
  475. symtablestack.pop(recst);
  476. if trecorddef(record_dec).is_packed and
  477. record_dec.needs_inittable then
  478. Message(type_e_no_packed_inittable);
  479. end;
  480. { reads a type definition and returns a pointer to it }
  481. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  482. var
  483. pt : tnode;
  484. tt2 : tdef;
  485. aktenumdef : tenumdef;
  486. s : TIDString;
  487. l,v : TConstExprInt;
  488. oldpackrecords : longint;
  489. defpos,storepos : tfileposinfo;
  490. procedure expr_type;
  491. var
  492. pt1,pt2 : tnode;
  493. lv,hv : TConstExprInt;
  494. old_block_type : tblock_type;
  495. dospecialize : boolean;
  496. begin
  497. old_block_type:=block_type;
  498. dospecialize:=false;
  499. { use of current parsed object:
  500. - classes can be used also in classes
  501. - objects can be parameters }
  502. if (token=_ID) and
  503. assigned(current_objectdef) and
  504. (current_objectdef.objname^=pattern) and
  505. (
  506. (testcurobject=2) or
  507. is_class_or_interface_or_objc(current_objectdef)
  508. )then
  509. begin
  510. consume(_ID);
  511. def:=current_objectdef;
  512. exit;
  513. end;
  514. { Generate a specialization? }
  515. if try_to_consume(_SPECIALIZE) then
  516. dospecialize:=true;
  517. { we can't accept a equal in type }
  518. pt1:=comp_expr(false);
  519. if not dospecialize and
  520. try_to_consume(_POINTPOINT) then
  521. begin
  522. { get high value of range }
  523. pt2:=comp_expr(false);
  524. { make both the same type or give an error. This is not
  525. done when both are integer values, because typecasting
  526. between -3200..3200 will result in a signed-unsigned
  527. conflict and give a range check error (PFV) }
  528. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  529. inserttypeconv(pt1,pt2.resultdef);
  530. { both must be evaluated to constants now }
  531. if (pt1.nodetype=ordconstn) and
  532. (pt2.nodetype=ordconstn) then
  533. begin
  534. lv:=tordconstnode(pt1).value;
  535. hv:=tordconstnode(pt2).value;
  536. { Check bounds }
  537. if hv<lv then
  538. message(parser_e_upper_lower_than_lower)
  539. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  540. message(type_e_cant_eval_constant_expr)
  541. else
  542. begin
  543. { All checks passed, create the new def }
  544. case pt1.resultdef.typ of
  545. enumdef :
  546. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  547. orddef :
  548. begin
  549. if is_char(pt1.resultdef) then
  550. def:=torddef.create(uchar,lv,hv)
  551. else
  552. if is_boolean(pt1.resultdef) then
  553. def:=torddef.create(pasbool,lv,hv)
  554. else if is_signed(pt1.resultdef) then
  555. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  556. else
  557. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  558. end;
  559. end;
  560. end;
  561. end
  562. else
  563. Message(sym_e_error_in_type_def);
  564. pt2.free;
  565. end
  566. else
  567. begin
  568. { a simple type renaming or generic specialization }
  569. if (pt1.nodetype=typen) then
  570. begin
  571. def:=ttypenode(pt1).resultdef;
  572. if dospecialize then
  573. generate_specialization(def)
  574. else
  575. begin
  576. if (df_generic in def.defoptions) then
  577. begin
  578. Message(parser_e_no_generics_as_types);
  579. def:=generrordef;
  580. end
  581. else if is_objccategory(def) then
  582. begin
  583. Message(parser_e_no_category_as_types);
  584. def:=generrordef
  585. end
  586. end;
  587. end
  588. else
  589. Message(sym_e_error_in_type_def);
  590. end;
  591. pt1.free;
  592. block_type:=old_block_type;
  593. end;
  594. procedure set_dec;
  595. begin
  596. consume(_SET);
  597. consume(_OF);
  598. read_anon_type(tt2,true);
  599. if assigned(tt2) then
  600. begin
  601. case tt2.typ of
  602. { don't forget that min can be negativ PM }
  603. enumdef :
  604. if (tenumdef(tt2).min>=0) and
  605. (tenumdef(tt2).max<=255) then
  606. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  607. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  608. else
  609. Message(sym_e_ill_type_decl_set);
  610. orddef :
  611. begin
  612. if (torddef(tt2).ordtype<>uvoid) and
  613. (torddef(tt2).ordtype<>uwidechar) and
  614. (torddef(tt2).low>=0) then
  615. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  616. if Torddef(tt2).high>int64(high(byte)) then
  617. message(sym_e_ill_type_decl_set)
  618. else
  619. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  620. else
  621. Message(sym_e_ill_type_decl_set);
  622. end;
  623. else
  624. Message(sym_e_ill_type_decl_set);
  625. end;
  626. end
  627. else
  628. def:=generrordef;
  629. end;
  630. procedure array_dec(is_packed: boolean);
  631. var
  632. lowval,
  633. highval : TConstExprInt;
  634. indexdef : tdef;
  635. hdef : tdef;
  636. arrdef : tarraydef;
  637. procedure setdefdecl(def:tdef);
  638. begin
  639. case def.typ of
  640. enumdef :
  641. begin
  642. lowval:=tenumdef(def).min;
  643. highval:=tenumdef(def).max;
  644. if (m_fpc in current_settings.modeswitches) and
  645. (tenumdef(def).has_jumps) then
  646. Message(type_e_array_index_enums_with_assign_not_possible);
  647. indexdef:=def;
  648. end;
  649. orddef :
  650. begin
  651. if torddef(def).ordtype in [uchar,
  652. u8bit,u16bit,
  653. s8bit,s16bit,s32bit,
  654. {$ifdef cpu64bitaddr}
  655. u32bit,s64bit,
  656. {$endif cpu64bitaddr}
  657. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  658. uwidechar] then
  659. begin
  660. lowval:=torddef(def).low;
  661. highval:=torddef(def).high;
  662. indexdef:=def;
  663. end
  664. else
  665. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  666. end;
  667. else
  668. Message(sym_e_error_in_type_def);
  669. end;
  670. end;
  671. begin
  672. arrdef:=nil;
  673. consume(_ARRAY);
  674. { open array? }
  675. if try_to_consume(_LECKKLAMMER) then
  676. begin
  677. { defaults }
  678. indexdef:=generrordef;
  679. { use defaults which don't overflow the compiler }
  680. lowval:=0;
  681. highval:=0;
  682. repeat
  683. { read the expression and check it, check apart if the
  684. declaration is an enum declaration because that needs to
  685. be parsed by readtype (PFV) }
  686. if token=_LKLAMMER then
  687. begin
  688. read_anon_type(hdef,true);
  689. setdefdecl(hdef);
  690. end
  691. else
  692. begin
  693. pt:=expr;
  694. if pt.nodetype=typen then
  695. setdefdecl(pt.resultdef)
  696. else
  697. begin
  698. if (pt.nodetype=rangen) then
  699. begin
  700. if (trangenode(pt).left.nodetype=ordconstn) and
  701. (trangenode(pt).right.nodetype=ordconstn) then
  702. begin
  703. { make both the same type or give an error. This is not
  704. done when both are integer values, because typecasting
  705. between -3200..3200 will result in a signed-unsigned
  706. conflict and give a range check error (PFV) }
  707. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  708. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  709. lowval:=tordconstnode(trangenode(pt).left).value;
  710. highval:=tordconstnode(trangenode(pt).right).value;
  711. if highval<lowval then
  712. begin
  713. Message(parser_e_array_lower_less_than_upper_bound);
  714. highval:=lowval;
  715. end
  716. else if (lowval<int64(low(aint))) or
  717. (highval > high(aint)) then
  718. begin
  719. Message(parser_e_array_range_out_of_bounds);
  720. lowval :=0;
  721. highval:=0;
  722. end;
  723. if is_integer(trangenode(pt).left.resultdef) then
  724. range_to_type(lowval,highval,indexdef)
  725. else
  726. indexdef:=trangenode(pt).left.resultdef;
  727. end
  728. else
  729. Message(type_e_cant_eval_constant_expr);
  730. end
  731. else
  732. Message(sym_e_error_in_type_def)
  733. end;
  734. pt.free;
  735. end;
  736. { if the array is already created add the new arrray
  737. as element of the existing array, otherwise create a new array }
  738. if assigned(arrdef) then
  739. begin
  740. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  741. arrdef:=tarraydef(arrdef.elementdef);
  742. end
  743. else
  744. begin
  745. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  746. def:=arrdef;
  747. end;
  748. if is_packed then
  749. include(arrdef.arrayoptions,ado_IsBitPacked);
  750. if token=_COMMA then
  751. consume(_COMMA)
  752. else
  753. break;
  754. until false;
  755. consume(_RECKKLAMMER);
  756. end
  757. else
  758. begin
  759. if is_packed then
  760. Message(parser_e_packed_dynamic_open_array);
  761. arrdef:=tarraydef.create(0,-1,s32inttype);
  762. include(arrdef.arrayoptions,ado_IsDynamicArray);
  763. def:=arrdef;
  764. end;
  765. consume(_OF);
  766. read_anon_type(tt2,true);
  767. { set element type of the last array definition }
  768. if assigned(arrdef) then
  769. begin
  770. arrdef.elementdef:=tt2;
  771. if is_packed and
  772. tt2.needs_inittable then
  773. Message(type_e_no_packed_inittable);
  774. end;
  775. end;
  776. var
  777. p : tnode;
  778. hdef : tdef;
  779. pd : tabstractprocdef;
  780. is_func,
  781. enumdupmsg, first : boolean;
  782. newtype : ttypesym;
  783. oldlocalswitches : tlocalswitches;
  784. bitpacking: boolean;
  785. begin
  786. def:=nil;
  787. case token of
  788. _STRING,_FILE:
  789. begin
  790. single_type(def,false,true);
  791. end;
  792. _LKLAMMER:
  793. begin
  794. consume(_LKLAMMER);
  795. first := true;
  796. { allow negativ value_str }
  797. l:=int64(-1);
  798. enumdupmsg:=false;
  799. aktenumdef:=tenumdef.create;
  800. repeat
  801. s:=orgpattern;
  802. defpos:=current_tokenpos;
  803. consume(_ID);
  804. { only allow assigning of specific numbers under fpc mode }
  805. if not(m_tp7 in current_settings.modeswitches) and
  806. (
  807. { in fpc mode also allow := to be compatible
  808. with previous 1.0.x versions }
  809. ((m_fpc in current_settings.modeswitches) and
  810. try_to_consume(_ASSIGNMENT)) or
  811. try_to_consume(_EQUAL)
  812. ) then
  813. begin
  814. oldlocalswitches:=current_settings.localswitches;
  815. include(current_settings.localswitches,cs_allow_enum_calc);
  816. p:=comp_expr(true);
  817. current_settings.localswitches:=oldlocalswitches;
  818. if (p.nodetype=ordconstn) then
  819. begin
  820. { we expect an integer or an enum of the
  821. same type }
  822. if is_integer(p.resultdef) or
  823. is_char(p.resultdef) or
  824. equal_defs(p.resultdef,aktenumdef) then
  825. v:=tordconstnode(p).value
  826. else
  827. IncompatibleTypes(p.resultdef,s32inttype);
  828. end
  829. else
  830. Message(parser_e_illegal_expression);
  831. p.free;
  832. { please leave that a note, allows type save }
  833. { declarations in the win32 units ! }
  834. if (not first) and (v<=l) and (not enumdupmsg) then
  835. begin
  836. Message(parser_n_duplicate_enum);
  837. enumdupmsg:=true;
  838. end;
  839. l:=v;
  840. end
  841. else
  842. inc(l.svalue);
  843. first := false;
  844. storepos:=current_tokenpos;
  845. current_tokenpos:=defpos;
  846. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  847. current_tokenpos:=storepos;
  848. until not try_to_consume(_COMMA);
  849. def:=aktenumdef;
  850. consume(_RKLAMMER);
  851. end;
  852. _ARRAY:
  853. begin
  854. array_dec(false);
  855. end;
  856. _SET:
  857. begin
  858. set_dec;
  859. end;
  860. _CARET:
  861. begin
  862. consume(_CARET);
  863. single_type(tt2,(block_type=bt_type),false);
  864. def:=tpointerdef.create(tt2);
  865. if tt2.typ=forwarddef then
  866. current_module.checkforwarddefs.add(def);
  867. end;
  868. _RECORD:
  869. begin
  870. def:=record_dec;
  871. end;
  872. _PACKED,
  873. _BITPACKED:
  874. begin
  875. bitpacking :=
  876. (cs_bitpacking in current_settings.localswitches) or
  877. (token = _BITPACKED);
  878. consume(token);
  879. if token=_ARRAY then
  880. array_dec(bitpacking)
  881. else if token=_SET then
  882. set_dec
  883. else
  884. begin
  885. oldpackrecords:=current_settings.packrecords;
  886. if (not bitpacking) or
  887. (token in [_CLASS,_OBJECT]) then
  888. current_settings.packrecords:=1
  889. else
  890. current_settings.packrecords:=bit_alignment;
  891. case token of
  892. _CLASS :
  893. begin
  894. consume(_CLASS);
  895. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  896. end;
  897. _OBJECT :
  898. begin
  899. consume(_OBJECT);
  900. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  901. end;
  902. else
  903. def:=record_dec;
  904. end;
  905. current_settings.packrecords:=oldpackrecords;
  906. end;
  907. end;
  908. _DISPINTERFACE :
  909. begin
  910. { need extra check here since interface is a keyword
  911. in all pascal modes }
  912. if not(m_class in current_settings.modeswitches) then
  913. Message(parser_f_need_objfpc_or_delphi_mode);
  914. consume(token);
  915. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  916. end;
  917. _CLASS :
  918. begin
  919. consume(token);
  920. { Delphi only allows class of in type blocks }
  921. if (token=_OF) and
  922. (
  923. not(m_delphi in current_settings.modeswitches) or
  924. (block_type=bt_type)
  925. ) then
  926. begin
  927. consume(_OF);
  928. single_type(hdef,(block_type=bt_type),false);
  929. if is_class(hdef) or
  930. is_objcclass(hdef) then
  931. def:=tclassrefdef.create(hdef)
  932. else
  933. if hdef.typ=forwarddef then
  934. begin
  935. def:=tclassrefdef.create(hdef);
  936. current_module.checkforwarddefs.add(def);
  937. end
  938. else
  939. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  940. end
  941. else
  942. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  943. end;
  944. _CPPCLASS :
  945. begin
  946. consume(token);
  947. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  948. end;
  949. _OBJCCLASS :
  950. begin
  951. if not(m_objectivec1 in current_settings.modeswitches) then
  952. Message(parser_f_need_objc);
  953. consume(token);
  954. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil);
  955. end;
  956. _INTERFACE :
  957. begin
  958. { need extra check here since interface is a keyword
  959. in all pascal modes }
  960. if not(m_class in current_settings.modeswitches) then
  961. Message(parser_f_need_objfpc_or_delphi_mode);
  962. consume(token);
  963. if current_settings.interfacetype=it_interfacecom then
  964. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  965. else {it_interfacecorba}
  966. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  967. end;
  968. _OBJCPROTOCOL :
  969. begin
  970. if not(m_objectivec1 in current_settings.modeswitches) then
  971. Message(parser_f_need_objc);
  972. consume(token);
  973. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil);
  974. end;
  975. _OBJCCATEGORY :
  976. begin
  977. if not(m_objectivec1 in current_settings.modeswitches) then
  978. Message(parser_f_need_objc);
  979. consume(token);
  980. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil);
  981. end;
  982. _OBJECT :
  983. begin
  984. consume(token);
  985. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  986. end;
  987. _PROCEDURE,
  988. _FUNCTION:
  989. begin
  990. is_func:=(token=_FUNCTION);
  991. consume(token);
  992. pd:=tprocvardef.create(normal_function_level);
  993. if token=_LKLAMMER then
  994. parse_parameter_dec(pd);
  995. if is_func then
  996. begin
  997. consume(_COLON);
  998. single_type(pd.returndef,false,false);
  999. end;
  1000. if token=_OF then
  1001. begin
  1002. consume(_OF);
  1003. consume(_OBJECT);
  1004. include(pd.procoptions,po_methodpointer);
  1005. end;
  1006. def:=pd;
  1007. { possible proc directives }
  1008. if parseprocvardir then
  1009. begin
  1010. if check_proc_directive(true) then
  1011. begin
  1012. newtype:=ttypesym.create('unnamed',def);
  1013. parse_var_proc_directives(tsym(newtype));
  1014. newtype.typedef:=nil;
  1015. def.typesym:=nil;
  1016. newtype.free;
  1017. end;
  1018. { Add implicit hidden parameters and function result }
  1019. handle_calling_convention(pd);
  1020. end;
  1021. end;
  1022. else
  1023. expr_type;
  1024. end;
  1025. if def=nil then
  1026. def:=generrordef;
  1027. end;
  1028. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1029. begin
  1030. read_named_type(def,'',nil,nil,parseprocvardir);
  1031. end;
  1032. procedure write_persistent_type_info(st:tsymtable);
  1033. var
  1034. i : longint;
  1035. def : tdef;
  1036. vmtwriter : TVMTWriter;
  1037. begin
  1038. for i:=0 to st.DefList.Count-1 do
  1039. begin
  1040. def:=tdef(st.DefList[i]);
  1041. case def.typ of
  1042. recorddef :
  1043. write_persistent_type_info(trecorddef(def).symtable);
  1044. objectdef :
  1045. begin
  1046. { Skip generics and forward defs }
  1047. if (df_generic in def.defoptions) or
  1048. (oo_is_forward in tobjectdef(def).objectoptions) then
  1049. continue;
  1050. write_persistent_type_info(tobjectdef(def).symtable);
  1051. { Write also VMT if not done yet }
  1052. if not(ds_vmt_written in def.defstates) then
  1053. begin
  1054. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1055. if is_interface(tobjectdef(def)) then
  1056. vmtwriter.writeinterfaceids;
  1057. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1058. vmtwriter.writevmt;
  1059. vmtwriter.free;
  1060. include(def.defstates,ds_vmt_written);
  1061. end;
  1062. end;
  1063. procdef :
  1064. begin
  1065. if assigned(tprocdef(def).localst) and
  1066. (tprocdef(def).localst.symtabletype=localsymtable) then
  1067. write_persistent_type_info(tprocdef(def).localst);
  1068. if assigned(tprocdef(def).parast) then
  1069. write_persistent_type_info(tprocdef(def).parast);
  1070. end;
  1071. end;
  1072. { generate always persistent tables for types in the interface so it can
  1073. be reused in other units and give always the same pointer location. }
  1074. { Init }
  1075. if (
  1076. assigned(def.typesym) and
  1077. (st.symtabletype=globalsymtable) and
  1078. not is_objc_class_or_protocol(def)
  1079. ) or
  1080. def.needs_inittable or
  1081. (ds_init_table_used in def.defstates) then
  1082. RTTIWriter.write_rtti(def,initrtti);
  1083. { RTTI }
  1084. if (
  1085. assigned(def.typesym) and
  1086. (st.symtabletype=globalsymtable) and
  1087. not is_objc_class_or_protocol(def)
  1088. ) or
  1089. (ds_rtti_table_used in def.defstates) then
  1090. RTTIWriter.write_rtti(def,fullrtti);
  1091. end;
  1092. end;
  1093. end.