Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void analyze(ClassDeclaration d, Class c) {
- // what are d and c exactly? and what's the difference? when (and why) is this function being called?
- import std.stdio;
- auto oldManglePrefix = manglePrefix;
- auto oldScope = currentScope;
- auto oldThisType = thisType;
- auto oldFieldIndex = fieldIndex;
- auto oldMethodIndex = methodIndex;
- scope(exit) {
- manglePrefix = oldManglePrefix;
- currentScope = oldScope;
- thisType = oldThisType;
- fieldIndex = oldFieldIndex;
- methodIndex = oldMethodIndex;
- }
- thisType = Type.get(c).getParamType(false, true); // get the type of the class??
- // Update mangle prefix.
- auto name = c.name.toString(context); // name of class
- writeln("sid1: ", name);
- manglePrefix = manglePrefix ~ to!string(name.length) ~ name; // some mangle prefix stuff :P
- c.mangle = "C" ~ manglePrefix;
- writeln("sid2: ", c.mangle);
- auto dscope = currentScope = c.dscope = c.hasContext // getting the current scope of the class?
- ? new VoldemortScope(c, oldScope)
- : new AggregateScope(c, oldScope);
- Field[] baseFields; // list of fields inherited from the base class(es)
- Method[] baseMethods; // list of methods inherited from the base class(es)
- // i guess here we're visiting each of the base classes and converting them into some sort of structure to be used later?
- foreach(i; d.bases) {
- writeln("sid5: ", i.name.toString(context));
- import d.semantic.identifier : AliasResolver;
- c.base = AliasResolver!(function Class(identified) {
- static if(is(typeof(identified) : Symbol)) {
- if(auto c = cast(Class) identified) {
- return c;
- }
- }
- static if(is(typeof(identified.location))) {
- import d.exception;
- throw new CompileException(identified.location, typeid(identified).toString() ~ " is not a class.");
- } else {
- // for typeof(null)
- assert(0);
- }
- })(pass).visit(i);
- break; // ?? meaning process only one item??
- }
- // If no inheritance is specified, inherit from object.
- if(!c.base) {
- c.base = pass.object.getObject();
- }
- methodIndex = 0;
- // building the vtable begins?
- // object.Object, let's do some compiler magic.
- if(c is c.base) {
- auto vtblType = Type.get(BuiltinType.Void).getPointer(TypeQualifier.Immutable); // i.e. vtableType is immutable?
- // TODO: use defaultinit.
- auto vtbl = new Field(d.location, 0, vtblType, BuiltinName!"__vtbl", null); // vtable is a field??
- vtbl.step = Step.Processed; // ??
- baseFields = [vtbl];
- fieldIndex = 1;
- } else {
- scheduler.require(c.base); // ??
- fieldIndex = 0;
- foreach(m; c.base.members) {
- if(auto field = cast(Field) m) {
- baseFields ~= field;
- fieldIndex = max(fieldIndex, field.index); // why?
- c.dscope.addSymbol(field); // add the field to the current scope?
- } else if(auto method = cast(Method) m) {
- baseMethods ~= method;
- methodIndex = max(methodIndex, method.index);
- c.dscope.addOverloadableSymbol(method); // ?
- }
- }
- fieldIndex++;
- }
- if (c.hasContext) { // ?? what is a context? how do you define it?
- // XXX: check for duplicate.
- auto ctxPtr = Type.getContextType(ctxSym).getPointer();
- auto ctx = new Field(c.location, fieldIndex++, ctxPtr, BuiltinName!"__ctx", new NullLiteral(c.location, ctxPtr));
- ctx.step = Step.Processed;
- baseFields ~= ctx;
- }
- auto members = DeclarationVisitor(pass, AggregateType.Class).flatten(d.members, c); // what is d.members? and c? flatten converts to a list?
- c.step = Step.Signed;
- // process the members individually now? check for overrides?
- uint overloadCount = 0;
- foreach(m; members) {
- if(auto method = cast(Method) m) {
- scheduler.require(method, Step.Signed); // ??
- auto mt = method.type;
- auto rt = mt.returnType;
- auto ats = mt.parameters[1 .. $]; // are these arguments?
- writeln("sid3: ", method.name.toString(context));
- CandidatesLoop: foreach(ref candidate; baseMethods) {
- writeln("sid4%");
- writeln("sid4: ", candidate.name);
- if (!candidate || method.name != candidate.name) {
- continue;
- }
- auto ct = candidate.type;
- if (ct.isVariadic != mt.isVariadic) {
- continue;
- }
- auto crt = ct.returnType;
- auto cts = ct.parameters[1 .. $];
- if (ats.length != cts.length || rt.isRef != crt.isRef) {
- continue;
- }
- if (implicitCastFrom(pass, rt.getType(), crt.getType()) < CastKind.Exact) { // ??
- continue;
- }
- import std.range;
- foreach(at, ct; lockstep(ats, cts)) { // ??
- if (at.isRef != ct.isRef) {
- continue CandidatesLoop;
- }
- if (implicitCastFrom(pass, ct.getType(), at.getType()) < CastKind.Exact) { // ??
- continue CandidatesLoop;
- }
- }
- // ??
- if(method.index == 0) {
- method.index = candidate.index;
- // Remove candidate from scope.
- auto os = cast(OverloadSet) dscope.resolve(method.name);
- assert(os, "This must be an overload set");
- uint i = 0;
- while (os.set[i] !is candidate) {
- i++;
- }
- foreach(s; os.set[i + 1 .. $]) {
- os.set[i++] = s;
- }
- os.set = os.set[0 .. i];
- overloadCount++;
- candidate = null;
- break;
- } else {
- import d.exception;
- throw new CompileException(
- method.location,
- method.name.toString(context) ~ " overrides a base class methode but is not marked override",
- );
- }
- }
- if(method.index == 0) {
- import d.exception;
- throw new CompileException(method.location, "Override not found for " ~ method.name.toString(context));
- }
- }
- }
- // Remove overloaded base method. // ??
- if (overloadCount) {
- uint i = 0;
- while (baseMethods[i] !is null) {
- i++;
- }
- foreach(baseMethod; baseMethods[i + 1 .. $]) {
- if(baseMethod) {
- baseMethods[i++] = baseMethod;
- }
- }
- baseMethods = baseMethods[0 .. i];
- }
- c.members = cast(Symbol[]) baseFields;
- c.members ~= baseMethods;
- scheduler.require(members);
- c.members ~= members;
- //writeln("sid: ", c.members);
- c.step = Step.Processed; // ??
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement