void Tree::generateDeferClass() { ClassDefinition::Properties properties; startGeneratedClass(CommonNames::deferTypeName, properties); auto deferClass = getCurrentClass(); // addClosure(fun () closure) auto addClosureMethod = MethodDefinition::create(CommonNames::addClosureMethodName, nullptr, false, deferClass); auto closureType = Type::create(Type::Function); closureType->setFunctionSignature(FunctionSignature::create(nullptr)); auto closureInterfaceType = Type::create(closureType->getClosureInterfaceName()); addClosureMethod->addArgument(closureInterfaceType, "closure"); addClassMember(addClosureMethod); deferClass->generateDefaultConstructor(); finishClass(); }
LEXEME *expression_lambda(LEXEME *lex, SYMBOL *funcsp, TYPE *atp, TYPE **tp, EXPRESSION **exp, int flags) { LAMBDA *self; SYMBOL *vpl, *ths; HASHREC *hrl; TYPE *ltp; STRUCTSYM ssl; if (funcsp) funcsp->noinline = TRUE; IncGlobalFlag(); self = Alloc(sizeof(LAMBDA)); ltp = Alloc(sizeof(TYPE)); ltp->type = bt_struct; ltp->syms = CreateHashTable(1); ltp->tags = CreateHashTable(1); ltp->size = 0; self->captured = CreateHashTable(1); self->oldSyms = localNameSpace->syms; self->oldTags = localNameSpace->tags; self->index = lambdaIndex; self->captureMode = cmNone; self->isMutable = FALSE; self->captureThis = FALSE; self->cls = makeID(sc_type, ltp, NULL, LambdaName()); ltp->sp = self->cls; SetLinkerNames(self->cls, lk_cdecl); self->cls->islambda = TRUE; self->cls->structAlign = getAlign(sc_global, &stdpointer); self->func = makeID(sc_member, ltp, NULL, "$internalFunc"); self->func->parentClass = self->cls; self->functp = &stdauto; self->enclosingFunc = theCurrentFunc; if (lambdas) lambdas->prev = self; self->next = lambdas; lambdas = self; localNameSpace->syms = CreateHashTable(1); localNameSpace->tags = CreateHashTable(1); if (lambdas->next) { self->lthis = lambdas->next->lthis; } else if (funcsp && funcsp->parentClass) { self->lthis = ((SYMBOL *)funcsp->tp->syms->table[0]->p); } lex = getsym(); // past [ if (funcsp) { // can have a capture list; if (MATCHKW(lex, assign)) { lex = getsym(); if (MATCHKW(lex, comma) || MATCHKW(lex, closebr)) { self->captureMode = cmValue; skip(&lex, comma); } else { lex = backupsym(); } } else if (MATCHKW(lex, and)) { lex = getsym(); if (MATCHKW(lex, comma) || MATCHKW(lex, closebr)) { self->captureMode = cmRef; skip(&lex, comma); } else { lex = backupsym(); } } if (!MATCHKW(lex, comma) && !MATCHKW(lex, closebr)) { do { enum e_cm localMode = self->captureMode; if (MATCHKW(lex, comma)) skip(&lex, comma); if (MATCHKW(lex, kw_this)) { lex = getsym(); if (localMode == cmValue || !self->lthis) { error(ERR_CANNOT_CAPTURE_THIS); } else { if (self->captureThis) { error(ERR_CAPTURE_ITEM_LISTED_MULTIPLE_TIMES); } self->captureThis = TRUE; lambda_capture(NULL, cmThis, TRUE); } continue; } else if (MATCHKW(lex, and)) { if (localMode == cmRef) { error(ERR_INVALID_LAMBDA_CAPTURE_MODE); } localMode = cmRef; lex = getsym(); } else { if (localMode == cmValue) { error(ERR_INVALID_LAMBDA_CAPTURE_MODE); } localMode = cmValue; } if (ISID(lex)) { SYMBOL *sp = search(lex->value.s.a, localNameSpace->syms); LAMBDA *current = lambdas; while (current && !sp) { sp = search(lex->value.s.a, current->oldSyms); current = current->next; } lex = getsym(); if (sp) { if (sp->packed) { if (!MATCHKW(lex, ellipse)) error(ERR_PACK_SPECIFIER_REQUIRED_HERE); else lex = getsym(); } if (sp->packed) { int n; TEMPLATEPARAMLIST * templateParam = sp->tp->templateParam->p->byPack.pack; HASHREC *hr; for (n=0; templateParam; templateParam = templateParam->next, n++); hr = funcsp->tp->syms->table[0]; while (hr && ((SYMBOL *)hr->p) != sp) hr = hr->next; while (hr && n) { lambda_capture((SYMBOL *)hr->p, localMode, TRUE); hr = hr->next; n--; } } else { lambda_capture(sp, localMode, TRUE); } } else errorstr(ERR_UNDEFINED_IDENTIFIER, lex->value.s.a); } else { error(ERR_INVALID_LAMBDA_CAPTURE_MODE); } } while (MATCHKW(lex, comma)); } needkw(&lex, closebr); } else { if (!MATCHKW(lex, closebr)) { error(ERR_LAMBDA_CANNOT_CAPTURE); } else { lex = getsym(); } } if (MATCHKW(lex, openpa)) { TYPE *tpx = &stdvoid; HASHREC *hr; lex = getFunctionParams(lex, NULL, &self->func, &tpx, FALSE, sc_auto); self->funcargs = self->func->tp->syms->table[0]; hr = self->func->tp->syms->table[0]; while (hr) { SYMBOL *sym = (SYMBOL *)hr->p; if (sym->init) { error(ERR_CANNOT_DEFAULT_PARAMETERS_WITH_LAMBDA); } hr = hr->next; } if (MATCHKW(lex, kw_mutable)) { HASHREC *hr = self->captured->table[0]; while (hr) { LAMBDASP *lsp = (LAMBDASP *)hr->p; if (lsp->sym->lambdaMode == cmValue) { lsp->sym->tp = basetype(lsp->sym->tp); } hr = hr->next; } self->isMutable = TRUE; lex = getsym(); } ParseAttributeSpecifiers(&lex, funcsp, TRUE); if (MATCHKW(lex, pointsto)) { lex = getsym(); lex = get_type_id(lex, &self->functp, funcsp, sc_cast, FALSE, TRUE); } } else { TYPE *tp1 = Alloc(sizeof(TYPE)); SYMBOL *spi; tp1->type = bt_func; tp1->size = getSize(bt_pointer); tp1->btp = &stdvoid; tp1->sp = self->func; self->func->tp = tp1; spi = makeID(sc_parameter, tp1, NULL, AnonymousName()); spi->anonymous = TRUE; spi->tp = Alloc(sizeof(TYPE)); spi->tp->type = bt_void; insert(spi, localNameSpace->syms); SetLinkerNames(spi, lk_cpp); self->func->tp->syms = localNameSpace->syms; self->funcargs = self->func->tp->syms->table[0]; self->func->tp->syms->table[0] = NULL; } vpl = makeID(sc_parameter, &stdpointer, NULL, AnonymousName()); vpl->assigned = vpl->used = TRUE; SetLinkerNames(vpl, lk_cdecl); hrl = Alloc(sizeof(HASHREC)); hrl->p = (struct _hrintern_ *)vpl; hrl->next = lambdas->func->tp->syms->table[0]; lambdas->func->tp->syms->table[0] = hrl; vpl = makeID(sc_parameter, &stdpointer, NULL, AnonymousName()); vpl->assigned = vpl->used = TRUE; SetLinkerNames(vpl, lk_cdecl); hrl = Alloc(sizeof(HASHREC)); hrl->p = (struct _hrintern_ *)vpl; hrl->next = lambdas->func->tp->syms->table[0]; lambdas->func->tp->syms->table[0] = hrl; SetLinkerNames(lambdas->func, lk_cdecl); injectThisPtr(lambdas->func, basetype(lambdas->func->tp)->syms); lambdas->func->tp->btp = self->functp; lambdas->func->linkage = lk_virtual; lambdas->func->isInline = TRUE; ssl.str = self->cls; ssl.tmpl = NULL; addStructureDeclaration(&ssl); ths = makeID(sc_member, &stdpointer, NULL, "$this"); lambda_insert(ths, lambdas); if (MATCHKW(lex, begin)) { lex = body(lex, self->func); } else { error(ERR_LAMBDA_FUNCTION_BODY_EXPECTED); } dropStructureDeclaration(); localNameSpace->syms = self->oldSyms; localNameSpace->tags = self->oldTags; inferType(); finishClass(); *exp = createLambda(0); *tp = lambdas->cls->tp; lambdas = lambdas->next; if (lambdas) lambdas->prev = NULL; DecGlobalFlag(); return lex; }
void Tree::finishFunction(MethodDefinition* function) { function->setIsFunction(true); addClassMember(function); globalNameBindings.overloadMethod(function->getName(), function); finishClass(); }
void Tree::generateArrayClass() { ClassDefinition::Properties properties; startGeneratedClass(BuiltInTypes::arrayTypeName, properties); ClassDefinition* arrayClass = getCurrentClass(); // Add method: // int length() auto lengthMethod = MethodDefinition::create(BuiltInTypes::arrayLengthMethodName, Type::create(Type::Integer), false, arrayClass); addClassMember(lengthMethod); // Add method: // int size() auto sizeMethod = MethodDefinition::create(BuiltInTypes::arraySizeMethodName, Type::create(Type::Integer), false, arrayClass); addClassMember(sizeMethod); // Add method: // int capacity() auto capacityMethod = MethodDefinition::create(BuiltInTypes::arrayCapacityMethodName, Type::create(Type::Integer), false, arrayClass); addClassMember(capacityMethod); // The methods: // // append(_ element) // appendAll(_[] array) // _[] concat(_[] array) // _[] slice(int begin, int end) // // are used like this: // // array.append(1) // array.appendAll(array2) // array3 = array.concat(array2) // array4 = array.slice(1, 3) // // Which is equal to: // // array += 1 // array += array2 // array3 = array + array2 // array4 = array[1...3] // // Add method: // append(_ element) auto appendMethod = MethodDefinition::create(BuiltInTypes::arrayAppendMethodName, nullptr, false, arrayClass); appendMethod->addArgument(Type::Placeholder, "element"); addClassMember(appendMethod); // Add method: // appendAll(_[] array) auto appendAllMethod = MethodDefinition::create(BuiltInTypes::arrayAppendAllMethodName, nullptr, false, arrayClass); auto arrayType = Type::create(Type::Placeholder); arrayType->setArray(true); appendAllMethod->addArgument(arrayType, "array"); addClassMember(appendAllMethod); // Add method: // _[] concat(_[] array) auto concatReturnType = Type::create(Type::Placeholder); concatReturnType->setArray(true); auto concatMethod = MethodDefinition::create(BuiltInTypes::arrayConcatMethodName, concatReturnType, false, arrayClass); auto concatArrayType = Type::create(Type::Placeholder); concatArrayType->setArray(true); concatMethod->addArgument(concatArrayType, "array"); addClassMember(concatMethod); // Add method: // _[] slice(int begin, int end) auto sliceReturnType = Type::create(Type::Placeholder); sliceReturnType->setArray(true); auto sliceMethod = MethodDefinition::create(BuiltInTypes::arraySliceMethodName, sliceReturnType, false, arrayClass); sliceMethod->addArgument(Type::Integer, "begin"); sliceMethod->addArgument(Type::Integer, "end"); addClassMember(sliceMethod); // Add method: // each() (_) auto eachMethod = MethodDefinition::create(BuiltInTypes::arrayEachMethodName, Type::create(Type::Void), false, arrayClass); auto eachLambdaSignature = FunctionSignature::create(Type::create(Type::Void)); eachLambdaSignature->addArgument(Type::create(Type::Integer)); eachMethod->setLambdaSignature(eachLambdaSignature, arrayClass->getLocation()); addClassMember(eachMethod); finishClass(); }
ClassDefinition* Tree::insertBuiltInType(const Identifier& name) { ClassDefinition::Properties properties; startGeneratedClass(name, properties); return finishClass(); }