ptype.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does parsing types for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptype;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype;
  23. const
  24. { forward types should only be possible inside a TYPE statement }
  25. typecanbeforward : boolean = false;
  26. var
  27. { hack, which allows to use the current parsed }
  28. { object type as function argument type }
  29. testcurobject : byte;
  30. curobjectname : stringid;
  31. { reads a string, file type or a type id and returns a name and }
  32. { tdef }
  33. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  34. procedure read_type(var tt:ttype;const name : stringid);
  35. { reads a type definition }
  36. { to a appropriating tdef, s gets the name of }
  37. { the type to allow name mangling }
  38. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  39. implementation
  40. uses
  41. { common }
  42. cutils,
  43. { global }
  44. globals,tokens,verbose,
  45. systems,
  46. { symtable }
  47. symconst,symbase,symdef,symsym,symtable,defbase,
  48. { pass 1 }
  49. node,
  50. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  51. { parser }
  52. scanner,
  53. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  54. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  55. { reads a type definition }
  56. { to a appropriating tdef, s gets the name of }
  57. { the type to allow name mangling }
  58. var
  59. is_unit_specific : boolean;
  60. pos : tfileposinfo;
  61. srsym : tsym;
  62. srsymtable : tsymtable;
  63. sorg : stringid;
  64. begin
  65. s:=pattern;
  66. sorg:=orgpattern;
  67. pos:=akttokenpos;
  68. { classes can be used also in classes }
  69. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  70. begin
  71. tt.setdef(aktobjectdef);
  72. consume(_ID);
  73. exit;
  74. end;
  75. { objects can be parameters }
  76. if (testcurobject=2) and (curobjectname=pattern) then
  77. begin
  78. tt.setdef(aktobjectdef);
  79. consume(_ID);
  80. exit;
  81. end;
  82. { try to load the symbol to see if it's a unitsym }
  83. is_unit_specific:=false;
  84. searchsym(s,srsym,srsymtable);
  85. consume(_ID);
  86. if assigned(srsym) and
  87. (srsym.typ=unitsym) then
  88. begin
  89. is_unit_specific:=true;
  90. consume(_POINT);
  91. if srsym.owner.unitid=0 then
  92. begin
  93. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  94. pos:=akttokenpos;
  95. s:=pattern;
  96. end
  97. else
  98. srsym:=nil;
  99. consume(_ID);
  100. end;
  101. { Types are first defined with an error def before assigning
  102. the real type so check if it's an errordef. if so then
  103. give an error. Only check for typesyms in the current symbol
  104. table as forwarddef are not resolved directly }
  105. if assigned(srsym) and
  106. (srsym.owner=symtablestack) and
  107. (ttypesym(srsym).restype.def.deftype=errordef) then
  108. begin
  109. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  110. tt:=generrortype;
  111. exit;
  112. end;
  113. { are we parsing a possible forward def ? }
  114. if isforwarddef and
  115. not(is_unit_specific) then
  116. begin
  117. tt.setdef(tforwarddef.create(s,pos));
  118. exit;
  119. end;
  120. { unknown sym ? }
  121. if not assigned(srsym) then
  122. begin
  123. Message1(sym_e_id_not_found,sorg);
  124. tt:=generrortype;
  125. exit;
  126. end;
  127. { type sym ? }
  128. if (srsym.typ<>typesym) then
  129. begin
  130. Message(type_e_type_id_expected);
  131. tt:=generrortype;
  132. exit;
  133. end;
  134. { Give an error when referring to an errordef }
  135. if (ttypesym(srsym).restype.def.deftype=errordef) then
  136. begin
  137. Message(sym_e_error_in_type_def);
  138. tt:=generrortype;
  139. exit;
  140. end;
  141. { Only use the definitions for current unit, becuase
  142. they can be refered from the parameters and symbols are not
  143. loaded at that time. A symbol reference to an other unit
  144. is still possible, because it's already loaded (PFV)
  145. can't use in [] here, becuase unitid can be > 255 }
  146. if (ttypesym(srsym).owner.unitid=0) then
  147. tt.setdef(ttypesym(srsym).restype.def)
  148. else
  149. tt.setsym(srsym);
  150. end;
  151. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  152. { reads a string, file type or a type id and returns a name and }
  153. { tdef }
  154. var
  155. hs : string;
  156. t2 : ttype;
  157. begin
  158. case token of
  159. _STRING:
  160. begin
  161. string_dec(tt);
  162. s:='STRING';
  163. end;
  164. _FILE:
  165. begin
  166. consume(_FILE);
  167. if token=_OF then
  168. begin
  169. consume(_OF);
  170. single_type(t2,hs,false);
  171. tt.setdef(tfiledef.createtyped(t2));
  172. s:='FILE$OF$'+hs;
  173. end
  174. else
  175. begin
  176. tt:=cfiletype;
  177. s:='FILE';
  178. end;
  179. end;
  180. _ID:
  181. begin
  182. id_type(tt,s,isforwarddef);
  183. end;
  184. else
  185. begin
  186. message(type_e_type_id_expected);
  187. s:='<unknown>';
  188. tt:=generrortype;
  189. end;
  190. end;
  191. end;
  192. { reads a record declaration }
  193. function record_dec : tdef;
  194. var
  195. symtable : tsymtable;
  196. storetypecanbeforward : boolean;
  197. old_object_option : tsymoptions;
  198. begin
  199. { create recdef }
  200. symtable:=trecordsymtable.create;
  201. record_dec:=trecorddef.create(symtable);
  202. { update symtable stack }
  203. symtable.next:=symtablestack;
  204. symtablestack:=symtable;
  205. { parse record }
  206. consume(_RECORD);
  207. old_object_option:=current_object_option;
  208. current_object_option:=[sp_public];
  209. storetypecanbeforward:=typecanbeforward;
  210. { for tp7 don't allow forward types }
  211. if m_tp7 in aktmodeswitches then
  212. typecanbeforward:=false;
  213. read_var_decs(true,false,false);
  214. consume(_END);
  215. typecanbeforward:=storetypecanbeforward;
  216. current_object_option:=old_object_option;
  217. { may be scale record size to a size of n*4 ? }
  218. symtablestack.datasize:=align(symtablestack.datasize,symtablestack.dataalignment);
  219. { restore symtable stack }
  220. symtablestack:=symtable.next;
  221. end;
  222. { reads a type definition and returns a pointer to it }
  223. procedure read_type(var tt : ttype;const name : stringid);
  224. var
  225. pt : tnode;
  226. tt2 : ttype;
  227. aktenumdef : tenumdef;
  228. ap : tarraydef;
  229. s : stringid;
  230. l,v : TConstExprInt;
  231. oldaktpackrecords : longint;
  232. hs : string;
  233. defpos,storepos : tfileposinfo;
  234. procedure expr_type;
  235. var
  236. pt1,pt2 : tnode;
  237. lv,hv : TConstExprInt;
  238. begin
  239. { use of current parsed object ? }
  240. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  241. begin
  242. consume(_ID);
  243. tt.setdef(aktobjectdef);
  244. exit;
  245. end;
  246. { classes can be used also in classes }
  247. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  248. begin
  249. tt.setdef(aktobjectdef);
  250. consume(_ID);
  251. exit;
  252. end;
  253. { we can't accept a equal in type }
  254. pt1:=comp_expr(not(ignore_equal));
  255. if (token=_POINTPOINT) then
  256. begin
  257. consume(_POINTPOINT);
  258. { get high value of range }
  259. pt2:=comp_expr(not(ignore_equal));
  260. { make both the same type }
  261. inserttypeconv(pt1,pt2.resulttype);
  262. { both must be evaluated to constants now }
  263. if (pt1.nodetype=ordconstn) and
  264. (pt2.nodetype=ordconstn) then
  265. begin
  266. lv:=tordconstnode(pt1).value;
  267. hv:=tordconstnode(pt2).value;
  268. { Check bounds }
  269. if hv<lv then
  270. Message(cg_e_upper_lower_than_lower)
  271. else
  272. begin
  273. { All checks passed, create the new def }
  274. case pt1.resulttype.def.deftype of
  275. enumdef :
  276. tt.setdef(tenumdef.create_subrange(tenumdef(pt1.resulttype.def),lv,hv));
  277. orddef :
  278. begin
  279. if is_char(pt1.resulttype.def) then
  280. tt.setdef(torddef.create(uchar,lv,hv))
  281. else
  282. if is_boolean(pt1.resulttype.def) then
  283. tt.setdef(torddef.create(bool8bit,l,hv))
  284. else
  285. tt.setdef(torddef.create(range_to_basetype(lv,hv),lv,hv));
  286. end;
  287. end;
  288. end;
  289. end
  290. else
  291. Message(sym_e_error_in_type_def);
  292. pt2.free;
  293. end
  294. else
  295. begin
  296. { a simple type renaming }
  297. if (pt1.nodetype=typen) then
  298. tt:=ttypenode(pt1).resulttype
  299. else
  300. Message(sym_e_error_in_type_def);
  301. end;
  302. pt1.free;
  303. end;
  304. procedure array_dec;
  305. var
  306. lowval,
  307. highval : longint;
  308. arraytype : ttype;
  309. ht : ttype;
  310. procedure setdefdecl(const t:ttype);
  311. begin
  312. case t.def.deftype of
  313. enumdef :
  314. begin
  315. lowval:=tenumdef(t.def).min;
  316. highval:=tenumdef(t.def).max;
  317. if tenumdef(t.def).has_jumps then
  318. Message(type_e_array_index_enums_with_assign_not_possible);
  319. arraytype:=t;
  320. end;
  321. orddef :
  322. begin
  323. if torddef(t.def).typ in [uchar,
  324. u8bit,u16bit,
  325. s8bit,s16bit,s32bit,
  326. bool8bit,bool16bit,bool32bit,
  327. uwidechar] then
  328. begin
  329. lowval:=torddef(t.def).low;
  330. highval:=torddef(t.def).high;
  331. arraytype:=t;
  332. end
  333. else
  334. Message1(parser_e_type_cant_be_used_in_array_index,t.def.gettypename);
  335. end;
  336. else
  337. Message(sym_e_error_in_type_def);
  338. end;
  339. end;
  340. begin
  341. consume(_ARRAY);
  342. { open array? }
  343. if token=_LECKKLAMMER then
  344. begin
  345. consume(_LECKKLAMMER);
  346. { defaults }
  347. arraytype:=generrortype;
  348. lowval:=longint($80000000);
  349. highval:=$7fffffff;
  350. tt.reset;
  351. repeat
  352. { read the expression and check it, check apart if the
  353. declaration is an enum declaration because that needs to
  354. be parsed by readtype (PFV) }
  355. if token=_LKLAMMER then
  356. begin
  357. read_type(ht,'');
  358. setdefdecl(ht);
  359. end
  360. else
  361. begin
  362. pt:=expr;
  363. if pt.nodetype=typen then
  364. setdefdecl(pt.resulttype)
  365. else
  366. begin
  367. if (pt.nodetype=rangen) then
  368. begin
  369. if (trangenode(pt).left.nodetype=ordconstn) and
  370. (trangenode(pt).right.nodetype=ordconstn) then
  371. begin
  372. lowval:=tordconstnode(trangenode(pt).left).value;
  373. highval:=tordconstnode(trangenode(pt).right).value;
  374. if highval<lowval then
  375. begin
  376. Message(parser_e_array_lower_less_than_upper_bound);
  377. highval:=lowval;
  378. end;
  379. arraytype:=trangenode(pt).right.resulttype;
  380. end
  381. else
  382. Message(type_e_cant_eval_constant_expr);
  383. end
  384. else
  385. Message(sym_e_error_in_type_def)
  386. end;
  387. pt.free;
  388. end;
  389. { create arraydef }
  390. if not assigned(tt.def) then
  391. begin
  392. ap:=tarraydef.create(lowval,highval,arraytype);
  393. tt.setdef(ap);
  394. end
  395. else
  396. begin
  397. ap.elementtype.setdef(tarraydef.create(lowval,highval,arraytype));
  398. ap:=tarraydef(ap.elementtype.def);
  399. end;
  400. if token=_COMMA then
  401. consume(_COMMA)
  402. else
  403. break;
  404. until false;
  405. consume(_RECKKLAMMER);
  406. end
  407. else
  408. begin
  409. ap:=tarraydef.create(0,-1,s32bittype);
  410. ap.IsDynamicArray:=true;
  411. tt.setdef(ap);
  412. end;
  413. consume(_OF);
  414. read_type(tt2,'');
  415. { if no error, set element type }
  416. if assigned(ap) then
  417. ap.elementtype:=tt2;
  418. end;
  419. var
  420. p : tnode;
  421. enumdupmsg : boolean;
  422. begin
  423. tt.reset;
  424. case token of
  425. _STRING,_FILE:
  426. begin
  427. single_type(tt,hs,false);
  428. end;
  429. _LKLAMMER:
  430. begin
  431. consume(_LKLAMMER);
  432. { allow negativ value_str }
  433. l:=-1;
  434. enumdupmsg:=false;
  435. aktenumdef:=tenumdef.create;
  436. repeat
  437. s:=orgpattern;
  438. defpos:=akttokenpos;
  439. consume(_ID);
  440. { only allow assigning of specific numbers under fpc mode }
  441. if (m_fpc in aktmodeswitches) and
  442. (token=_ASSIGNMENT) then
  443. begin
  444. consume(_ASSIGNMENT);
  445. p:=comp_expr(true);
  446. if (p.nodetype=ordconstn) then
  447. begin
  448. { we expect an integer or an enum of the
  449. same type }
  450. if is_integer(p.resulttype.def) or
  451. is_char(p.resulttype.def) or
  452. is_equal(p.resulttype.def,aktenumdef) then
  453. v:=tordconstnode(p).value
  454. else
  455. Message2(type_e_incompatible_types,p.resulttype.def.typename,s32bittype.def.typename);
  456. end
  457. else
  458. Message(cg_e_illegal_expression);
  459. p.free;
  460. { please leave that a note, allows type save }
  461. { declarations in the win32 units ! }
  462. if (v<=l) and (not enumdupmsg) then
  463. begin
  464. Message(parser_n_duplicate_enum);
  465. enumdupmsg:=true;
  466. end;
  467. l:=v;
  468. end
  469. else if (m_delphi in aktmodeswitches) and
  470. (token=_EQUAL) then
  471. begin
  472. consume(_EQUAL);
  473. p:=comp_expr(true);
  474. if (p.nodetype=ordconstn) then
  475. begin
  476. { we expect an integer or an enum of the
  477. same type }
  478. if is_integer(p.resulttype.def) or
  479. is_equal(p.resulttype.def,aktenumdef) then
  480. l:=tordconstnode(p).value
  481. else
  482. Message2(type_e_incompatible_types,p.resulttype.def.typename,s32bittype.def.typename);
  483. end
  484. else
  485. Message(cg_e_illegal_expression);
  486. p.free;
  487. end
  488. else
  489. inc(l);
  490. storepos:=akttokenpos;
  491. akttokenpos:=defpos;
  492. constsymtable.insert(tenumsym.create(s,aktenumdef,l));
  493. akttokenpos:=storepos;
  494. until not try_to_consume(_COMMA);
  495. tt.setdef(aktenumdef);
  496. consume(_RKLAMMER);
  497. end;
  498. _ARRAY:
  499. begin
  500. array_dec;
  501. end;
  502. _SET:
  503. begin
  504. consume(_SET);
  505. consume(_OF);
  506. read_type(tt2,'');
  507. if assigned(tt2.def) then
  508. begin
  509. case tt2.def.deftype of
  510. { don't forget that min can be negativ PM }
  511. enumdef :
  512. if tenumdef(tt2.def).min>=0 then
  513. tt.setdef(tsetdef.create(tt2,tenumdef(tt2.def).max))
  514. else
  515. Message(sym_e_ill_type_decl_set);
  516. orddef :
  517. begin
  518. case torddef(tt2.def).typ of
  519. uchar :
  520. tt.setdef(tsetdef.create(tt2,255));
  521. u8bit,u16bit,u32bit,
  522. s8bit,s16bit,s32bit :
  523. begin
  524. if (torddef(tt2.def).low>=0) then
  525. tt.setdef(tsetdef.create(tt2,torddef(tt2.def).high))
  526. else
  527. Message(sym_e_ill_type_decl_set);
  528. end;
  529. else
  530. Message(sym_e_ill_type_decl_set);
  531. end;
  532. end;
  533. else
  534. Message(sym_e_ill_type_decl_set);
  535. end;
  536. end
  537. else
  538. tt:=generrortype;
  539. end;
  540. _CARET:
  541. begin
  542. consume(_CARET);
  543. single_type(tt2,hs,typecanbeforward);
  544. tt.setdef(tpointerdef.create(tt2));
  545. end;
  546. _RECORD:
  547. begin
  548. tt.setdef(record_dec);
  549. end;
  550. _PACKED:
  551. begin
  552. consume(_PACKED);
  553. if token=_ARRAY then
  554. array_dec
  555. else
  556. begin
  557. oldaktpackrecords:=aktalignment.recordalignmax;
  558. aktalignment.recordalignmax:=1;
  559. if token in [_CLASS,_OBJECT] then
  560. tt.setdef(object_dec(name,nil))
  561. else
  562. tt.setdef(record_dec);
  563. aktalignment.recordalignmax:=oldaktpackrecords;
  564. end;
  565. end;
  566. _CLASS,
  567. _CPPCLASS,
  568. _INTERFACE,
  569. _OBJECT:
  570. begin
  571. tt.setdef(object_dec(name,nil));
  572. end;
  573. _PROCEDURE:
  574. begin
  575. consume(_PROCEDURE);
  576. tt.setdef(tprocvardef.create);
  577. if token=_LKLAMMER then
  578. parameter_dec(tprocvardef(tt.def));
  579. if token=_OF then
  580. begin
  581. consume(_OF);
  582. consume(_OBJECT);
  583. include(tprocvardef(tt.def).procoptions,po_methodpointer);
  584. end;
  585. end;
  586. _FUNCTION:
  587. begin
  588. consume(_FUNCTION);
  589. tt.def:=tprocvardef.create;
  590. if token=_LKLAMMER then
  591. parameter_dec(tprocvardef(tt.def));
  592. consume(_COLON);
  593. single_type(tprocvardef(tt.def).rettype,hs,false);
  594. if token=_OF then
  595. begin
  596. consume(_OF);
  597. consume(_OBJECT);
  598. include(tprocvardef(tt.def).procoptions,po_methodpointer);
  599. end;
  600. end;
  601. else
  602. expr_type;
  603. end;
  604. if tt.def=nil then
  605. tt:=generrortype;
  606. end;
  607. end.
  608. {
  609. $Log$
  610. Revision 1.43 2002-09-09 19:34:07 peter
  611. * check for incomplete types in the current symtable when parsing
  612. forwarddef. Maybe this shall be delphi/tp only
  613. Revision 1.42 2002/07/20 11:57:56 florian
  614. * types.pas renamed to defbase.pas because D6 contains a types
  615. unit so this would conflicts if D6 programms are compiled
  616. + Willamette/SSE2 instructions to assembler added
  617. Revision 1.41 2002/05/18 13:34:16 peter
  618. * readded missing revisions
  619. Revision 1.40 2002/05/16 19:46:44 carl
  620. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  621. + try to fix temp allocation (still in ifdef)
  622. + generic constructor calls
  623. + start of tassembler / tmodulebase class cleanup
  624. Revision 1.38 2002/05/12 16:53:10 peter
  625. * moved entry and exitcode to ncgutil and cgobj
  626. * foreach gets extra argument for passing local data to the
  627. iterator function
  628. * -CR checks also class typecasts at runtime by changing them
  629. into as
  630. * fixed compiler to cycle with the -CR option
  631. * fixed stabs with elf writer, finally the global variables can
  632. be watched
  633. * removed a lot of routines from cga unit and replaced them by
  634. calls to cgobj
  635. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  636. u32bit then the other is typecasted also to u32bit without giving
  637. a rangecheck warning/error.
  638. * fixed pascal calling method with reversing also the high tree in
  639. the parast, detected by tcalcst3 test
  640. Revision 1.37 2002/04/19 15:46:03 peter
  641. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  642. in most cases and not written to the ppu
  643. * add mangeledname_prefix() routine to generate the prefix of
  644. manglednames depending on the current procedure, object and module
  645. * removed static procprefix since the mangledname is now build only
  646. on demand from tprocdef.mangledname
  647. Revision 1.36 2002/04/16 16:12:47 peter
  648. * give error when using enums with jumps as array index
  649. * allow char as enum value
  650. Revision 1.35 2002/04/04 19:06:04 peter
  651. * removed unused units
  652. * use tlocation.size in cg.a_*loc*() routines
  653. Revision 1.34 2002/01/24 18:25:49 peter
  654. * implicit result variable generation for assembler routines
  655. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  656. Revision 1.33 2002/01/15 16:13:34 jonas
  657. * fixed web bugs 1758 and 1760
  658. Revision 1.32 2002/01/06 12:08:15 peter
  659. * removed uauto from orddef, use new range_to_basetype generating
  660. the correct ordinal type for a range
  661. }