-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQImplBlock.cpp
More file actions
146 lines (122 loc) · 3.61 KB
/
QImplBlock.cpp
File metadata and controls
146 lines (122 loc) · 3.61 KB
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
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
using namespace QLang;
using namespace std;
void StructDefinition::ParseImplBlock( Lexer &l, Scope *s )
{
int sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_IMPL )
{
COMPILE_ERROR( l, "Internal Compiler Error" );
}
// Read the first identifier (could be protocol name or struct name)
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected identifier after 'impl'" );
}
string firstName = l.getSymbolText();
string structName;
string protocolName;
// Check if this is 'impl Protocol for Struct' or 'impl Struct'
sym = l.peekSymbol();
if ( sym == Lexer::KEYWORD_FOR )
{
// impl Protocol for Struct { ... }
l.getSymbol(); // consume 'for'
protocolName = firstName;
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected struct name after 'for'" );
}
structName = l.getSymbolText();
}
else
{
// impl Struct { ... }
structName = firstName;
}
// Look up the struct in scope
Symbol *structSym = s->findSymbol( structName );
if ( structSym == nullptr )
{
COMPILE_ERROR( l, "Unknown struct '" + structName + "' in impl block" );
}
StructDefinition *structDef = dynamic_cast<StructDefinition *>( structSym );
if ( structDef == nullptr )
{
COMPILE_ERROR( l, "'" + structName + "' is not a struct" );
}
// If a protocol was specified, verify it exists
if ( !protocolName.empty() )
{
Symbol *protoSym = s->findSymbol( protocolName );
if ( protoSym == nullptr )
{
COMPILE_ERROR( l, "Unknown protocol '" + protocolName + "' in impl block" );
}
ProtocolDefinition *protoDef = dynamic_cast<ProtocolDefinition *>( protoSym );
if ( protoDef == nullptr )
{
COMPILE_ERROR( l, "'" + protocolName + "' is not a protocol" );
}
}
// Expect '{'
sym = l.getSymbol();
if ( sym != '{' )
{
COMPILE_ERROR( l, "Expected '{' in impl block" );
}
// Use a local scope for impl methods so they don't pollute the global scope
// and don't conflict with protocol declarations of the same method name.
SmartPtr<Scope> implScope = new Scope( Scope::kScope_Class, structName );
implScope->setParent( s );
// Parse methods until '}'
while ( l.peekSymbol() != '}' )
{
if ( l.peekSymbol() != Lexer::KEYWORD_FN )
{
COMPILE_ERROR( l, "Expected 'fn' for method in impl block" );
}
SmartPtr<FunctionDefinition> method = FunctionDefinition::Parse( l, implScope );
structDef->addMethod( method );
}
sym = l.getSymbol();
assert( sym == '}' );
if ( !protocolName.empty() )
{
// Verify that the struct implements all required protocol methods
Symbol *protoSym = s->findSymbol( protocolName );
ProtocolDefinition *protoDef = dynamic_cast<ProtocolDefinition *>( protoSym );
// protoDef is non-null here — we already validated it above
const std::vector<SmartPtr<FunctionDefinition> > &required = protoDef->getRequiredMethods();
const std::vector<SmartPtr<FunctionDefinition> > &implemented = structDef->getMethods();
for ( const SmartPtr<FunctionDefinition> &req : required )
{
bool found = false;
for ( const SmartPtr<FunctionDefinition> &impl : implemented )
{
if ( impl->getName() == req->getName() )
{
found = true;
break;
}
}
if ( !found )
{
COMPILE_ERROR( l, "Struct '" + structName + "' does not implement method '" + req->getName() + "' required by protocol '" + protocolName + "'" );
}
}
cout << "Completed impl " << protocolName << " for " << structName << endl;
}
else
{
cout << "Completed impl " << structName << endl;
}
}