]> gitweb.ps.run Git - toc/blob - src/typeInfo.h
add comments, fix struct/function lookup
[toc] / src / typeInfo.h
1 #pragma once\r
2 \r
3 #include "repr.h"\r
4 \r
5 struct TypeInfo\r
6 {\r
7   Type type;\r
8   bool isStruct;\r
9 };\r
10 \r
11 #include "find.h"\r
12 \r
13 TypeInfo typeType(const Program & p, Type t)\r
14 {\r
15   TypeInfo result;\r
16   result.isStruct = true;\r
17   if (t.name == "int" || t.name == "float" || t.name == "double" ||\r
18       t.name == "char" || t.name == "long" || t.name == "short" || t.name == "bool" ||\r
19       t.name == "void")\r
20   {\r
21     result.isStruct = false;\r
22   }\r
23   result.type = t;\r
24   return result;\r
25 }\r
26 \r
27 TypeInfo typeExpr(const Program & p, std::shared_ptr<Context> globalCtx, Expr e)\r
28 {\r
29   TypeInfo result;\r
30 \r
31   switch (e.type)\r
32   {\r
33   case ExprType::Func:\r
34   {\r
35     auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
36     if (!f.has_value())\r
37       throw "Unknown function";\r
38     result = typeType(p, std::get<0>(*f).returnType);\r
39     break;\r
40   }\r
41   case ExprType::Method:\r
42   {\r
43     TypeInfo tiCaller = typeExpr(p, globalCtx, *e._method.expr);\r
44     if (!tiCaller.isStruct)\r
45       throw "Calling method on non-struct";\r
46     auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);\r
47     if (!s.has_value())\r
48       throw "Calling method on unknown struct";\r
49     auto m = findStructMethod(e._method.methodName, std::get<0>(*s));\r
50     if (!m.has_value())\r
51       throw "Unknown method";\r
52     result = typeType(p, m->t.returnType);\r
53     break;\r
54   }\r
55   case ExprType::Lit:\r
56     result.isStruct = false;\r
57     switch (e._lit.type)\r
58     {\r
59     case LitType::Bool:    result.type.name = "bool"; break;\r
60     case LitType::Int:     result.type.name = "int"; break;\r
61     case LitType::Decimal: result.type.name = "double"; break;\r
62     case LitType::String:  result.type.name = "char"; result.type.modifiers.push_back({ TypeModifierType::Pointer, false, -1 }); break;\r
63     }\r
64     break;\r
65   case ExprType::Paren:\r
66     result = typeExpr(p, globalCtx, *e._paren.expr);\r
67     break;\r
68   case ExprType::Dot:\r
69   {\r
70     auto tiCaller = typeExpr(p, globalCtx, *e._dot.expr);\r
71     if (!tiCaller.isStruct)\r
72       throw "Accessing member of non-struct";\r
73     auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx);\r
74     if (!s.has_value())\r
75       throw "Calling method on unknown struct";\r
76     auto sm = findStructMember(e._dot.identifier, std::get<0>(*s));\r
77     if (!sm.has_value())\r
78       throw "Unknown struct member";\r
79     result = typeType(p, sm->t.type);\r
80     break;\r
81   }\r
82   case ExprType::PrefixOp:\r
83     result = typeExpr(p, globalCtx, *e._prefixOp.expr);\r
84     break;\r
85   case ExprType::PostfixOp:\r
86     result = typeExpr(p, globalCtx, *e._postfixOp.expr);\r
87     break;\r
88   case ExprType::BinaryOp:\r
89     result = typeExpr(p, globalCtx, *e._binaryOp.lexpr);\r
90     break;\r
91   case ExprType::TernaryOp:\r
92     result = typeExpr(p, globalCtx, *e._ternaryOp.rexprTrue);\r
93     break;\r
94   case ExprType::Bracket:\r
95   {\r
96     TypeInfo ti = typeExpr(p, globalCtx, *e._brackets.lexpr);\r
97     if (!ti.type.modifiers.empty())\r
98     {\r
99       result = ti;\r
100       result.type.modifiers.pop_back();\r
101     }\r
102     else\r
103     {\r
104       throw "Indexing non-array";\r
105     }\r
106   }\r
107   case ExprType::Identifier:\r
108   {\r
109     auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
110     if (!v.has_value())\r
111       throw "Unknown variable";\r
112     result = typeType(p, std::get<0>(*v).type);\r
113     break;\r
114   }\r
115   }\r
116 \r
117   return result;\r
118 }