htypechk.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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(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. defbase,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. { ignore vecn,subscriptn }
  429. repeat
  430. case p.nodetype of
  431. vecn :
  432. p:=tvecnode(p).left;
  433. subscriptn :
  434. p:=tsubscriptnode(p).left;
  435. else
  436. break;
  437. end;
  438. until false;
  439. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  440. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  441. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  442. end;
  443. { change a proc call to a procload for assignment to a procvar }
  444. { this can only happen for proc/function without arguments }
  445. function is_procsym_call(p:tnode):boolean;
  446. begin
  447. is_procsym_call:=(p.nodetype=calln) and (tcallnode(p).left=nil) and
  448. (((tcallnode(p).symtableprocentry.typ=procsym) and (tcallnode(p).right=nil)) or
  449. (assigned(tcallnode(p).right) and (tcallnode(tcallnode(p).right).symtableprocentry.typ=varsym)));
  450. end;
  451. { local routines can't be assigned to procvars }
  452. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  453. begin
  454. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  455. CGMessage(type_e_cannot_local_proc_to_procvar);
  456. end;
  457. procedure set_varstate(p : tnode;must_be_valid : boolean);
  458. var
  459. hsym : tvarsym;
  460. begin
  461. while assigned(p) do
  462. begin
  463. if (nf_varstateset in p.flags) then
  464. exit;
  465. include(p.flags,nf_varstateset);
  466. case p.nodetype of
  467. typeconvn :
  468. begin
  469. case ttypeconvnode(p).convtype of
  470. tc_cchar_2_pchar,
  471. tc_cstring_2_pchar,
  472. tc_array_2_pointer :
  473. must_be_valid:=false;
  474. tc_pchar_2_string,
  475. tc_pointer_2_array :
  476. must_be_valid:=true;
  477. end;
  478. p:=tunarynode(p).left;
  479. end;
  480. subscriptn :
  481. p:=tunarynode(p).left;
  482. vecn:
  483. begin
  484. set_varstate(tbinarynode(p).right,true);
  485. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  486. must_be_valid:=true;
  487. p:=tunarynode(p).left;
  488. end;
  489. { do not parse calln }
  490. calln :
  491. break;
  492. callparan :
  493. begin
  494. set_varstate(tbinarynode(p).right,must_be_valid);
  495. p:=tunarynode(p).left;
  496. end;
  497. loadn :
  498. begin
  499. if (tloadnode(p).symtableentry.typ=varsym) then
  500. begin
  501. hsym:=tvarsym(tloadnode(p).symtableentry);
  502. if must_be_valid and (nf_first in p.flags) then
  503. begin
  504. if (hsym.varstate=vs_declared_and_first_found) or
  505. (hsym.varstate=vs_set_but_first_not_passed) then
  506. begin
  507. if (assigned(hsym.owner) and
  508. assigned(aktprocsym) and
  509. (hsym.owner = aktprocdef.localst)) then
  510. begin
  511. if tloadnode(p).symtable.symtabletype=localsymtable then
  512. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  513. else
  514. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  515. end;
  516. end;
  517. end;
  518. if (nf_first in p.flags) then
  519. begin
  520. if hsym.varstate=vs_declared_and_first_found then
  521. begin
  522. { this can only happen at left of an assignment, no ? PM }
  523. if (parsing_para_level=0) and not must_be_valid then
  524. hsym.varstate:=vs_assigned
  525. else
  526. hsym.varstate:=vs_used;
  527. end
  528. else
  529. if hsym.varstate=vs_set_but_first_not_passed then
  530. hsym.varstate:=vs_used;
  531. exclude(p.flags,nf_first);
  532. end
  533. else
  534. begin
  535. if (hsym.varstate=vs_assigned) and
  536. (must_be_valid or (parsing_para_level>0) or
  537. (p.resulttype.def.deftype=procvardef)) then
  538. hsym.varstate:=vs_used;
  539. if (hsym.varstate=vs_declared_and_first_found) and
  540. (must_be_valid or (parsing_para_level>0) or
  541. (p.resulttype.def.deftype=procvardef)) then
  542. hsym.varstate:=vs_set_but_first_not_passed;
  543. end;
  544. end;
  545. break;
  546. end;
  547. funcretn:
  548. begin
  549. { no claim if setting higher return value_str }
  550. if must_be_valid and
  551. (lexlevel=tfuncretnode(p).funcretsym.owner.symtablelevel) and
  552. ((tfuncretnode(p).funcretsym.funcretstate=vs_declared) or
  553. ((nf_is_first_funcret in p.flags) and
  554. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found))) then
  555. begin
  556. CGMessage(sym_w_function_result_not_set);
  557. { avoid multiple warnings }
  558. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  559. end;
  560. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  561. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  562. break;
  563. end;
  564. else
  565. break;
  566. end;{case }
  567. end;
  568. end;
  569. procedure unset_varstate(p : tnode);
  570. begin
  571. while assigned(p) do
  572. begin
  573. exclude(p.flags,nf_varstateset);
  574. case p.nodetype of
  575. typeconvn,
  576. subscriptn,
  577. vecn :
  578. p:=tunarynode(p).left;
  579. else
  580. break;
  581. end;
  582. end;
  583. end;
  584. procedure set_unique(p : tnode);
  585. begin
  586. while assigned(p) do
  587. begin
  588. case p.nodetype of
  589. vecn:
  590. begin
  591. include(p.flags,nf_callunique);
  592. break;
  593. end;
  594. typeconvn,
  595. subscriptn,
  596. derefn:
  597. p:=tunarynode(p).left;
  598. else
  599. break;
  600. end;
  601. end;
  602. end;
  603. procedure set_funcret_is_valid(p:tnode);
  604. begin
  605. while assigned(p) do
  606. begin
  607. case p.nodetype of
  608. funcretn:
  609. begin
  610. if (nf_is_first_funcret in p.flags) or
  611. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found) then
  612. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  613. break;
  614. end;
  615. vecn,
  616. {derefn,}
  617. typeconvn,
  618. subscriptn:
  619. p:=tunarynode(p).left;
  620. else
  621. break;
  622. end;
  623. end;
  624. end;
  625. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  626. var
  627. hp : tnode;
  628. gotwith,
  629. gotsubscript,
  630. gotpointer,
  631. gotclass,
  632. gotderef : boolean;
  633. fromdef,
  634. todef : tdef;
  635. begin
  636. valid_for_assign:=false;
  637. gotsubscript:=false;
  638. gotderef:=false;
  639. gotclass:=false;
  640. gotpointer:=false;
  641. gotwith:=false;
  642. hp:=p;
  643. if not(valid_void in opts) and
  644. is_void(hp.resulttype.def) then
  645. begin
  646. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  647. exit;
  648. end;
  649. while assigned(hp) do
  650. begin
  651. { property allowed? calln has a property check itself }
  652. if (nf_isproperty in hp.flags) then
  653. begin
  654. if (valid_property in opts) then
  655. valid_for_assign:=true
  656. else
  657. begin
  658. { check return type }
  659. case hp.resulttype.def.deftype of
  660. pointerdef :
  661. gotpointer:=true;
  662. objectdef :
  663. gotclass:=is_class_or_interface(hp.resulttype.def);
  664. recorddef, { handle record like class it needs a subscription }
  665. classrefdef :
  666. gotclass:=true;
  667. end;
  668. { 1. if it returns a pointer and we've found a deref,
  669. 2. if it returns a class or record and a subscription or with is found }
  670. if (gotpointer and gotderef) or
  671. (gotclass and (gotsubscript or gotwith)) then
  672. valid_for_assign:=true
  673. else
  674. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  675. end;
  676. exit;
  677. end;
  678. case hp.nodetype of
  679. temprefn :
  680. begin
  681. valid_for_assign := true;
  682. exit;
  683. end;
  684. derefn :
  685. begin
  686. gotderef:=true;
  687. hp:=tderefnode(hp).left;
  688. end;
  689. typeconvn :
  690. begin
  691. { typecast sizes must match, exceptions:
  692. - from formaldef
  693. - from void
  694. - typecast from pointer to array }
  695. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  696. todef:=hp.resulttype.def;
  697. if not((fromdef.deftype=formaldef) or
  698. is_void(fromdef) or
  699. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  700. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  701. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  702. (fromdef.size<>todef.size) then
  703. begin
  704. { in TP it is allowed to typecast to smaller types }
  705. if not(m_tp7 in aktmodeswitches) or
  706. (todef.size>fromdef.size) then
  707. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  708. end;
  709. case hp.resulttype.def.deftype of
  710. pointerdef :
  711. gotpointer:=true;
  712. objectdef :
  713. gotclass:=is_class_or_interface(hp.resulttype.def);
  714. classrefdef :
  715. gotclass:=true;
  716. arraydef :
  717. begin
  718. { pointer -> array conversion is done then we need to see it
  719. as a deref, because a ^ is then not required anymore }
  720. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  721. gotderef:=true;
  722. end;
  723. end;
  724. hp:=ttypeconvnode(hp).left;
  725. end;
  726. vecn,
  727. asn :
  728. hp:=tunarynode(hp).left;
  729. subscriptn :
  730. begin
  731. gotsubscript:=true;
  732. { a class/interface access is an implicit }
  733. { dereferencing }
  734. hp:=tsubscriptnode(hp).left;
  735. if is_class_or_interface(hp.resulttype.def) then
  736. gotderef:=true;
  737. end;
  738. subn,
  739. addn :
  740. begin
  741. { Allow add/sub operators on a pointer, or an integer
  742. and a pointer typecast and deref has been found }
  743. if ((hp.resulttype.def.deftype=pointerdef) or
  744. (is_integer(hp.resulttype.def) and gotpointer)) and
  745. gotderef then
  746. valid_for_assign:=true
  747. else
  748. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  749. exit;
  750. end;
  751. addrn :
  752. begin
  753. if gotderef or
  754. (nf_procvarload in hp.flags) then
  755. valid_for_assign:=true
  756. else
  757. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  758. exit;
  759. end;
  760. selfn,
  761. funcretn :
  762. begin
  763. valid_for_assign:=true;
  764. exit;
  765. end;
  766. calln :
  767. begin
  768. { check return type }
  769. case hp.resulttype.def.deftype of
  770. pointerdef :
  771. gotpointer:=true;
  772. objectdef :
  773. gotclass:=is_class_or_interface(hp.resulttype.def);
  774. recorddef, { handle record like class it needs a subscription }
  775. classrefdef :
  776. gotclass:=true;
  777. end;
  778. { 1. if it returns a pointer and we've found a deref,
  779. 2. if it returns a class or record and a subscription or with is found }
  780. if (gotpointer and gotderef) or
  781. (gotclass and (gotsubscript or gotwith)) then
  782. valid_for_assign:=true
  783. else
  784. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  785. exit;
  786. end;
  787. loadn :
  788. begin
  789. case tloadnode(hp).symtableentry.typ of
  790. absolutesym,
  791. varsym :
  792. begin
  793. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  794. begin
  795. { allow p^:= constructions with p is const parameter }
  796. if gotderef then
  797. valid_for_assign:=true
  798. else
  799. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  800. exit;
  801. end;
  802. { Are we at a with symtable, then we need to process the
  803. withrefnode also to check for maybe a const load }
  804. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  805. begin
  806. { continue with processing the withref node }
  807. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  808. gotwith:=true;
  809. end
  810. else
  811. begin
  812. { set the assigned flag for varsyms }
  813. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  814. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  815. valid_for_assign:=true;
  816. exit;
  817. end;
  818. end;
  819. funcretsym :
  820. begin
  821. valid_for_assign:=true;
  822. exit;
  823. end;
  824. typedconstsym :
  825. begin
  826. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  827. valid_for_assign:=true
  828. else
  829. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  830. exit;
  831. end;
  832. else
  833. begin
  834. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  835. exit;
  836. end;
  837. end;
  838. end;
  839. else
  840. begin
  841. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  842. exit;
  843. end;
  844. end;
  845. end;
  846. end;
  847. function valid_for_var(p:tnode):boolean;
  848. begin
  849. valid_for_var:=valid_for_assign(p,[]);
  850. end;
  851. function valid_for_formal_var(p : tnode) : boolean;
  852. begin
  853. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  854. end;
  855. function valid_for_formal_const(p : tnode) : boolean;
  856. var
  857. v : boolean;
  858. begin
  859. { p must have been firstpass'd before }
  860. { accept about anything but not a statement ! }
  861. case p.nodetype of
  862. calln,
  863. statementn,
  864. addrn :
  865. begin
  866. { addrn is not allowed as this generate a constant value,
  867. but a tp procvar are allowed (PFV) }
  868. if nf_procvarload in p.flags then
  869. v:=true
  870. else
  871. v:=false;
  872. end;
  873. else
  874. v:=true;
  875. end;
  876. valid_for_formal_const:=v;
  877. end;
  878. function valid_for_assignment(p:tnode):boolean;
  879. begin
  880. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  881. end;
  882. end.
  883. {
  884. $Log$
  885. Revision 1.48 2002-10-04 21:13:59 peter
  886. * ignore vecn,subscriptn when checking for a procvar loadn
  887. Revision 1.47 2002/09/16 18:09:34 peter
  888. * set_funcret_valid fixed when result was already used in a nested
  889. procedure
  890. Revision 1.46 2002/07/20 11:57:53 florian
  891. * types.pas renamed to defbase.pas because D6 contains a types
  892. unit so this would conflicts if D6 programms are compiled
  893. + Willamette/SSE2 instructions to assembler added
  894. Revision 1.45 2002/05/18 13:34:08 peter
  895. * readded missing revisions
  896. Revision 1.44 2002/05/16 19:46:37 carl
  897. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  898. + try to fix temp allocation (still in ifdef)
  899. + generic constructor calls
  900. + start of tassembler / tmodulebase class cleanup
  901. Revision 1.42 2002/04/02 17:11:28 peter
  902. * tlocation,treference update
  903. * LOC_CONSTANT added for better constant handling
  904. * secondadd splitted in multiple routines
  905. * location_force_reg added for loading a location to a register
  906. of a specified size
  907. * secondassignment parses now first the right and then the left node
  908. (this is compatible with Kylix). This saves a lot of push/pop especially
  909. with string operations
  910. * adapted some routines to use the new cg methods
  911. Revision 1.41 2002/01/16 09:33:46 jonas
  912. * no longer allow assignments to pointer expressions (unless there's a
  913. deref), reported by John Lee
  914. }