Skip to content

Commit

Permalink
Improve type pretty-printing. Slightly.
Browse files Browse the repository at this point in the history
The two-pass parser treats any unknown variable as a function, and if
those leak into expressions, it can lead to weird error messages.

Bug: issue #354
Test: manual test
  • Loading branch information
dvander committed Sep 8, 2019
1 parent 4003ec1 commit 21088f2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
12 changes: 4 additions & 8 deletions compiler/expressions.cpp
Expand Up @@ -288,14 +288,10 @@ type_to_name(int tag)
if (tag == pc_anytag)
return "any";

const char* name = pc_tagname(tag);
if (name)
return name;

Type* type = gTypes.find(tag);
if (type && type->isFunction())
return "function";
return "unknown";
if (!type)
return "unknown";
return type->prettyName();
}

int
Expand Down Expand Up @@ -1833,7 +1829,7 @@ field_expression(svalue& thisval, value* lval, symbol** target, methodmap_method

methodmap_t* map = gTypes.find(thisval.val.tag)->asMethodmap();
if (!map) {
error(104, pc_typename(thisval.val.tag));
error(104, type_to_name(thisval.val.tag));
return FER_Fail;
}

Expand Down
17 changes: 3 additions & 14 deletions compiler/parser.cpp
Expand Up @@ -583,17 +583,6 @@ inst_datetime_defines(void) {
insert_subst("__TIME__", 8, ltime);
}

const char*
pc_typename(int tag) {
if (tag == 0)
return "int";
if (tag == sc_rationaltag)
return "float";
if (tag == pc_tag_string)
return "char";
return pc_tagname(tag);
}

const char*
pc_tagname(int tag) {
if (Type* type = gTypes.find(tag))
Expand Down Expand Up @@ -1790,7 +1779,7 @@ declloc(int tokid) {
int explicit_dims = type->numdim;
if (parse_new_typename(NULL, &tag)) {
if (tag != type->semantic_tag())
error(164, pc_typename(tag), pc_typename(type->tag));
error(164, type_to_name(tag), type_to_name(type->tag));
if (gTypes.find(tag)->isEnumStruct()) {
assert(explicit_dims > 0);
explicit_dims--;
Expand Down Expand Up @@ -2887,7 +2876,7 @@ parse_old_array_dims(declinfo_t* decl, int flags) {
// Fixed array with dynamic size. Note that this protects this code
// from not supporting new-style enum structs (it is called before
// rewriting happens).
error(161, pc_typename(type->tag));
error(161, type_to_name(type->tag));
}
}

Expand Down Expand Up @@ -3744,7 +3733,7 @@ dodelete() {

methodmap_t* map = gTypes.find(sval.val.tag)->asMethodmap();
if (!map) {
error(115, "type", pc_tagname(sval.val.tag));
error(115, "type", type_to_name(sval.val.tag));
return;
}

Expand Down
1 change: 0 additions & 1 deletion compiler/sc.h
Expand Up @@ -468,7 +468,6 @@ int pc_compile(int argc, char** argv);
int pc_addconstant(const char* name, cell value, int tag);
int pc_addtag(const char* name);
int pc_findtag(const char* name);
const char* pc_typename(int tag);
const char* pc_tagname(int tag);
int parse_decl(declinfo_t* decl, int flags);
const char* type_to_name(int tag);
Expand Down
7 changes: 4 additions & 3 deletions compiler/sctracker.cpp
Expand Up @@ -391,9 +391,10 @@ deduce_layout_spec_by_tag(int tag)
if (type && type->isStruct())
return Layout_PawnStruct;

const char* name = pc_tagname(tag);
if (findglb(name))
return Layout_Enum;
if (Type* type = gTypes.find(tag)) {
if (findglb(type->name()))
return Layout_Enum;
}

return Layout_None;
}
Expand Down
10 changes: 10 additions & 0 deletions compiler/types.cpp
Expand Up @@ -71,6 +71,14 @@ Type::isDeclaredButNotDefined() const
return false;
}

const char*
Type::prettyName() const
{
if (kind_ == TypeKind::Function)
return kindName();
return name();
}

const char*
Type::kindName() const
{
Expand All @@ -89,6 +97,8 @@ Type::kindName() const
if (funcenum_ptr_) {
if (funcenum_ptr_->entries.length() > 1)
return "typeset";
if (name_.startsWith("::"))
return "function";
return "typedef";
}
return "function";
Expand Down
1 change: 1 addition & 0 deletions compiler/types.h
Expand Up @@ -92,6 +92,7 @@ class Type
return kind_;
}
const char* kindName() const;
const char* prettyName() const;
cell smx_export_value() const {
return value_ | int(kind_) | fixed_;
}
Expand Down
2 changes: 1 addition & 1 deletion third_party/amtl

0 comments on commit 21088f2

Please sign in to comment.