goscript-types 0.1.0

goscript types
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#![allow(dead_code)]
use super::constant;
use super::lookup::missing_method;
use super::objects::{TCObjects, TypeKey};
use super::typ;
use super::typ::{fmt_type, BasicType, Type};
use super::universe::{Builtin, Universe};
use goscript_parser::ast;
use goscript_parser::ast::*;
use goscript_parser::objects::{FuncTypeKey, IdentKey, Objects as AstObjects};
use goscript_parser::position;
use goscript_parser::token::Token;
use goscript_parser::visitor::{walk_expr, ExprVisitor};
use std::fmt;
use std::fmt::Display;
use std::fmt::Write;

/// An OperandMode specifies the (addressing) mode of an operand.
#[derive(Clone, Debug, PartialEq)]
pub enum OperandMode {
    Invalid,                   // operand is invalid
    NoValue,                   // operand represents no value (result of a function call w/o result)
    Builtin(Builtin),          // operand is a built-in function
    TypeExpr,                  // operand is a type
    Constant(constant::Value), // operand is a constant; the operand's typ is a Basic type
    Variable,                  // operand is an addressable variable
    MapIndex, // operand is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment)
    Value,    // operand is a computed value
    CommaOk,  // like value, but operand may be used in a comma,ok expression
}

impl OperandMode {
    pub fn constant_val(&self) -> Option<&constant::Value> {
        match self {
            OperandMode::Constant(v) => Some(v),
            _ => None,
        }
    }

    pub fn builtin_id(&self) -> Option<Builtin> {
        match self {
            OperandMode::Builtin(id) => Some(*id),
            _ => None,
        }
    }
}

impl fmt::Display for OperandMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match self {
            OperandMode::Invalid => "invalid operand",
            OperandMode::NoValue => "no value",
            OperandMode::Builtin(_) => "built-in",
            OperandMode::TypeExpr => "type",
            OperandMode::Constant(_) => "constant",
            OperandMode::Variable => "variable",
            OperandMode::MapIndex => "map index expression",
            OperandMode::Value => "value",
            OperandMode::CommaOk => "comma, ok expression",
        })
    }
}

/// An Operand represents an intermediate value during type checking.
/// Operands have an (addressing) mode, the expression evaluating to
/// the operand, the operand's type, a value for constants, and an id
/// for built-in functions.
#[derive(Clone, Debug)]
pub struct Operand {
    pub mode: OperandMode,
    pub expr: Option<ast::Expr>,
    pub typ: Option<TypeKey>,
}

impl Operand {
    pub fn new() -> Operand {
        Operand::with(OperandMode::Invalid, None, None)
    }

    pub fn with(mode: OperandMode, expr: Option<ast::Expr>, typ: Option<TypeKey>) -> Operand {
        Operand {
            mode: mode,
            expr: expr,
            typ: typ,
        }
    }

    pub fn invalid(&self) -> bool {
        self.mode == OperandMode::Invalid
    }

    pub fn pos(&self, ast_objs: &AstObjects) -> position::Pos {
        if let Some(e) = &self.expr {
            e.pos(ast_objs)
        } else {
            0
        }
    }

    pub fn set_const(&mut self, t: &Token, u: &Universe) {
        let bt = match t {
            Token::INT(_) => BasicType::UntypedInt,
            Token::FLOAT(_) => BasicType::UntypedFloat,
            Token::IMAG(_) => BasicType::UntypedComplex,
            Token::CHAR(_) => BasicType::UntypedRune,
            Token::STRING(_) => BasicType::UntypedString,
            _ => unreachable!(),
        };
        self.mode = OperandMode::Constant(constant::Value::with_literal(t));
        self.typ = Some(u.types()[&bt]);
    }

    pub fn is_nil(&self, u: &Universe) -> bool {
        match self.mode {
            OperandMode::Value => self.typ == Some(u.types()[&BasicType::UntypedNil]),
            _ => false,
        }
    }

    /// assignable_to returns whether self is assignable to a variable of type 't'.
    /// If the result is false and a non-None reason is provided, it may be set
    /// to a more detailed explanation of the failure.
    pub fn assignable_to(&self, t: TypeKey, reason: Option<&mut String>, objs: &TCObjects) -> bool {
        let u = objs.universe();
        if self.invalid() || t == u.types()[&BasicType::Invalid] {
            return true; // avoid spurious errors
        }

        if typ::identical(self.typ.unwrap(), t, objs) {
            return true;
        }

        let (k_left, k_right) = (t, self.typ.unwrap());
        let t_left = &objs.types[k_left];
        let t_right = &objs.types[k_right];
        let ut_key_left = typ::underlying_type(k_left, objs);
        let ut_key_right = typ::underlying_type(k_right, objs);
        let ut_left = &objs.types[ut_key_left];
        let ut_right = &objs.types[ut_key_right];

        if ut_right.is_untyped(objs) {
            match ut_left {
                Type::Basic(detail) => {
                    if self.is_nil(u) && detail.typ() == BasicType::UnsafePointer {
                        return true;
                    }
                    if let OperandMode::Constant(val) = &self.mode {
                        return val.representable(detail, None);
                    }
                    // The result of a comparison is an untyped boolean,
                    // but may not be a constant.
                    if detail.typ() == BasicType::Bool {
                        return ut_right.try_as_basic().unwrap().typ() == BasicType::UntypedBool;
                    }
                }
                Type::Interface(detail) => return self.is_nil(u) || detail.is_empty(),
                Type::Pointer(_)
                | Type::Signature(_)
                | Type::Slice(_)
                | Type::Map(_)
                | Type::Chan(_) => return self.is_nil(u),
                _ => {}
            }
        }

        // 'right' is typed
        // self's type 'right' and 'left' have identical underlying types
        // and at least one of 'right' or 'left' is not a named type
        if typ::identical(ut_key_right, ut_key_left, objs)
            && (!t_right.is_named() || !t_left.is_named())
        {
            return true;
        }

        // 'left' is an interface and 'right' implements 'left'
        if let Some(_) = ut_left.try_as_interface() {
            if let Some((m, wrong_type)) = missing_method(k_right, ut_key_left, true, objs) {
                if let Some(re) = reason {
                    let msg = if wrong_type {
                        "wrong type for method"
                    } else {
                        "missing method"
                    };
                    *re = format!("{} {}", msg, objs.lobjs[m].name());
                }
                return false;
            }
            return true;
        }

        // 'right' is a bidirectional channel value, 'left' is a channel
        // type, they have identical element types,
        // and at least one of 'right' or 'left' is not a named type
        if let Some(cr) = ut_right.try_as_chan() {
            if cr.dir() == typ::ChanDir::SendRecv {
                if let Some(cl) = ut_left.try_as_chan() {
                    if typ::identical(cr.elem(), cl.elem(), objs) {
                        return !t_right.is_named() || !t_left.is_named();
                    }
                }
            }
        }

        false
    }

    /// Operand string formats
    /// (not all "untyped" cases can appear due to the type system)
    ///
    /// mode       format
    ///
    /// invalid    <expr> (               <mode>                    )
    /// novalue    <expr> (               <mode>                    )
    /// builtin    <expr> (               <mode>                    )
    /// typexpr    <expr> (               <mode>                    )
    ///
    /// constant   <expr> (<untyped kind> <mode>                    )
    /// constant   <expr> (               <mode>       of type <typ>)
    /// constant   <expr> (<untyped kind> <mode> <val>              )
    /// constant   <expr> (               <mode> <val> of type <typ>)
    ///
    /// variable   <expr> (<untyped kind> <mode>                    )
    /// variable   <expr> (               <mode>       of type <typ>)
    ///
    /// mapindex   <expr> (<untyped kind> <mode>                    )
    /// mapindex   <expr> (               <mode>       of type <typ>)
    ///
    /// value      <expr> (<untyped kind> <mode>                    )
    /// value      <expr> (               <mode>       of type <typ>)
    ///
    /// commaok    <expr> (<untyped kind> <mode>                    )
    /// commaok    <expr> (               <mode>       of type <typ>)
    pub fn fmt(
        &self,
        f: &mut fmt::Formatter<'_>,
        tc_objs: &TCObjects,
        ast_objs: &AstObjects,
    ) -> fmt::Result {
        let universe = tc_objs.universe();
        let mut has_expr = true;

        // <expr> (
        if let Some(expr) = &self.expr {
            fmt_expr(&expr, f, ast_objs)?;
        } else {
            match &self.mode {
                OperandMode::Builtin(bi) => {
                    f.write_str(universe.builtins()[bi].name)?;
                }
                OperandMode::TypeExpr => {
                    fmt_type(self.typ, f, tc_objs)?;
                }
                OperandMode::Constant(val) => {
                    f.write_str(&val.to_string())?;
                }
                _ => has_expr = false,
            }
        }
        if has_expr {
            f.write_str(" (")?;
        }

        // <untyped kind>
        let has_type = match self.mode {
            OperandMode::Invalid
            | OperandMode::NoValue
            | OperandMode::Builtin(_)
            | OperandMode::TypeExpr => false,
            _ => {
                let tval = &tc_objs.types[self.typ.unwrap()];
                if tval.is_untyped(tc_objs) {
                    f.write_str(tval.try_as_basic().unwrap().name())?;
                    f.write_char(' ')?;
                    false
                } else {
                    true
                }
            }
        };

        // <mode>
        self.mode.fmt(f)?;

        // <val>
        if let OperandMode::Constant(val) = &self.mode {
            if self.expr.is_some() {
                f.write_char(' ')?;
                f.write_str(&val.to_string())?;
            }
        }

        // <typ>
        if has_type {
            if self.typ != Some(universe.types()[&BasicType::Invalid]) {
                f.write_str(" of type ")?;
                fmt_type(self.typ, f, tc_objs)?;
            } else {
                f.write_str(" with invalid type")?;
            }
        }

        // )
        if has_expr {
            f.write_char(')')?;
        }
        Ok(())
    }
}

/// fmt_expr formats the (possibly shortened) string representation for 'expr'.
/// Shortened representations are suitable for user interfaces but may not
/// necessarily follow Go syntax.
pub fn fmt_expr(expr: &Expr, f: &mut fmt::Formatter<'_>, ast_objs: &AstObjects) -> fmt::Result {
    // The AST preserves source-level parentheses so there is
    // no need to introduce them here to correct for different
    // operator precedences. (This assumes that the AST was
    // generated by a Go parser.)
    ExprFormater {
        f: f,
        ast_objs: ast_objs,
    }
    .visit_expr(expr)
}

struct ExprFormater<'a, 'b> {
    f: &'a mut fmt::Formatter<'b>,
    ast_objs: &'a AstObjects,
}

impl<'a, 'b> ExprVisitor for ExprFormater<'a, 'b> {
    type Result = fmt::Result;

    fn visit_expr(&mut self, expr: &Expr) -> Self::Result {
        walk_expr(self, expr)
    }

    fn visit_expr_ident(&mut self, _: &Expr, ident: &IdentKey) -> Self::Result {
        self.fmt_ident(ident)
    }

    fn visit_expr_ellipsis(&mut self, _: &Expr, els: &Option<Expr>) -> Self::Result {
        self.f.write_str("...")?;
        if let Some(e) = els {
            self.visit_expr(e)?;
        }
        Ok(())
    }

    fn visit_expr_basic_lit(&mut self, _: &Expr, blit: &BasicLit) -> Self::Result {
        blit.token.fmt(self.f)
    }

    fn visit_expr_func_lit(&mut self, this: &Expr, flit: &FuncLit) -> Self::Result {
        self.f.write_char('(')?;
        self.visit_expr_func_type(this, &flit.typ)?;
        self.f.write_str(" literal)")
    }

    fn visit_expr_composit_lit(&mut self, _: &Expr, clit: &CompositeLit) -> Self::Result {
        self.f.write_char('(')?;
        match &clit.typ {
            Some(t) => {
                self.visit_expr(t)?;
            }
            None => {
                self.f.write_str("(bad expr)")?;
            }
        }
        self.f.write_str(" literal)")
    }

    fn visit_expr_paren(&mut self, _: &Expr, expr: &Expr) -> Self::Result {
        self.f.write_char('(')?;
        self.visit_expr(expr)?;
        self.f.write_char(')')
    }

    fn visit_expr_selector(&mut self, this: &Expr, expr: &Expr, ident: &IdentKey) -> Self::Result {
        self.visit_expr(expr)?;
        self.f.write_char('.')?;
        self.visit_expr_ident(this, ident)
    }

    fn visit_expr_index(&mut self, _: &Expr, expr: &Expr, index: &Expr) -> Self::Result {
        self.visit_expr(expr)?;
        self.f.write_char('[')?;
        self.visit_expr(index)?;
        self.f.write_char(']')
    }

    fn visit_expr_slice(
        &mut self,
        _: &Expr,
        expr: &Expr,
        low: &Option<Expr>,
        high: &Option<Expr>,
        max: &Option<Expr>,
    ) -> Self::Result {
        self.visit_expr(expr)?;
        self.f.write_char('[')?;
        if let Some(l) = low {
            self.visit_expr(l)?;
        }
        self.f.write_char(':')?;
        if let Some(h) = high {
            self.visit_expr(h)?;
        }
        if let Some(m) = max {
            self.f.write_char(':')?;
            self.visit_expr(m)?;
        }
        self.f.write_char(']')
    }

    fn visit_expr_type_assert(
        &mut self,
        _: &Expr,
        expr: &Expr,
        typ: &Option<Expr>,
    ) -> Self::Result {
        self.visit_expr(expr)?;
        self.f.write_str(".(")?;
        match &typ {
            Some(t) => {
                self.visit_expr(t)?;
            }
            None => {
                self.f.write_str("(bad expr)")?;
            }
        }
        self.f.write_char(')')
    }

    fn visit_expr_call(
        &mut self,
        _: &Expr,
        func: &Expr,
        args: &Vec<Expr>,
        ellipsis: bool,
    ) -> Self::Result {
        self.visit_expr(func)?;
        self.f.write_char('(')?;
        for (i, arg) in args.iter().enumerate() {
            if i > 0 {
                self.f.write_str(", ")?;
            }
            self.visit_expr(arg)?;
        }
        if ellipsis {
            self.f.write_str("...")?;
        }
        self.f.write_char(')')
    }

    fn visit_expr_star(&mut self, _: &Expr, expr: &Expr) -> Self::Result {
        self.f.write_char('*')?;
        self.visit_expr(expr)
    }

    fn visit_expr_unary(&mut self, _: &Expr, expr: &Expr, op: &Token) -> Self::Result {
        op.fmt(self.f)?;
        self.visit_expr(expr)
    }

    fn visit_expr_binary(
        &mut self,
        _: &Expr,
        left: &Expr,
        op: &Token,
        right: &Expr,
    ) -> Self::Result {
        self.visit_expr(left)?;
        self.f.write_fmt(format_args!(" {} ", op))?;
        self.visit_expr(right)
    }

    fn visit_expr_key_value(&mut self, _: &Expr, _: &Expr, _: &Expr) -> Self::Result {
        self.f.write_str("(bad expr)")
    }

    fn visit_expr_array_type(&mut self, _: &Expr, len: &Option<Expr>, elm: &Expr) -> Self::Result {
        self.f.write_char('[')?;
        if let Some(l) = len {
            self.visit_expr(l)?;
        }
        self.f.write_char(']')?;
        self.visit_expr(elm)
    }

    fn visit_expr_struct_type(&mut self, _: &Expr, s: &StructType) -> Self::Result {
        self.f.write_str("struct{")?;
        self.fmt_fields(&s.fields, "; ", false)?;
        self.f.write_char('}')
    }

    fn visit_expr_func_type(&mut self, _: &Expr, s: &FuncTypeKey) -> Self::Result {
        self.f.write_str("func")?;
        self.fmt_sig(&self.ast_objs.ftypes[*s])
    }

    fn visit_expr_interface_type(&mut self, _: &Expr, s: &InterfaceType) -> Self::Result {
        self.f.write_str("interface{")?;
        self.fmt_fields(&s.methods, "; ", true)?;
        self.f.write_char('}')
    }

    fn visit_map_type(&mut self, _: &Expr, key: &Expr, val: &Expr, _: &Expr) -> Self::Result {
        self.f.write_str("map[")?;
        self.visit_expr(key)?;
        self.f.write_char(']')?;
        self.visit_expr(val)
    }

    fn visit_chan_type(&mut self, _: &Expr, chan: &Expr, dir: &ChanDir) -> Self::Result {
        let s = match dir {
            ChanDir::Send => "chan<- ",
            ChanDir::Recv => "<-chan ",
            ChanDir::SendRecv => "chan ",
        };
        self.f.write_str(s)?;
        self.visit_expr(chan)
    }

    fn visit_bad_expr(&mut self, _: &Expr, _: &BadExpr) -> Self::Result {
        self.f.write_str("(bad expr)")
    }
}

impl<'a, 'b> ExprFormater<'a, 'b> {
    fn fmt_sig(&mut self, sig: &FuncType) -> fmt::Result {
        self.f.write_char('(')?;
        self.fmt_fields(&sig.params, ", ", false)?;
        self.f.write_char(')')?;
        if let Some(re) = &sig.results {
            self.f.write_char(' ')?;
            if re.list.len() == 1 {
                let field = &self.ast_objs.fields[re.list[0]];
                if field.names.len() == 0 {
                    self.visit_expr(&field.typ)?;
                }
            } else {
                self.f.write_char('(')?;
                self.fmt_fields(&re, ", ", false)?;
                self.f.write_char(')')?;
            }
        }
        Ok(())
    }

    fn fmt_fields(&mut self, fields: &FieldList, sep: &str, iface: bool) -> fmt::Result {
        for (i, fkey) in fields.list.iter().enumerate() {
            if i > 0 {
                self.f.write_str(sep)?;
            }
            let field = &self.ast_objs.fields[*fkey];
            for (i, name) in field.names.iter().enumerate() {
                if i > 0 {
                    self.f.write_str(", ")?;
                    self.fmt_ident(name)?;
                }
            }
            // types of interface methods consist of signatures only
            if iface {
                match &field.typ {
                    Expr::Func(sig) => {
                        self.fmt_sig(&self.ast_objs.ftypes[*sig])?;
                    }
                    _ => {}
                }
            } else {
                // named fields are separated with a blank from the field type
                if field.names.len() > 0 {
                    self.f.write_char(' ')?;
                }
                self.visit_expr(&field.typ)?;
            }
        }
        Ok(())
    }

    fn fmt_ident(&mut self, ident: &IdentKey) -> fmt::Result {
        self.f.write_str(&self.ast_objs.idents[*ident].name)
    }
}