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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
"""
mwrap_typecheck.py — Semantic analysis and type classification.
Copyright (c) 2007-2008 David Bindel
See the file COPYING for copying permissions
Converted to Python by Zydrunas Gimbutas (2026),
with assistance from Claude Code / Claude Opus 4.6 (Anthropic).
"""
import sys
from mwrap_ast import (
VT, Expr, TypeQual, Var, Func,
promote_int,
iospec_is_input, iospec_is_output, iospec_is_inonly,
)
# ---------------------------------------------------------------------------
# Label assignment
# ---------------------------------------------------------------------------
def _label_dim_args_expr(args, icount):
"""Assign input_label to dimension expressions; returns updated icount."""
for e in args:
e.input_label = icount
icount += 1
return icount
def _label_dim_args_var(vars, icount):
"""Walk var list, label dimension expressions."""
for v in vars:
if v.qual:
icount = _label_dim_args_expr(v.qual.args, icount)
return icount
def _label_args_var(vars, icount, ocount):
"""Assign input/output labels to vars; returns (icount, ocount)."""
for v in vars:
if iospec_is_input(v.iospec):
v.input_label = icount
icount += 1
if iospec_is_output(v.iospec):
v.output_label = ocount
ocount += 1
return icount, ocount
def label_args(f):
"""Assign prhs[] / plhs[] indices to a Func's variables."""
icount = 1 if f.thisv else 0
ocount = 0
icount, ocount = _label_args_var(f.ret, icount, ocount)
icount, ocount = _label_args_var(f.args, icount, ocount)
icount = _label_dim_args_var(f.ret, icount)
icount = _label_dim_args_var(f.args, icount)
# ---------------------------------------------------------------------------
# Type-info assignment
# ---------------------------------------------------------------------------
def _assign_scalar_tinfo(v, line, tags, tagp, tagr, taga, tagar):
"""Assign tinfo for a scalar/complex type. Returns error count."""
if not v.qual:
v.tinfo = tags
elif v.qual.qual == '*':
v.tinfo = tagp
elif v.qual.qual == '&':
v.tinfo = tagr
elif v.qual.qual == 'a':
v.tinfo = taga
# check max 2D
if len(v.qual.args) > 2:
print(f"Error ({line}): Array {v.name} should be 1D or 2D",
file=sys.stderr)
return 1
elif v.qual.qual == 'r':
v.tinfo = tagar
if tagar == VT.unk:
print(f"Error ({line}): Array ref {v.name} must be to a real array",
file=sys.stderr)
return 1
if len(v.qual.args) > 2:
print(f"Error ({line}): Array {v.name} should be 1D or 2D",
file=sys.stderr)
return 1
else:
assert False, f"Unknown qual '{v.qual.qual}'"
return 0
def assign_tinfo(ctx, v, line):
"""Assign VT_* tinfo to a single Var. Returns error count."""
bt = v.basetype
if ctx.is_scalar_type(bt):
return _assign_scalar_tinfo(v, line,
VT.scalar, VT.p_scalar, VT.r_scalar,
VT.array, VT.rarray)
elif ctx.is_cscalar_type(bt):
return _assign_scalar_tinfo(v, line,
VT.cscalar, VT.p_cscalar, VT.r_cscalar,
VT.carray, VT.unk)
elif ctx.is_zscalar_type(bt):
return _assign_scalar_tinfo(v, line,
VT.zscalar, VT.p_zscalar, VT.r_zscalar,
VT.zarray, VT.unk)
elif bt == "const":
if v.qual:
print(f"Error ({line}): Constant {v.name} cannot have modifiers",
file=sys.stderr)
return 1
v.tinfo = VT.const
# Strip quotes from name if present
if v.name and v.name.startswith("'"):
v.name = v.name.replace("'", "")
elif bt == "cstring":
if v.qual and v.qual.qual != 'a':
print(f"Error ({line}): String type {v.name} cannot have modifiers",
file=sys.stderr)
return 1
if v.qual and len(v.qual.args) > 1:
print(f"Error ({line}): Strings are one dimensional",
file=sys.stderr)
return 1
v.tinfo = VT.string
elif bt == "mxArray":
if v.qual:
print(f"Error ({line}): mxArray {v.name} cannot have modifiers",
file=sys.stderr)
return 1
v.tinfo = VT.mx
else:
# Object type
if not v.qual:
v.tinfo = VT.obj
elif v.qual.qual == '*':
v.tinfo = VT.p_obj
elif v.qual.qual == '&':
v.tinfo = VT.r_obj
elif v.qual.qual in ('a', 'r'):
print(f"Error ({line}): {v.name} cannot be an array of object {bt}",
file=sys.stderr)
return 1
else:
assert False
return 0
# ---------------------------------------------------------------------------
# Return-value validation
# ---------------------------------------------------------------------------
def _typecheck_return(ctx, ret, line):
if not ret:
return 0
v = ret[0]
err = assign_tinfo(ctx, v, line)
if v.tinfo in (VT.array, VT.carray, VT.zarray):
if not (v.qual and v.qual.args):
print(f"Error ({line}): Return array {v.name} must have dims",
file=sys.stderr)
err += 1
elif v.tinfo == VT.const:
print(f"Error ({line}): Cannot return constant", file=sys.stderr)
err += 1
elif v.tinfo == VT.rarray:
print(f"Error ({line}): Ref to array {v.name} looks just like array on return",
file=sys.stderr)
err += 1
elif v.tinfo == VT.string and v.qual:
print(f"Error ({line}): Return string {v.name} cannot have dims",
file=sys.stderr)
err += 1
return err
# ---------------------------------------------------------------------------
# Argument validation
# ---------------------------------------------------------------------------
def _typecheck_args(ctx, args, line):
err = 0
for v in args:
err += assign_tinfo(ctx, v, line)
if iospec_is_inonly(v.iospec):
continue
# Output / inout checks
if v.name and v.name[0:1].isdigit():
print(f"Error ({line}): Number {v.name} cannot be output",
file=sys.stderr)
err += 1
if (v.tinfo in (VT.obj, VT.p_obj, VT.r_obj) and
not ctx.is_mxarray_type(v.basetype)):
print(f"Error ({line}): Object {v.name} cannot be output",
file=sys.stderr)
err += 1
elif (v.tinfo in (VT.array, VT.carray, VT.zarray, VT.rarray) and
v.iospec == 'o' and
not (v.qual and v.qual.args)):
print(f"Error ({line}): Output array {v.name} must have dims",
file=sys.stderr)
err += 1
elif v.tinfo == VT.rarray and not iospec_is_output(v.iospec):
print(f"Error ({line}): Array ref {v.name} *must* be output",
file=sys.stderr)
err += 1
elif v.tinfo == VT.scalar:
print(f"Error ({line}): Scalar {v.name} cannot be output",
file=sys.stderr)
err += 1
elif v.tinfo == VT.const:
print(f"Error ({line}): Constant {v.name} cannot be output",
file=sys.stderr)
err += 1
elif v.tinfo == VT.string and not (v.qual and v.qual.args):
print(f"Error ({line}): String {v.name} cannot be output without size",
file=sys.stderr)
err += 1
elif v.tinfo == VT.mx and v.iospec == 'b':
print(f"Error ({line}): mxArray {v.name} cannot be used for inout",
file=sys.stderr)
err += 1
return err
# ---------------------------------------------------------------------------
# Fortran-ize arguments
# ---------------------------------------------------------------------------
def _fortranize_args_var(args, line):
"""Convert scalar args to pointer-to-scalar for FORTRAN. Returns error count."""
err = 0
for v in args:
if v.tinfo in (VT.obj, VT.p_obj, VT.r_obj):
print(f"Error ({line}): Cannot pass object {v.name} to FORTRAN",
file=sys.stderr)
err += 1
elif v.tinfo == VT.rarray:
print(f"Error ({line}): Cannot pass pointer ref {v.name} to FORTRAN",
file=sys.stderr)
err += 1
elif v.tinfo == VT.string:
print(f"Warning ({line}): Danger passing C string {v.name} to FORTRAN",
file=sys.stderr)
elif v.tinfo in (VT.scalar, VT.r_scalar):
v.tinfo = VT.p_scalar
elif v.tinfo in (VT.cscalar, VT.r_cscalar):
v.tinfo = VT.p_cscalar
elif v.tinfo in (VT.zscalar, VT.r_zscalar):
v.tinfo = VT.p_zscalar
return err
def _fortranize_ret(v, line):
if not v:
return 0
if v.tinfo in (VT.cscalar, VT.zscalar):
print(f"Warning ({line}): Danger returning complex from FORTRAN",
file=sys.stderr)
elif v.tinfo != VT.scalar:
print(f"Error ({line}): Can only return scalars from FORTRAN",
file=sys.stderr)
return 1
return 0
def _fortranize_args(f, line):
if not f.fort:
return 0
err = _fortranize_args_var(f.args, line)
if f.ret:
err += _fortranize_ret(f.ret[0], line)
return err
# ---------------------------------------------------------------------------
# Top-level typecheck
# ---------------------------------------------------------------------------
def typecheck(ctx, f, line):
"""Run full semantic analysis on a Func. Returns error count."""
label_args(f)
return (_typecheck_return(ctx, f.ret, line) +
_typecheck_args(ctx, f.args, line) +
_fortranize_args(f, line))
|