1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
{
Copyright (c) 1998-2002 by Florian Klaempfl
This unit handles the pass_typecheck and node conversion pass
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit pass_1;
{$i fpcdefs.inc}
interface
uses
node;
procedure typecheckpass(var p : tnode);
function do_typecheckpass(var p : tnode) : boolean;
function do_typecheckpass_changed(var p : tnode; out nodechanged: boolean) : boolean;
procedure firstpass(var p : tnode);
function do_firstpass(var p : tnode) : boolean;
{$ifdef state_tracking}
procedure do_track_state_pass(p:Tnode);
{$endif}
implementation
uses
globtype,comphook,systems,cclasses,
cutils,globals,
procinfo,
cgbase,symdef
{$ifdef extdebug}
,verbose,htypechk
{$endif extdebug}
{$ifdef state_tracking}
,nstate
{$endif}
;
{*****************************************************************************
Global procedures
*****************************************************************************}
procedure typecheckpass_internal(var p : tnode; out node_changed: boolean);
var
oldcodegenerror : boolean;
oldlocalswitches : tlocalswitches;
oldverbosity : longint;
oldpos : tfileposinfo;
hp : tnode;
begin
node_changed:=false;
if (p.resultdef=nil) then
begin
oldcodegenerror:=codegenerror;
oldpos:=current_filepos;
oldlocalswitches:=current_settings.localswitches;
oldverbosity:=status.verbosity;
codegenerror:=false;
current_filepos:=p.fileinfo;
current_settings.localswitches:=p.localswitches;
status.verbosity:=p.verbosity;
hp:=p.pass_typecheck;
{ should the node be replaced? }
if assigned(hp) then
begin
node_changed:=true;
p.free;
{ switch to new node }
p:=hp;
{ run typecheckpass }
typecheckpass(p);
end;
current_settings.localswitches:=oldlocalswitches;
current_filepos:=oldpos;
status.verbosity:=oldverbosity;
if codegenerror then
begin
include(p.flags,nf_error);
{ default to errortype if no type is set yet }
if p.resultdef=nil then
p.resultdef:=generrordef;
end;
codegenerror:=codegenerror or oldcodegenerror;
end
else
begin
{ update the codegenerror boolean with the previous result of this node }
if (nf_error in p.flags) then
codegenerror:=true;
end;
end;
procedure typecheckpass(var p : tnode);
var
node_changed: boolean;
begin
typecheckpass_internal(p,node_changed);
end;
function do_typecheckpass_changed(var p : tnode; out nodechanged: boolean) : boolean;
begin
codegenerror:=false;
typecheckpass_internal(p,nodechanged);
do_typecheckpass_changed:=codegenerror;
end;
function do_typecheckpass(var p : tnode) : boolean;
var
nodechanged: boolean;
begin
result:=do_typecheckpass_changed(p,nodechanged);
end;
procedure firstpass(var p : tnode);
var
oldcodegenerror : boolean;
oldlocalswitches : tlocalswitches;
oldpos : tfileposinfo;
oldverbosity: longint;
hp : tnode;
begin
if (nf_pass1_done in p.flags) then
exit;
if not(nf_error in p.flags) then
begin
oldcodegenerror:=codegenerror;
oldpos:=current_filepos;
oldlocalswitches:=current_settings.localswitches;
oldverbosity:=status.verbosity;
codegenerror:=false;
current_filepos:=p.fileinfo;
current_settings.localswitches:=p.localswitches;
status.verbosity:=p.verbosity;
{ checks make always a call }
if ([cs_check_range,cs_check_overflow,cs_check_stack] * current_settings.localswitches <> []) then
include(current_procinfo.flags,pi_do_call);
{ determine the resultdef if not done }
if (p.resultdef=nil) then
begin
hp:=p.pass_typecheck;
{ should the node be replaced? }
if assigned(hp) then
begin
p.free;
{ switch to new node }
p:=hp;
{ run typecheckpass }
typecheckpass(p);
end;
if codegenerror then
begin
include(p.flags,nf_error);
{ default to errortype if no type is set yet }
if p.resultdef=nil then
p.resultdef:=generrordef;
end;
codegenerror:=codegenerror or oldcodegenerror;
end;
if not(nf_error in p.flags) then
begin
{ first pass }
hp:=p.pass_1;
{ should the node be replaced? }
if assigned(hp) then
begin
p.free;
{ switch to new node }
p := hp;
{ run firstpass }
firstpass(p);
end
else
begin
{ inlining happens in pass_1 and can cause new }
{ simplify opportunities }
hp:=p.simplify(true);
if assigned(hp) then
begin
p.free;
p := hp;
firstpass(p);
end;
end;
if codegenerror then
include(p.flags,nf_error)
else
begin
{$ifdef EXTDEBUG}
if (p.expectloc=LOC_INVALID) then
Comment(V_Warning,'Expectloc is not set in firstpass: '+nodetype2str[p.nodetype]);
{$endif EXTDEBUG}
end;
end;
include(p.flags,nf_pass1_done);
codegenerror:=codegenerror or oldcodegenerror;
current_settings.localswitches:=oldlocalswitches;
current_filepos:=oldpos;
status.verbosity:=oldverbosity;
end
else
codegenerror:=true;
end;
function do_firstpass(var p : tnode) : boolean;
begin
codegenerror:=false;
firstpass(p);
{$ifdef state_tracking}
writeln('TRACKSTART');
writeln('before');
writenode(p);
do_track_state_pass(p);
writeln('after');
writenode(p);
writeln('TRACKDONE');
{$endif}
do_firstpass:=codegenerror;
end;
{$ifdef state_tracking}
procedure do_track_state_pass(p:Tnode);
begin
aktstate:=Tstate_storage.create;
p.track_state_pass(true);
aktstate.destroy;
end;
{$endif}
end.
|