htypechk.pas 41 KB

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