htypechk.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  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 htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,
  23. node,
  24. symtype,symdef;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. const
  32. tok2nodes=25;
  33. tok2node:array[1..tok2nodes] of ttok2noderec=(
  34. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  35. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  36. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  37. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  38. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  39. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  40. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  41. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  42. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  43. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  44. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  45. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  46. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  47. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  48. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  52. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  57. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  59. );
  60. const
  61. { firstcallparan without varspez we don't count the ref }
  62. {$ifdef extdebug}
  63. count_ref : boolean = true;
  64. {$endif def extdebug}
  65. get_para_resulttype : boolean = false;
  66. allow_array_constructor : boolean = false;
  67. { is overloading of this operator allowed for this
  68. binary operator }
  69. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  70. { is overloading of this operator allowed for this
  71. unary operator }
  72. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  73. { check operator args and result type }
  74. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  75. function isbinaryoverloaded(var t : tnode) : boolean;
  76. { Register Allocation }
  77. procedure make_not_regable(p : tnode);
  78. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  79. { subroutine handling }
  80. function is_procsym_load(p:tnode):boolean;
  81. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  82. {
  83. type
  84. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  85. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  86. { sets varsym varstate field correctly }
  87. procedure unset_varstate(p : tnode);
  88. procedure set_varstate(p : tnode;must_be_valid : boolean);
  89. { sets the callunique flag, if the node is a vecn, }
  90. { takes care of type casts etc. }
  91. procedure set_unique(p : tnode);
  92. { sets funcret_is_valid to true, if p contains a funcref node }
  93. procedure set_funcret_is_valid(p : tnode);
  94. function valid_for_formal_var(p : tnode) : boolean;
  95. function valid_for_formal_const(p : tnode) : boolean;
  96. function valid_for_var(p:tnode):boolean;
  97. function valid_for_assignment(p:tnode):boolean;
  98. implementation
  99. uses
  100. globtype,systems,
  101. cutils,verbose,globals,
  102. symconst,symsym,symtable,
  103. defutil,defcmp,cpubase,
  104. ncnv,nld,
  105. nmem,ncal,nmat,
  106. cgbase
  107. ;
  108. type
  109. TValidAssign=(Valid_Property,Valid_Void);
  110. TValidAssigns=set of TValidAssign;
  111. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  112. procedure internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean);
  113. begin
  114. case ld.deftype of
  115. recorddef,
  116. variantdef :
  117. begin
  118. allowed:=true;
  119. end;
  120. procvardef :
  121. begin
  122. if (rd.deftype in [pointerdef,procdef,procvardef]) and
  123. (treetyp in [equaln,unequaln]) then
  124. begin
  125. allowed:=false;
  126. exit;
  127. end;
  128. allowed:=true;
  129. end;
  130. pointerdef :
  131. begin
  132. if ((rd.deftype in [pointerdef,classrefdef,procvardef]) or
  133. is_class_or_interface(rd)) and
  134. (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn]) then
  135. begin
  136. allowed:=false;
  137. exit;
  138. end;
  139. { don't allow operations on pointer/integer }
  140. if is_integer(rd) then
  141. begin
  142. allowed:=false;
  143. exit;
  144. end;
  145. { don't allow pchar+string }
  146. if is_pchar(ld) and
  147. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  148. (is_chararray(rd) or
  149. is_char(rd) or
  150. (rd.deftype=stringdef)) then
  151. begin
  152. allowed:=false;
  153. exit;
  154. end;
  155. allowed:=true;
  156. end;
  157. arraydef :
  158. begin
  159. { not mmx }
  160. if (cs_mmx in aktlocalswitches) and
  161. is_mmx_able_array(ld) then
  162. begin
  163. allowed:=false;
  164. exit;
  165. end;
  166. { not chararray+[char,string,chararray] }
  167. if is_chararray(ld) and
  168. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  169. (is_char(rd) or
  170. is_pchar(rd) or
  171. is_integer(rd) or
  172. (rd.deftype=stringdef) or
  173. is_chararray(rd)) then
  174. begin
  175. allowed:=false;
  176. exit;
  177. end;
  178. { dynamic array compare with niln }
  179. if is_dynamic_array(ld) and
  180. (rt=niln) and
  181. (treetyp in [equaln,unequaln]) then
  182. begin
  183. allowed:=false;
  184. exit;
  185. end;
  186. allowed:=true;
  187. end;
  188. objectdef :
  189. begin
  190. { <> and = are defined for classes }
  191. if (treetyp in [equaln,unequaln]) and
  192. is_class_or_interface(ld) then
  193. begin
  194. allowed:=false;
  195. exit;
  196. end;
  197. allowed:=true;
  198. end;
  199. stringdef :
  200. begin
  201. if ((rd.deftype=stringdef) or
  202. is_char(rd) or
  203. is_pchar(rd) or
  204. is_chararray(rd)) and
  205. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) then
  206. begin
  207. allowed:=false;
  208. exit;
  209. end;
  210. allowed:=true;
  211. end;
  212. end;
  213. end;
  214. var
  215. allowed : boolean;
  216. begin
  217. isbinaryoperatoroverloadable:=false;
  218. { power ** is always possible }
  219. if (treetyp=starstarn) then
  220. begin
  221. isbinaryoperatoroverloadable:=true;
  222. exit;
  223. end;
  224. { order of arguments does not matter so we have to check also
  225. the reversed order }
  226. allowed:=false;
  227. internal_check(treetyp,ld,lt,rd,rt,allowed);
  228. internal_check(treetyp,rd,rt,ld,lt,allowed);
  229. isbinaryoperatoroverloadable:=allowed;
  230. end;
  231. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  232. begin
  233. isunaryoperatoroverloadable:=false;
  234. case treetyp of
  235. assignn :
  236. begin
  237. if (rd.deftype=orddef) and
  238. (dd.deftype=orddef) then
  239. begin
  240. isunaryoperatoroverloadable:=false;
  241. exit;
  242. end;
  243. isunaryoperatoroverloadable:=true;
  244. end;
  245. subn :
  246. begin
  247. if is_integer(rd) or
  248. (rd.deftype=floatdef) then
  249. begin
  250. isunaryoperatoroverloadable:=false;
  251. exit;
  252. end;
  253. {$ifdef SUPPORT_MMX}
  254. if (cs_mmx in aktlocalswitches) and
  255. is_mmx_able_array(rd) then
  256. begin
  257. isunaryoperatoroverloadable:=false;
  258. exit;
  259. end;
  260. {$endif SUPPORT_MMX}
  261. isunaryoperatoroverloadable:=true;
  262. end;
  263. notn :
  264. begin
  265. if is_integer(rd) or
  266. is_boolean(rd) then
  267. begin
  268. isunaryoperatoroverloadable:=false;
  269. exit;
  270. end;
  271. {$ifdef SUPPORT_MMX}
  272. if (cs_mmx in aktlocalswitches) and
  273. is_mmx_able_array(rd) then
  274. begin
  275. isunaryoperatoroverloadable:=false;
  276. exit;
  277. end;
  278. {$endif SUPPORT_MMX}
  279. isunaryoperatoroverloadable:=true;
  280. end;
  281. end;
  282. end;
  283. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  284. var
  285. ld,rd,dd : tdef;
  286. i : longint;
  287. begin
  288. case pf.parast.symindex.count of
  289. 2 : begin
  290. isoperatoracceptable:=false;
  291. for i:=1 to tok2nodes do
  292. if tok2node[i].tok=optoken then
  293. begin
  294. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  295. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  296. dd:=pf.rettype.def;
  297. isoperatoracceptable:=
  298. tok2node[i].op_overloading_supported and
  299. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  300. break;
  301. end;
  302. end;
  303. 1 : begin
  304. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  305. dd:=pf.rettype.def;
  306. for i:=1 to tok2nodes do
  307. if tok2node[i].tok=optoken then
  308. begin
  309. isoperatoracceptable:=
  310. tok2node[i].op_overloading_supported and
  311. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  312. break;
  313. end;
  314. end;
  315. else
  316. isoperatoracceptable:=false;
  317. end;
  318. end;
  319. function isbinaryoverloaded(var t : tnode) : boolean;
  320. var
  321. rd,ld : tdef;
  322. optoken : ttoken;
  323. operpd : tprocdef;
  324. ht : tnode;
  325. begin
  326. isbinaryoverloaded:=false;
  327. operpd:=nil;
  328. { load easier access variables }
  329. ld:=tbinarynode(t).left.resulttype.def;
  330. rd:=tbinarynode(t).right.resulttype.def;
  331. if isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  332. begin
  333. isbinaryoverloaded:=true;
  334. case t.nodetype of
  335. equaln,
  336. unequaln :
  337. optoken:=_EQUAL;
  338. addn:
  339. optoken:=_PLUS;
  340. subn:
  341. optoken:=_MINUS;
  342. muln:
  343. optoken:=_STAR;
  344. starstarn:
  345. optoken:=_STARSTAR;
  346. slashn:
  347. optoken:=_SLASH;
  348. ltn:
  349. optoken:=tokens._lt;
  350. gtn:
  351. optoken:=tokens._gt;
  352. lten:
  353. optoken:=_lte;
  354. gten:
  355. optoken:=_gte;
  356. symdifn :
  357. optoken:=_SYMDIF;
  358. modn :
  359. optoken:=_OP_MOD;
  360. orn :
  361. optoken:=_OP_OR;
  362. xorn :
  363. optoken:=_OP_XOR;
  364. andn :
  365. optoken:=_OP_AND;
  366. divn :
  367. optoken:=_OP_DIV;
  368. shln :
  369. optoken:=_OP_SHL;
  370. shrn :
  371. optoken:=_OP_SHR;
  372. else
  373. exit;
  374. end;
  375. { check if the operator contains overloaded procdefs }
  376. if overloaded_operators[optoken]=nil then
  377. begin
  378. CGMessage(parser_e_operator_not_overloaded);
  379. isbinaryoverloaded:=false;
  380. exit;
  381. end;
  382. { Check if the assignment is available, if not then
  383. give a message that the types are not compatible }
  384. if optoken in [_EQUAL] then
  385. begin
  386. operpd:=overloaded_operators[optoken].search_procdef_binary_operator(ld,rd);
  387. if not assigned(operpd) then
  388. begin
  389. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  390. isbinaryoverloaded:=false;
  391. exit;
  392. end;
  393. end;
  394. { the nil as symtable signs firstcalln that this is
  395. an overloaded operator }
  396. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  397. inc(tcallnode(ht).symtableprocentry.refs);
  398. { we already know the procdef to use for equal, so it can
  399. skip the overload choosing in callnode.det_resulttype }
  400. if assigned(operpd) then
  401. tcallnode(ht).procdefinition:=operpd;
  402. { we need copies, because the originals will be destroyed when we give a }
  403. { changed node back to firstpass! (JM) }
  404. if assigned(tbinarynode(t).left) then
  405. if assigned(tbinarynode(t).right) then
  406. tcallnode(ht).left :=
  407. ccallparanode.create(tbinarynode(t).right.getcopy,
  408. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  409. else
  410. tcallnode(ht).left :=
  411. ccallparanode.create(nil,
  412. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  413. else if assigned(tbinarynode(t).right) then
  414. tcallnode(ht).left :=
  415. ccallparanode.create(tbinarynode(t).right.getcopy,
  416. ccallparanode.create(nil,nil));
  417. if t.nodetype=unequaln then
  418. ht:=cnotnode.create(ht);
  419. t:=ht;
  420. end;
  421. end;
  422. {****************************************************************************
  423. Register Calculation
  424. ****************************************************************************}
  425. { marks an lvalue as "unregable" }
  426. procedure make_not_regable(p : tnode);
  427. begin
  428. case p.nodetype of
  429. typeconvn :
  430. make_not_regable(ttypeconvnode(p).left);
  431. loadn :
  432. if tloadnode(p).symtableentry.typ=varsym then
  433. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  434. end;
  435. end;
  436. { calculates the needed registers for a binary operator }
  437. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  438. begin
  439. p.left_right_max;
  440. { Only when the difference between the left and right registers < the
  441. wanted registers allocate the amount of registers }
  442. if assigned(p.left) then
  443. begin
  444. if assigned(p.right) then
  445. begin
  446. { the location must be already filled in because we need it to }
  447. { calculate the necessary number of registers (JM) }
  448. if p.location.loc = LOC_INVALID then
  449. internalerror(200110101);
  450. if (abs(p.left.registers32-p.right.registers32)<r32) or
  451. ((p.location.loc = LOC_FPUREGISTER) and
  452. (p.right.registersfpu <= p.left.registersfpu) and
  453. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  454. (p.left.registers32 < p.right.registers32)) then
  455. inc(p.registers32,r32);
  456. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  457. inc(p.registersfpu,fpu);
  458. {$ifdef SUPPORT_MMX}
  459. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  460. inc(p.registersmmx,mmx);
  461. {$endif SUPPORT_MMX}
  462. { the following is a little bit guessing but I think }
  463. { it's the only way to solve same internalerrors: }
  464. { if the left and right node both uses registers }
  465. { and return a mem location, but the current node }
  466. { doesn't use an integer register we get probably }
  467. { trouble when restoring a node }
  468. if (p.left.registers32=p.right.registers32) and
  469. (p.registers32=p.left.registers32) and
  470. (p.registers32>0) and
  471. (p.left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  472. (p.right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  473. inc(p.registers32);
  474. end
  475. else
  476. begin
  477. if (p.left.registers32<r32) then
  478. inc(p.registers32,r32);
  479. if (p.left.registersfpu<fpu) then
  480. inc(p.registersfpu,fpu);
  481. {$ifdef SUPPORT_MMX}
  482. if (p.left.registersmmx<mmx) then
  483. inc(p.registersmmx,mmx);
  484. {$endif SUPPORT_MMX}
  485. end;
  486. end;
  487. { error CGMessage, if more than 8 floating point }
  488. { registers are needed }
  489. { if p.registersfpu>maxfpuregs then
  490. CGMessage(cg_e_too_complex_expr); now pushed if needed PM }
  491. end;
  492. {****************************************************************************
  493. Subroutine Handling
  494. ****************************************************************************}
  495. function is_procsym_load(p:tnode):boolean;
  496. begin
  497. { ignore vecn,subscriptn }
  498. repeat
  499. case p.nodetype of
  500. vecn :
  501. p:=tvecnode(p).left;
  502. subscriptn :
  503. p:=tsubscriptnode(p).left;
  504. else
  505. break;
  506. end;
  507. until false;
  508. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  509. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  510. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  511. end;
  512. { local routines can't be assigned to procvars }
  513. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  514. begin
  515. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  516. CGMessage(type_e_cannot_local_proc_to_procvar);
  517. end;
  518. procedure set_varstate(p : tnode;must_be_valid : boolean);
  519. var
  520. hsym : tvarsym;
  521. begin
  522. while assigned(p) do
  523. begin
  524. if (nf_varstateset in p.flags) then
  525. exit;
  526. include(p.flags,nf_varstateset);
  527. case p.nodetype of
  528. typeconvn :
  529. begin
  530. case ttypeconvnode(p).convtype of
  531. tc_cchar_2_pchar,
  532. tc_cstring_2_pchar,
  533. tc_array_2_pointer :
  534. must_be_valid:=false;
  535. tc_pchar_2_string,
  536. tc_pointer_2_array :
  537. must_be_valid:=true;
  538. end;
  539. p:=tunarynode(p).left;
  540. end;
  541. subscriptn :
  542. p:=tunarynode(p).left;
  543. vecn:
  544. begin
  545. set_varstate(tbinarynode(p).right,true);
  546. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  547. must_be_valid:=true;
  548. p:=tunarynode(p).left;
  549. end;
  550. { do not parse calln }
  551. calln :
  552. break;
  553. callparan :
  554. begin
  555. set_varstate(tbinarynode(p).right,must_be_valid);
  556. p:=tunarynode(p).left;
  557. end;
  558. loadn :
  559. begin
  560. if (tloadnode(p).symtableentry.typ=varsym) then
  561. begin
  562. hsym:=tvarsym(tloadnode(p).symtableentry);
  563. if must_be_valid and (nf_first in p.flags) then
  564. begin
  565. if (hsym.varstate=vs_declared_and_first_found) or
  566. (hsym.varstate=vs_set_but_first_not_passed) then
  567. begin
  568. if (assigned(hsym.owner) and
  569. assigned(aktprocsym) and
  570. (hsym.owner = aktprocdef.localst)) then
  571. begin
  572. if tloadnode(p).symtable.symtabletype=localsymtable then
  573. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  574. else
  575. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  576. end;
  577. end;
  578. end;
  579. if (nf_first in p.flags) then
  580. begin
  581. if hsym.varstate=vs_declared_and_first_found then
  582. begin
  583. { this can only happen at left of an assignment, no ? PM }
  584. if (parsing_para_level=0) and not must_be_valid then
  585. hsym.varstate:=vs_assigned
  586. else
  587. hsym.varstate:=vs_used;
  588. end
  589. else
  590. if hsym.varstate=vs_set_but_first_not_passed then
  591. hsym.varstate:=vs_used;
  592. exclude(p.flags,nf_first);
  593. end
  594. else
  595. begin
  596. if (hsym.varstate=vs_assigned) and
  597. (must_be_valid or (parsing_para_level>0) or
  598. (p.resulttype.def.deftype=procvardef)) then
  599. hsym.varstate:=vs_used;
  600. if (hsym.varstate=vs_declared_and_first_found) and
  601. (must_be_valid or (parsing_para_level>0) or
  602. (p.resulttype.def.deftype=procvardef)) then
  603. hsym.varstate:=vs_set_but_first_not_passed;
  604. end;
  605. end;
  606. break;
  607. end;
  608. funcretn:
  609. begin
  610. { no claim if setting higher return value_str }
  611. if must_be_valid and
  612. (lexlevel=tfuncretnode(p).funcretsym.owner.symtablelevel) and
  613. ((tfuncretnode(p).funcretsym.funcretstate=vs_declared) or
  614. ((nf_is_first_funcret in p.flags) and
  615. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found))) then
  616. begin
  617. CGMessage(sym_w_function_result_not_set);
  618. { avoid multiple warnings }
  619. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  620. end;
  621. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  622. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  623. break;
  624. end;
  625. else
  626. break;
  627. end;{case }
  628. end;
  629. end;
  630. procedure unset_varstate(p : tnode);
  631. begin
  632. while assigned(p) do
  633. begin
  634. exclude(p.flags,nf_varstateset);
  635. case p.nodetype of
  636. typeconvn,
  637. subscriptn,
  638. vecn :
  639. p:=tunarynode(p).left;
  640. else
  641. break;
  642. end;
  643. end;
  644. end;
  645. procedure set_unique(p : tnode);
  646. begin
  647. while assigned(p) do
  648. begin
  649. case p.nodetype of
  650. vecn:
  651. begin
  652. include(p.flags,nf_callunique);
  653. break;
  654. end;
  655. typeconvn,
  656. subscriptn,
  657. derefn:
  658. p:=tunarynode(p).left;
  659. else
  660. break;
  661. end;
  662. end;
  663. end;
  664. procedure set_funcret_is_valid(p:tnode);
  665. begin
  666. while assigned(p) do
  667. begin
  668. case p.nodetype of
  669. funcretn:
  670. begin
  671. if (nf_is_first_funcret in p.flags) or
  672. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found) then
  673. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  674. break;
  675. end;
  676. vecn,
  677. {derefn,}
  678. typeconvn,
  679. subscriptn:
  680. p:=tunarynode(p).left;
  681. else
  682. break;
  683. end;
  684. end;
  685. end;
  686. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  687. var
  688. hp : tnode;
  689. gotwith,
  690. gotsubscript,
  691. gotpointer,
  692. gotvec,
  693. gotclass,
  694. gotderef : boolean;
  695. fromdef,
  696. todef : tdef;
  697. begin
  698. valid_for_assign:=false;
  699. gotsubscript:=false;
  700. gotvec:=false;
  701. gotderef:=false;
  702. gotclass:=false;
  703. gotpointer:=false;
  704. gotwith:=false;
  705. hp:=p;
  706. if not(valid_void in opts) and
  707. is_void(hp.resulttype.def) then
  708. begin
  709. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  710. exit;
  711. end;
  712. while assigned(hp) do
  713. begin
  714. { property allowed? calln has a property check itself }
  715. if (nf_isproperty in hp.flags) then
  716. begin
  717. if (valid_property in opts) then
  718. valid_for_assign:=true
  719. else
  720. begin
  721. { check return type }
  722. case hp.resulttype.def.deftype of
  723. pointerdef :
  724. gotpointer:=true;
  725. objectdef :
  726. gotclass:=is_class_or_interface(hp.resulttype.def);
  727. recorddef, { handle record like class it needs a subscription }
  728. classrefdef :
  729. gotclass:=true;
  730. end;
  731. { 1. if it returns a pointer and we've found a deref,
  732. 2. if it returns a class or record and a subscription or with is found }
  733. if (gotpointer and gotderef) or
  734. (gotclass and (gotsubscript or gotwith)) then
  735. valid_for_assign:=true
  736. else
  737. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  738. end;
  739. exit;
  740. end;
  741. case hp.nodetype of
  742. temprefn :
  743. begin
  744. valid_for_assign := true;
  745. exit;
  746. end;
  747. derefn :
  748. begin
  749. gotderef:=true;
  750. hp:=tderefnode(hp).left;
  751. end;
  752. typeconvn :
  753. begin
  754. { typecast sizes must match, exceptions:
  755. - from formaldef
  756. - from void
  757. - typecast from pointer to array }
  758. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  759. todef:=hp.resulttype.def;
  760. if not((fromdef.deftype=formaldef) or
  761. is_void(fromdef) or
  762. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  763. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  764. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  765. (fromdef.size<>todef.size) then
  766. begin
  767. { in TP it is allowed to typecast to smaller types }
  768. if not(m_tp7 in aktmodeswitches) or
  769. (todef.size>fromdef.size) then
  770. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  771. end;
  772. case hp.resulttype.def.deftype of
  773. pointerdef :
  774. gotpointer:=true;
  775. objectdef :
  776. gotclass:=is_class_or_interface(hp.resulttype.def);
  777. classrefdef :
  778. gotclass:=true;
  779. arraydef :
  780. begin
  781. { pointer -> array conversion is done then we need to see it
  782. as a deref, because a ^ is then not required anymore }
  783. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  784. gotderef:=true;
  785. end;
  786. end;
  787. hp:=ttypeconvnode(hp).left;
  788. end;
  789. vecn :
  790. begin
  791. gotvec:=true;
  792. hp:=tunarynode(hp).left;
  793. end;
  794. asn :
  795. hp:=tunarynode(hp).left;
  796. subscriptn :
  797. begin
  798. gotsubscript:=true;
  799. { a class/interface access is an implicit }
  800. { dereferencing }
  801. hp:=tsubscriptnode(hp).left;
  802. if is_class_or_interface(hp.resulttype.def) then
  803. gotderef:=true;
  804. end;
  805. subn,
  806. addn :
  807. begin
  808. { Allow add/sub operators on a pointer, or an integer
  809. and a pointer typecast and deref has been found }
  810. if ((hp.resulttype.def.deftype=pointerdef) or
  811. (is_integer(hp.resulttype.def) and gotpointer)) and
  812. gotderef then
  813. valid_for_assign:=true
  814. else
  815. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  816. exit;
  817. end;
  818. addrn :
  819. begin
  820. if gotderef or
  821. (nf_procvarload in hp.flags) then
  822. valid_for_assign:=true
  823. else
  824. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  825. exit;
  826. end;
  827. selfn,
  828. funcretn :
  829. begin
  830. valid_for_assign:=true;
  831. exit;
  832. end;
  833. calln :
  834. begin
  835. { check return type }
  836. case hp.resulttype.def.deftype of
  837. arraydef :
  838. begin
  839. { dynamic arrays are allowed when there is also a
  840. vec node }
  841. if is_dynamic_array(hp.resulttype.def) and
  842. gotvec then
  843. begin
  844. gotderef:=true;
  845. gotpointer:=true;
  846. end;
  847. end;
  848. pointerdef :
  849. gotpointer:=true;
  850. objectdef :
  851. gotclass:=is_class_or_interface(hp.resulttype.def);
  852. recorddef, { handle record like class it needs a subscription }
  853. classrefdef :
  854. gotclass:=true;
  855. end;
  856. { 1. if it returns a pointer and we've found a deref,
  857. 2. if it returns a class or record and a subscription or with is found }
  858. if (gotpointer and gotderef) or
  859. (gotclass and (gotsubscript or gotwith)) then
  860. valid_for_assign:=true
  861. else
  862. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  863. exit;
  864. end;
  865. loadn :
  866. begin
  867. case tloadnode(hp).symtableentry.typ of
  868. absolutesym,
  869. varsym :
  870. begin
  871. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  872. begin
  873. { allow p^:= constructions with p is const parameter }
  874. if gotderef then
  875. valid_for_assign:=true
  876. else
  877. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  878. exit;
  879. end;
  880. { Are we at a with symtable, then we need to process the
  881. withrefnode also to check for maybe a const load }
  882. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  883. begin
  884. { continue with processing the withref node }
  885. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  886. gotwith:=true;
  887. end
  888. else
  889. begin
  890. { set the assigned flag for varsyms }
  891. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  892. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  893. valid_for_assign:=true;
  894. exit;
  895. end;
  896. end;
  897. funcretsym :
  898. begin
  899. valid_for_assign:=true;
  900. exit;
  901. end;
  902. typedconstsym :
  903. begin
  904. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  905. valid_for_assign:=true
  906. else
  907. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  908. exit;
  909. end;
  910. else
  911. begin
  912. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  913. exit;
  914. end;
  915. end;
  916. end;
  917. else
  918. begin
  919. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  920. exit;
  921. end;
  922. end;
  923. end;
  924. end;
  925. function valid_for_var(p:tnode):boolean;
  926. begin
  927. valid_for_var:=valid_for_assign(p,[]);
  928. end;
  929. function valid_for_formal_var(p : tnode) : boolean;
  930. begin
  931. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  932. end;
  933. function valid_for_formal_const(p : tnode) : boolean;
  934. var
  935. v : boolean;
  936. begin
  937. { p must have been firstpass'd before }
  938. { accept about anything but not a statement ! }
  939. case p.nodetype of
  940. calln,
  941. statementn,
  942. addrn :
  943. begin
  944. { addrn is not allowed as this generate a constant value,
  945. but a tp procvar are allowed (PFV) }
  946. if nf_procvarload in p.flags then
  947. v:=true
  948. else
  949. v:=false;
  950. end;
  951. else
  952. v:=true;
  953. end;
  954. valid_for_formal_const:=v;
  955. end;
  956. function valid_for_assignment(p:tnode):boolean;
  957. begin
  958. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  959. end;
  960. end.
  961. {
  962. $Log$
  963. Revision 1.56 2003-01-02 19:50:21 peter
  964. * fixed operator checking for objects
  965. * made binary operator checking simpeler
  966. Revision 1.55 2002/12/27 18:06:32 peter
  967. * fix overload error for dynarr:=nil
  968. Revision 1.54 2002/12/22 16:34:49 peter
  969. * proc-procvar crash fixed (tw2277)
  970. Revision 1.53 2002/12/11 22:39:24 peter
  971. * better error message when no operator is found for equal
  972. Revision 1.52 2002/11/27 22:11:59 peter
  973. * rewrote isbinaryoverloadable to use a case. it's now much easier
  974. to understand what is happening
  975. Revision 1.51 2002/11/25 17:43:17 peter
  976. * splitted defbase in defutil,symutil,defcmp
  977. * merged isconvertable and is_equal into compare_defs(_ext)
  978. * made operator search faster by walking the list only once
  979. Revision 1.50 2002/10/07 20:12:08 peter
  980. * ugly hack to fix tb0411
  981. Revision 1.49 2002/10/05 00:47:03 peter
  982. * support dynamicarray<>nil
  983. Revision 1.48 2002/10/04 21:13:59 peter
  984. * ignore vecn,subscriptn when checking for a procvar loadn
  985. Revision 1.47 2002/09/16 18:09:34 peter
  986. * set_funcret_valid fixed when result was already used in a nested
  987. procedure
  988. Revision 1.46 2002/07/20 11:57:53 florian
  989. * types.pas renamed to defbase.pas because D6 contains a types
  990. unit so this would conflicts if D6 programms are compiled
  991. + Willamette/SSE2 instructions to assembler added
  992. Revision 1.45 2002/05/18 13:34:08 peter
  993. * readded missing revisions
  994. Revision 1.44 2002/05/16 19:46:37 carl
  995. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  996. + try to fix temp allocation (still in ifdef)
  997. + generic constructor calls
  998. + start of tassembler / tmodulebase class cleanup
  999. Revision 1.42 2002/04/02 17:11:28 peter
  1000. * tlocation,treference update
  1001. * LOC_CONSTANT added for better constant handling
  1002. * secondadd splitted in multiple routines
  1003. * location_force_reg added for loading a location to a register
  1004. of a specified size
  1005. * secondassignment parses now first the right and then the left node
  1006. (this is compatible with Kylix). This saves a lot of push/pop especially
  1007. with string operations
  1008. * adapted some routines to use the new cg methods
  1009. Revision 1.41 2002/01/16 09:33:46 jonas
  1010. * no longer allow assignments to pointer expressions (unless there's a
  1011. deref), reported by John Lee
  1012. }