aboutsummaryrefslogtreecommitdiff
path: root/lib/Parser/ParserFunction.cc
blob: 2f80c4209269fe72466ab5f922b96f418fc87c29 (plain)
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

#include "cps/Diagnostics.h"
#include "cps/Parser.h"
#include "cps/FunctionAST.h"
#include "cps/Operator.h"

using namespace cps;

// Array-parameter bounds live in the signature, not the AST, so they must be
// integer literals (an optional leading '-' is allowed).
bool Parser::ParseSignedIntBound(int64_t &Out) {
    bool Negative = false;
    if (CurTok == '-') {
        Negative = true;
        getNextToken();
    }
    if (CurTok != tok_number_int) {
        cps::sourceError(Lex.getLine(), "Array parameter bounds must be integer literals");
        return false;
    }
    return ParseIntegerValue(Negative, Out);
}

// ( [BYREF|BYVAL] <name> : <type>
//                | <name> : ARRAY OF <type>                 -- rank 1, bounds from the argument
//                | <name> : ARRAY[<int>:<int>,...] OF <type> , ... )
// The pass mode is sticky across parameters (guide §8.3: "the BYVAL or BYREF
// keyword need not be repeated"); unannotated parameters default to BYVAL
// (§8.1). BYREF is procedure-only (§8.3), gated by AllowByRef.
std::optional<std::vector<ParamDecl>> Parser::ParsePrototypeArgs(bool AllowByRef) {
    std::vector<ParamDecl> Params;
    if (CurTok != '(') return Params;
    getNextToken();

    PassMode Current = PassMode::ByVal;
    while (CurTok != ')') {
        if (CurTok == tok_byref) {
            if (!AllowByRef) {
                cps::sourceError(Lex.getLine(), "BYREF parameters are not allowed in a FUNCTION (procedures only)");
                return std::nullopt;
            }
            Current = PassMode::ByRef;
            getNextToken();
        } else if (CurTok == tok_byval) {
            Current = PassMode::ByVal;
            getNextToken();
        }

        if (CurTok != tok_identifier) {
            cps::sourceError(Lex.getLine(), "Expected argument name");
            return std::nullopt;
        }
        ParamDecl P;
        P.Line = Lex.getLine();
        P.Name = Lex.IdentifierStr;
        P.Mode = Current;
        getNextToken();

        if (CurTok != tok_colon) {
            cps::sourceError(Lex.getLine(), "Expected ':' after argument name");
            return std::nullopt;
        }
        getNextToken();

        if (CurTok == tok_array) {
            getNextToken();
            P.IsArray = true;
            if (CurTok == '[') {
                getNextToken();
                while (true) {
                    int64_t Lo = 0, Hi = 0;
                    if (!ParseSignedIntBound(Lo)) return std::nullopt;
                    if (CurTok != tok_colon) {
                        cps::sourceError(Lex.getLine(), "Expected ':' between array parameter bounds");
                        return std::nullopt;
                    }
                    getNextToken();
                    if (!ParseSignedIntBound(Hi)) return std::nullopt;
                    P.DeclaredBounds.emplace_back(Lo, Hi);
                    if (CurTok == ']') break;
                    if (CurTok != ',') {
                        cps::sourceError(Lex.getLine(), "Expected ',' or ']' in array parameter bounds");
                        return std::nullopt;
                    }
                    getNextToken();
                }
                getNextToken(); // eat ']'
                P.Rank = static_cast<int>(P.DeclaredBounds.size());
            } else {
                P.Rank = 1; // ARRAY OF T: bounds travel with the argument
            }
            if (CurTok != tok_of) {
                cps::sourceError(Lex.getLine(), "Expected OF after ARRAY in parameter '%s'",
                        P.Name.c_str());
                return std::nullopt;
            }
            getNextToken();
        }

        P.TypeName = ParseTypeName(false);
        if (P.TypeName.empty()) {
            cps::sourceError(Lex.getLine(), "Expected argument type for '%s'", P.Name.c_str());
            return std::nullopt;
        }
        if (CurTok == tok_ellipsis) {
            if (P.IsArray || !isTemplatePack(P.TypeName)) {
                sourceError(P.Line, "A pack parameter must use the current template pack and cannot be ARRAY");
                return std::nullopt;
            }
            P.IsPack = true;
            getNextToken();
            if (CurTok != ')') {
                sourceError(P.Line, "A pack parameter must be last");
                return std::nullopt;
            }
        } else if (isTemplatePack(P.TypeName)) {
            sourceError(P.Line, "A template parameter pack requires '...'");
            return std::nullopt;
        }

        for (const ParamDecl &Prev : Params) {
            if (sameIdentifier(Prev.Name, P.Name)) {
                cps::sourceError(Lex.getLine(), "Duplicate parameter name '%s'",
                        P.Name.c_str());
                return std::nullopt;
            }
        }
        Params.push_back(std::move(P));

        if (CurTok == ')') break;
        if (CurTok != ',') {
            cps::sourceError(Lex.getLine(), "Expected ',' or ')'");
            return std::nullopt;
        }
        getNextToken();
        if (CurTok == ')') {
            cps::sourceError(Lex.getLine(), "Expected another parameter after ','");
            return std::nullopt;
        }
    }
    getNextToken();
    return Params;
}

std::string Parser::ParseOperatorName(int *OpOut) {
    getNextToken(); // OPERATOR
    int Op = CurTok;
    if (OpOut) *OpOut = Op;
    if (operatorSpelling(Op).empty()) {
        sourceError(Lex.getLine(), "Expected a supported operator after OPERATOR");
        getNextToken();
        return {};
    }
    getNextToken();
    if (CurTok != '(') {
        sourceError(Lex.getLine(), "Expected '(' after operator name");
        return {};
    }
    return operatorName(Op);
}

// A callee is an identifier or OPERATOR <op>; false means a diagnosed failure.
bool Parser::ParseCalleeName(std::string &Name) {
    if (CurTok == tok_operator) {
        Name = ParseOperatorName();
        return !Name.empty();
    }
    Name = Lex.IdentifierStr;
    getNextToken();
    return true;
}

std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerClass, bool IsPublic, bool IsOverload) {
    int Line = Lex.getLine();
    getNextToken();
    if (CurTok == tok_delete || (CurTok == tok_identifier && sameIdentifier(Lex.IdentifierStr, "DELETE"))) {
        cps::sourceError(Line, "destructor DELETE must be a PROCEDURE");
        return nullptr;
    }
    if (CurTok == tok_new || (CurTok == tok_identifier && sameIdentifier(Lex.IdentifierStr, "NEW"))) {
        cps::sourceError(Line, "constructor NEW must be a PROCEDURE");
        return nullptr;
    }
    if (CurTok != tok_identifier && CurTok != tok_operator) {
        cps::sourceError(Lex.getLine(), "Expected function name");
        return nullptr;
    }

    int Operator = 0;
    std::string Name;
    if (CurTok == tok_operator) {
        Name = ParseOperatorName(&Operator);
        if (Name.empty()) return nullptr;
        if (!IsOverload) {
            sourceError(Line, "An operator function must be declared OVERLOAD");
            return nullptr;
        }
    } else {
        Name = Lex.IdentifierStr;
        getNextToken();
    }

    auto Args = ParsePrototypeArgs(/*AllowByRef=*/false);
    if (!Args) return nullptr;

    std::string RetType = "INTEGER";
    bool IsDefaultReturn = false;
    if (CurTok == tok_returns) {
        getNextToken();
        if (CurTok == tok_default) {
            IsDefaultReturn = true;
            getNextToken();
        }
        if (CurTok == tok_array) {
            cps::sourceError(Lex.getLine(), "A FUNCTION cannot RETURN an array; use a BYREF PROCEDURE parameter instead");
            return nullptr;
        }
        RetType = ParseTypeName(true);
        if (RetType.empty()) {
            cps::sourceError(Lex.getLine(), "Expected return type for function '%s'", Name.c_str());
            return nullptr;
        }
    }

    auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), RetType, false, OwnerClass, IsPublic, IsOverload);
    Proto->setOperator(Operator);
    Proto->setDefaultReturn(IsDefaultReturn);

    auto Def = ParseFunctionBody(std::move(Proto), tok_endfunction, "expected ENDFUNCTION", Line);
    if (IsDefaultReturn && !CurrentTemplate.empty()) {
        sourceError(Line, "DEFAULT return types are not supported in templates");
        return nullptr;
    }
    return Def;
}

std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerClass, bool IsPublic, bool IsOverload) {
    int Line = Lex.getLine();
    getNextToken();
    if (CurTok != tok_identifier && CurTok != tok_new && CurTok != tok_delete && CurTok != tok_operator) {
        cps::sourceError(Lex.getLine(), "Expected procedure name");
        return nullptr;
    }

    int Operator = 0;
    bool IsOperator = CurTok == tok_operator;
    std::string OperatorName;
    if (IsOperator) {
        OperatorName = ParseOperatorName(&Operator);
        if (OperatorName.empty()) return nullptr;
        if (Operator != tok_assign || OwnerClass.empty() || !IsOverload) {
            sourceError(Line, "Assignment operator must be an OVERLOAD member PROCEDURE");
            return nullptr;
        }
    }
    std::string Name = IsOperator ? OperatorName : CurTok == tok_new ? "NEW" : CurTok == tok_delete ? "DELETE" : Lex.IdentifierStr;
    if (OwnerClass.empty() && sameIdentifier(Name, "DELETE")) {
        cps::sourceError(Line, "destructor DELETE is only allowed in a CLASS");
        return nullptr;
    }
    if (OwnerClass.empty() && sameIdentifier(Name, "NEW")) {
        cps::sourceError(Line, "constructor NEW is only allowed in a CLASS");
        return nullptr;
    }
    if (!IsOperator) getNextToken();

    auto Args = ParsePrototypeArgs(/*AllowByRef=*/true);
    if (!Args) return nullptr;
    if (sameIdentifier(Name, "DELETE") && !Args->empty()) {
        cps::sourceError(Line, "destructor DELETE must have no parameters");
        return nullptr;
    }
    auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID", false, OwnerClass, IsPublic, IsOverload);
    Proto->setOperator(Operator);

    return ParseFunctionBody(std::move(Proto), tok_endprocedure, "expected ENDPROCEDURE", Line);
}

// Shared FUNCTION/PROCEDURE tail: the body (with the prototype's pack
// parameters in scope) up to EndToken, which must be present.
std::unique_ptr<FunctionDefAST> Parser::ParseFunctionBody(
    std::unique_ptr<PrototypeAST> Proto, int EndToken, const char *EndMessage, int Line) {
    auto SavedPacks = std::move(PackNames);
    PackNames.clear();
    for (const auto &P : Proto->getParams()) if (P.IsPack) PackNames.insert(P.Name);
    std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({EndToken});
    PackNames = std::move(SavedPacks);

    if (CurTok != EndToken) {
        cps::sourceError(Lex.getLine(), "%s", EndMessage);
        return nullptr;
    }
    getNextToken();

    auto Def = std::make_unique<FunctionDefAST>(std::move(Proto), std::move(Body));
    Def->setLine(Line);
    return Def;
}

// EXTERN "C" EXTERN_FUNCTION <name>(<type>, ... [, ...]) RETURNS <type>
// EXTERN "C" EXTERN_PROCEDURE <name>(<type>, ... [, ...])
// Parameters are unnamed; a trailing ... makes the prototype variadic.
std::unique_ptr<StmtAST> Parser::ParseExternDecl() {
    int Line = Lex.getLine();
    getNextToken();
    if (CurTok != tok_string_literal || Lex.StringVal != "C") {
        cps::sourceError(Lex.getLine(), "EXTERN must be followed by \"C\"");
        return nullptr;
    }
    getNextToken();
    if (CurTok != tok_extern_function && CurTok != tok_extern_procedure) {
        cps::sourceError(Lex.getLine(), "Expected EXTERN_FUNCTION or EXTERN_PROCEDURE after EXTERN \"C\"");
        return nullptr;
    }
    bool IsFunction = CurTok == tok_extern_function;
    getNextToken();
    if (CurTok != tok_identifier) {
        cps::sourceError(Lex.getLine(), "Expected C function name");
        return nullptr;
    }
    std::string Name = Lex.IdentifierStr;
    getNextToken();

    std::vector<ParamDecl> Params;
    bool IsVariadic = false;
    if (CurTok != '(') {
        cps::sourceError(Lex.getLine(), "Expected '(' after C function name '%s'",
                Name.c_str());
        return nullptr;
    }
    getNextToken();
    while (CurTok != ')') {
        if (CurTok == tok_ellipsis) {
            IsVariadic = true;
            getNextToken();
            if (CurTok != ')') {
                cps::sourceError(Lex.getLine(), "'...' must be the last parameter");
                return nullptr;
            }
            break;
        }
        if (CurTok == tok_byref || CurTok == tok_byval) {
            cps::sourceError(Lex.getLine(), "EXTERN parameters cannot be BYREF or BYVAL; use a C pointer type");
            return nullptr;
        }
        ParamDecl P;
        P.Line = Lex.getLine();
        P.TypeName = ParseTypeName(false);
        if (P.TypeName.empty()) return nullptr;
        P.Name = "arg" + std::to_string(Params.size() + 1);
        Params.push_back(std::move(P));
        if (CurTok == ')') break;
        if (CurTok != ',') {
            cps::sourceError(Lex.getLine(), "Expected ',' or ')'");
            return nullptr;
        }
        getNextToken();
        if (CurTok == ')') {
            cps::sourceError(Lex.getLine(), "Expected another parameter after ','");
            return nullptr;
        }
    }
    getNextToken();

    std::string RetType = "VOID";
    if (CurTok == tok_returns) {
        if (!IsFunction) {
            cps::sourceError(Lex.getLine(), "EXTERN_PROCEDURE cannot have RETURNS");
            return nullptr;
        }
        getNextToken();
        RetType = ParseTypeName(false);
        if (RetType.empty()) return nullptr;
    } else if (IsFunction) {
        cps::sourceError(Line, "EXTERN_FUNCTION '%s' requires RETURNS",
                Name.c_str());
        return nullptr;
    }

    auto Proto = std::make_unique<PrototypeAST>(Name, std::move(Params), RetType, /*IsExternal=*/true);
    Proto->setVariadic(IsVariadic);
    auto Decl = std::make_unique<ExternDeclAST>(std::move(Proto));
    Decl->setLine(Line);
    return Decl;
}

std::unique_ptr<StmtAST> Parser::ParseCallStmt() {
    int Line = Lex.getLine();
    getNextToken();
    if (CurTok != tok_identifier && CurTok != tok_operator) {
        cps::sourceError(Lex.getLine(), "Expected callee name after CALL");
        return nullptr;
    }

    std::string Callee;
    if (!ParseCalleeName(Callee)) return nullptr;

    std::vector<std::string> TemplateArgs;
    if (CurTok == '<') {
        auto Parsed = ParseTemplateArgs();
        if (!Parsed) return nullptr;
        TemplateArgs = std::move(*Parsed);
        if (CurTok != '(') {
            sourceError(Line, "Expected '(' after explicit template arguments");
            return nullptr;
        }
    }

    std::vector<DesignatorAccess> Accesses;
    if (!ParseDesignatorSuffix(Accesses)) return nullptr;
    if (!Accesses.empty()) {
        auto Call = ParseMemberCall(Callee, std::move(Accesses), Line);
        if (!Call) return nullptr;
        return std::make_unique<MemberCallStmtAST>(std::move(Call));
    }

    std::vector<std::unique_ptr<ExprAST>> Args;
    if (CurTok == '(') {
        getNextToken();
        auto Parsed = ParseExprList(')', true);
        if (!Parsed) return nullptr;
        Args = std::move(*Parsed);
    }

    if (CurTok == '.') {
        auto Result = ParseResultMethods(std::make_unique<CallExprAST>(Callee, std::move(Args), Line, std::move(TemplateArgs)), Line);
        if (!Result) return nullptr;
        return std::make_unique<MemberCallStmtAST>(
            std::unique_ptr<MemberCallExprAST>(static_cast<MemberCallExprAST *>(Result.release())));
    }
    return std::make_unique<CallStmtAST>(Callee, std::move(Args), Line, std::move(TemplateArgs));
}

std::unique_ptr<StmtAST> Parser::ParseReturnStmt() {
    getNextToken();
    std::unique_ptr<ExprAST> Expr = nullptr;
    // A bare RETURN is recognized right before any block terminator.
    if (!atCaseBoundary() && CurTok != tok_endcase && CurTok != tok_otherwise &&
        CurTok != tok_endif && CurTok != tok_else && CurTok != tok_endfunction &&
        CurTok != tok_endprocedure && CurTok != tok_endwhile && CurTok != tok_until &&
        CurTok != tok_next && CurTok != tok_catch && CurTok != tok_endtry &&
        CurTok != tok_eof) {
        Expr = ParseExpression();
        if (!Expr) return nullptr;
    }
    return std::make_unique<ReturnStmtAST>(std::move(Expr));
}