htypechk.pas 41 KB

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