3 #define YYLTYPE Location
6 extern External curExternal;
7 ///////////////// INSTANCE DECLARATION PASS ///////////////////////
9 // Returns true if we should add a * to the declarator
10 static int ReplaceClassSpec(OldList specs, Specifier spec, bool param)
12 if(spec.type == templateTypeSpecifier)
14 TemplateParameter parameter = spec.templateParameter;
16 if(!param && parameter.dataTypeString)
18 OldList * newSpecs = MkList();
19 Declarator decl = SpecDeclFromString(parameter.dataTypeString, newSpecs, null);
22 Specifier newSpec = CopySpecifier(newSpecs->first);
26 FreeList(newSpecs, FreeSpecifier);
30 bool isPointer = decl.type == pointerDeclarator;
37 else if(!param && parameter.dataType)
39 OldList * newSpecs = parameter.dataType.specifiers;
40 Declarator decl = parameter.dataType.decl;
43 Specifier newSpec = CopySpecifier(newSpecs->first);
50 bool isPointer = decl.type == pointerDeclarator;
57 spec.type = nameSpecifier;
58 spec.name = CopyString("uint64");
59 spec.symbol = FindClass("uint64");
63 if(spec.type == nameSpecifier || spec.type == subClassSpecifier)
65 // TODO: Apply more care because nameSpecifier / subClassSpecifier use different parts of the union!
66 Symbol classSym = spec.symbol;
67 if(spec.type == subClassSpecifier)
69 classSym = FindClass("ecere::com::Class");
74 Class _class = classSym.registered;
76 FreeSpecifierContents(spec);
78 spec.type = nameSpecifier;
79 if(_class && _class.type == structClass)
84 FullClassNameCat(name, _class.fullName, false);
85 FreeSpecifierContents(spec);
86 spec.type = structSpecifier;
87 spec.baseSpecs = null;
88 spec.id = MkIdentifier(name);
90 spec.definitions = null;
92 spec.addNameSpace = false;
94 else if(_class && _class.type == noHeadClass)
97 FullClassNameCat(name, _class.fullName, false);
98 spec.type = structSpecifier;
99 spec.baseSpecs = null;
100 spec.id = MkIdentifier(name);
102 spec.definitions = null;
104 spec.addNameSpace = false;
108 if((_class.type != systemClass ||
109 !strcmp(_class.fullName, "enum") ||
110 (_class.dataTypeString && !strcmp(_class.dataTypeString, "char *")) ||
111 //strcmp(_class.fullName, "bool") &&
112 !strcmp(_class.fullName, "uint64") ||
113 !strcmp(_class.fullName, "uint32") ||
114 !strcmp(_class.fullName, "uint16") ||
115 !strcmp(_class.fullName, "uintptr") ||
116 !strcmp(_class.fullName, "intptr") ||
117 !strcmp(_class.fullName, "uintsize") ||
118 !strcmp(_class.fullName, "intsize") ||
119 !strcmp(_class.fullName, "uint") ||
120 !strcmp(_class.fullName, "byte")))
122 if(_class.dataTypeString)
124 if(!strcmp(_class.dataTypeString, "uint64") ||
125 !strcmp(_class.dataTypeString, "uint32") ||
126 !strcmp(_class.dataTypeString, "uint16") ||
127 !strcmp(_class.dataTypeString, "uintptr") ||
128 !strcmp(_class.dataTypeString, "intptr") ||
129 !strcmp(_class.dataTypeString, "uintsize") ||
130 !strcmp(_class.dataTypeString, "intsize") ||
131 !strcmp(_class.dataTypeString, "uint") ||
132 !strcmp(_class.dataTypeString, "byte"))
135 _class.dataType = ProcessTypeString(_class.dataTypeString, false);
136 if(_class.dataType && _class.dataType.kind == classType)
137 classSym = _class.dataType._class;
139 classSym = FindClass(_class.dataTypeString);
140 _class = classSym ? classSym.registered : null;
143 spec.name = CopyString(!strcmp(_class.dataTypeString, "char *") ? "char" : _class.dataTypeString);
148 spec.name = CopyString(null);
152 else if(!_class.base)
154 spec.type = baseSpecifier;
155 spec.specifier = VOID;
161 spec.type = structSpecifier;
162 spec.id = MkIdentifier("__ecereNameSpace__ecere__com__Instance");
164 spec.baseSpecs = null;
165 spec.definitions = null;
167 spec.addNameSpace = false;
170 if(_class && _class.dataTypeString && !strcmp(_class.dataTypeString, "char *"))
172 if(!_class || _class.type == normalClass || _class.type == noHeadClass)
174 else if(param && _class.type == structClass)
178 else if(spec.type == baseSpecifier)
180 if(spec.specifier == ANY_OBJECT || spec.specifier == CLASS)
182 spec.specifier = CONST;
183 specs.Add(MkSpecifier(VOID));
190 static void ReplaceByInstancePtr(Specifier spec, Declarator * declPtr, int type)
192 Declarator decl = *declPtr;
193 if(decl && decl.type == pointerDeclarator)
195 // Pointers to simple classes shouldn't be added pointers
199 decl.pointer.pointer = MkPointer(null, decl.pointer.pointer);
203 Declarator newDecl { };
207 decl.declarator = newDecl;
211 decl.type = pointerDeclarator;
212 decl.pointer.pointer = MkPointer(null, null);
217 static void InstDeclPassSpecifier(Specifier spec, bool byRefTypedObject)
222 if(spec.specifier == TYPED_OBJECT)
224 spec.type = extendedSpecifier;
225 spec.extDecl = MkExtDeclString(CopyString(byRefTypedObject ?
226 "struct __ecereNameSpace__ecere__com__Class * class, void *" :
227 "struct __ecereNameSpace__ecere__com__Class * class, const void *"));
228 DeclareStruct(curExternal, "ecere::com::Class", false, true);
238 for(e = spec.list->first; e; e = e.next)
245 case structSpecifier:
251 for(def = spec.definitions->first; def; def = def.next)
253 InstDeclPassDeclaration(def.decl);
256 InstDeclPassIdentifier(spec.id);
259 case extendedSpecifier:
260 if(spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s)
262 if(!strcmp(spec.extDecl.s, "dllexport"))
265 delete spec.extDecl.s;
266 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
267 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
271 if(targetPlatform == win32)
272 spec.extDecl.s = CopyString("__declspec(dllexport)");
274 spec.extDecl.s = CopyString("__attribute__ ((visibility(\"default\")))");
278 if(targetPlatform == win32)
279 spec.extDecl.s = CopyString("extern __declspec(dllexport)");
281 spec.extDecl.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
284 else if(!strcmp(spec.extDecl.s, "stdcall") || !strcmp(spec.extDecl.s, "_stdcall") ||
285 !strcmp(spec.extDecl.s, "__stdcall") || !strcmp(spec.extDecl.s, "__stdcall__"))
287 delete spec.extDecl.s;
288 if(targetPlatform == win32)
289 spec.extDecl.s = CopyString("__attribute__((__stdcall__))");
291 spec.extDecl.s = CopyString("");
298 static void InstDeclPassDeclarator(Declarator decl)
302 case structDeclarator:
304 InstDeclPassDeclarator(decl.declarator);
306 case identifierDeclarator:
309 InstDeclPassIdentifier(decl.identifier);
312 case bracketsDeclarator:
314 InstDeclPassDeclarator(decl.declarator);
316 case arrayDeclarator:
318 InstDeclPassDeclarator(decl.declarator);
320 case functionDeclarator:
323 InstDeclPassDeclarator(decl.declarator);
324 if(decl.function.parameters)
328 InstDeclPassDeclarator(decl.declarator);
329 for(type = decl.function.parameters->first; type; type = type.next)
331 bool typedObject = false;
332 Specifier spec = null;
335 spec = (Specifier)type.qualifiers->first;
336 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
340 InstDeclPassTypeName(type, true);
345 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
346 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
348 DeclareStruct(curExternal, "ecere::com::Class", false, true);
349 decl.function.parameters->Insert(spec.prev, _class);
355 case pointerDeclarator:
356 case extendedDeclarator:
357 case extendedDeclaratorEnd:
358 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
360 if(decl.extended.extended.type == extDeclString && decl.extended.extended.s && !strcmp(decl.extended.extended.s, "dllexport"))
362 delete decl.extended.extended.s;
363 if(targetPlatform == win32)
364 decl.extended.extended.s = CopyString("extern __declspec(dllexport)");
366 decl.extended.extended.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
368 else if(decl.extended.extended.type == extDeclString && decl.extended.extended.s &&
369 (!strcmp(decl.extended.extended.s, "stdcall") || !strcmp(decl.extended.extended.s, "_stdcall") ||
370 !strcmp(decl.extended.extended.s, "__stdcall") || !strcmp(decl.extended.extended.s, "__stdcall__")))
372 delete decl.extended.extended.s;
373 if(targetPlatform == win32)
374 decl.extended.extended.s = CopyString("__attribute__((__stdcall__))");
376 decl.extended.extended.s = CopyString("");
380 InstDeclPassDeclarator(decl.declarator);
385 /*static */void InstDeclPassTypeName(TypeName type, bool param)
390 for(spec = type.qualifiers->first; spec; spec = spec.next)
393 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
394 ReplaceByInstancePtr(spec, &type.declarator, result);
397 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
398 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
399 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass))))
400 ReplaceByInstancePtr(spec, &type.declarator, 2);
402 InstDeclPassSpecifier(spec, type.declarator && type.declarator.type == pointerDeclarator);
406 InstDeclPassDeclarator(type.declarator);
409 static void InstDeclPassIdentifier(Identifier id)
411 if(strchr(id.string, ':'))
417 strcpy(newID, "__ecereNameSpace__");
420 for(c = 0; (ch = id.string[c]); c++)
422 if(ch == ':') ch = '_';
428 id.string = CopyString(newID);
432 /*static */bool IsVoidPtrCast(TypeName typeName)
435 Declarator d = typeName.declarator;
436 if(d && d.type == pointerDeclarator && d.pointer.pointer && !d.pointer.pointer.pointer)
438 if(typeName.qualifiers)
441 for(s = typeName.qualifiers->first; s; s = s.next)
443 if(s.type == baseSpecifier && s.specifier == VOID)
451 static void AddPointerCast(Expression e)
453 Type src = e.expType;
455 if(src && (src.kind == templateType || src.kind == classType))
457 if(e.type != castExp || !IsVoidPtrCast(e.cast.typeName))
459 if(src) src.refCount++;
460 if(src.kind == templateType && src.templateParameter && src.templateParameter.type == type)
463 if(src.templateParameter.dataTypeString)
464 newType = ProcessTypeString(src.templateParameter.dataTypeString, false);
465 else if(src.templateParameter.dataType)
466 newType = ProcessType(src.templateParameter.dataType.specifiers, src.templateParameter.dataType.decl);
473 if(src && src.kind == classType && src._class)
475 Class sc = src._class.registered;
476 if(sc && (sc.type == structClass || sc.type == noHeadClass))
478 Type dest = e.destType;
479 if(dest && (dest.kind == templateType || dest.kind == classType))
481 if(dest) dest.refCount++;
483 if(dest.kind == templateType && dest.templateParameter && dest.templateParameter.type == type)
486 if(dest.templateParameter.dataTypeString)
487 newType = ProcessTypeString(dest.templateParameter.dataTypeString, false);
488 else if(dest.templateParameter.dataType)
489 newType = ProcessType(dest.templateParameter.dataType.specifiers, dest.templateParameter.dataType.decl);
496 if(!dest.passAsTemplate && dest.kind == classType && dest._class && dest._class.registered)
498 Class dc = dest._class.registered;
499 if(sc.templateClass) sc = sc.templateClass;
500 if(dc.templateClass) dc = dc.templateClass;
501 if(dc.base && sc != dc)
503 e.cast.exp = MkExpBrackets(MkListOne(MoveExpContents(e)));
505 e.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), QMkPtrDecl(null));
517 static void InstDeclPassExpression(Expression exp)
524 InstDeclPassIdentifier(exp.identifier);
533 InstDeclPassExpression(exp.op.exp1);
536 InstDeclPassExpression(exp.op.exp2);
537 if(exp.op.op != '=' && exp.op.exp1 && exp.op.exp1.expType && exp.op.exp1.expType.kind == pointerType && exp.op.exp1.expType.type && exp.op.exp1.expType.type.kind == templateType &&
538 exp.op.exp2.expType && exp.op.exp2.expType.kind == pointerType && exp.op.exp2.expType.type && exp.op.exp2.expType.type.kind == templateType)
540 Expression e = exp.op.exp2;
541 e.cast.exp = MkExpBrackets(MkListOne(MoveExpContents(e)));
543 e.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), QMkPtrDecl(null));
546 e.cast.exp = MkExpBrackets(MkListOne(MoveExpContents(e)));
548 e.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), QMkPtrDecl(null));
550 else if(exp.op.exp1 && (exp.op.op == '=' || exp.op.op == EQ_OP || exp.op.op == NE_OP))
551 AddPointerCast(exp.op.exp2);
554 case extensionExpressionExp:
558 for(e = exp.list->first; e; e = e.next)
559 InstDeclPassExpression(e);
565 InstDeclPassExpression(exp.index.exp);
566 for(e = exp.index.index->first; e; e = e.next)
567 InstDeclPassExpression(e);
573 InstDeclPassExpression(exp.call.exp);
574 if(exp.call.arguments)
576 for(e = exp.call.arguments->first; e; e = e.next)
578 bool addCast = false;
579 InstDeclPassExpression(e);
582 if(e.expType && e.expType.kind == pointerType && e.expType.type && (e.expType.type.kind == classType || (e.expType.type.kind == pointerType && e.expType.type.type && e.expType.type.type.kind != voidType)) &&
583 e.destType && e.destType.kind == pointerType && e.destType.type && e.destType.type.kind == pointerType && e.destType.type.type && e.destType.type.type.kind == voidType)
585 // Fix for adding a cast to Unserialize with a struct passed as a parameter:
586 else if(e.expType && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass && e.byReference &&
587 e.destType && e.destType.kind == classType && e.destType.classObjectType && e.destType.byReference)
590 if(addCast && (e.type != castExp || !IsVoidPtrCast(e.cast.typeName)))
592 e.cast.exp = MkExpBrackets(MkListOne(MoveExpContents(e)));
594 e.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), QMkPtrDecl(null));
603 InstDeclPassExpression(exp.member.exp);
609 InstDeclPassExpression(exp.member.exp);
613 InstDeclPassTypeName(exp.typeName, false);
617 Type type = exp.expType;
618 // Remove casts to simple structs... (Watch out for pointers later...)
619 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass && !exp.needCast)
621 if(exp.destType && exp.destType.classObjectType == typedObject && exp.destType.byReference)
623 // For Unserialize with a StaticString
624 FreeTypeName(exp.cast.typeName);
625 exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, MkPointer(null, null)), null));
629 Expression castExp = exp.cast.exp;
630 Expression prev = exp.prev, next = exp.next;
632 FreeExpContents(exp);
633 FreeType(exp.expType);
634 FreeType(exp.destType);
639 InstDeclPassExpression(exp);
644 if(exp.expType && exp.expType.kind == pointerType)
646 if(exp.cast.exp && exp.cast.exp.expType && exp.cast.exp.expType.kind == templateType && !exp.cast.exp.expType.isPointerType)
647 exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), exp.cast.exp);
650 InstDeclPassTypeName(exp.cast.typeName, exp.usage.usageArg /*false*/);
653 if(exp.expType && exp.expType.kind == templateType && exp.destType &&
654 (exp.destType.passAsTemplate || (!exp.destType.templateParameter || (!exp.destType.templateParameter.dataType && !exp.destType.templateParameter.dataTypeString))) &&
655 exp.cast.exp.expType && !exp.cast.exp.expType.passAsTemplate && exp.cast.exp.expType.isPointerType)
656 exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), exp.cast.exp);
657 InstDeclPassExpression(exp.cast.exp);
665 InstDeclPassExpression(exp.cond.cond);
666 for(e = exp.cond.exp->first; e; e = e.next)
667 InstDeclPassExpression(e);
668 InstDeclPassExpression(exp.cond.elseExp);
671 case extensionCompoundExp:
673 InstDeclPassStatement(exp.compound);
678 InstDeclPassExpression(exp.vaArg.exp);
681 case extensionInitializerExp:
683 InstDeclPassTypeName(exp.initializer.typeName, false);
684 InstDeclPassInitializer(exp.initializer.initializer);
690 static void InstDeclPassInitializer(Initializer init)
697 InstDeclPassExpression(init.exp);
698 AddPointerCast(init.exp);
701 case listInitializer:
704 for(i = init.list->first; i; i = i.next)
705 InstDeclPassInitializer(i);
711 static void InstDeclPassDeclaration(Declaration decl)
715 case initDeclaration:
720 for(spec = decl.specifiers->first; spec; spec = spec.next)
723 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
728 for(d = decl.declarators->first; d; d = d.next)
729 ReplaceByInstancePtr(spec, &d.declarator, type);
732 InstDeclPassSpecifier(spec, false);
738 for(d = decl.declarators->first; d; d = d.next)
740 InstDeclPassDeclarator(d.declarator);
742 InstDeclPassInitializer(d.initializer);
747 case structDeclaration:
752 for(spec = decl.specifiers->first; spec; spec = spec.next)
755 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
760 for(d = decl.declarators->first; d; d = d.next)
761 ReplaceByInstancePtr(spec, &d, type);
764 InstDeclPassSpecifier(spec, false);
771 for(d = decl.declarators->first; d; d = d.next)
772 InstDeclPassDeclarator(d);
776 // This should be removed by now?
777 case instDeclaration:
778 // InstDeclPassInstantiation(decl.inst);
783 static void InstDeclPassStatement(Statement stmt)
787 case badDeclarationStmt:
789 InstDeclPassDeclaration(stmt.decl);
792 InstDeclPassStatement(stmt.labeled.stmt);
795 // This expression should be constant...
796 if(stmt.caseStmt.exp)
797 InstDeclPassExpression(stmt.caseStmt.exp);
798 if(stmt.caseStmt.stmt)
799 InstDeclPassStatement(stmt.caseStmt.stmt);
805 Context prevContext = curContext;
807 if(!stmt.compound.isSwitch)
808 curContext = stmt.compound.context;
810 if(stmt.compound.declarations)
812 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
813 InstDeclPassDeclaration(decl);
815 if(stmt.compound.statements)
817 for(s = stmt.compound.statements->first; s; s = s.next)
818 InstDeclPassStatement(s);
820 curContext = prevContext;
828 for(exp = stmt.expressions->first; exp; exp = exp.next)
829 InstDeclPassExpression(exp);
838 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
839 InstDeclPassExpression(exp);
842 InstDeclPassStatement(stmt.ifStmt.stmt);
843 if(stmt.ifStmt.elseStmt)
844 InstDeclPassStatement(stmt.ifStmt.elseStmt);
850 if(stmt.switchStmt.exp)
852 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
853 InstDeclPassExpression(exp);
855 InstDeclPassStatement(stmt.switchStmt.stmt);
861 if(stmt.whileStmt.exp)
863 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
864 InstDeclPassExpression(exp);
866 InstDeclPassStatement(stmt.whileStmt.stmt);
874 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
875 InstDeclPassExpression(exp);
877 if(stmt.doWhile.stmt)
878 InstDeclPassStatement(stmt.doWhile.stmt);
884 if(stmt.forStmt.init)
885 InstDeclPassStatement(stmt.forStmt.init);
886 if(stmt.forStmt.check)
887 InstDeclPassStatement(stmt.forStmt.check);
888 if(stmt.forStmt.increment)
890 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
891 InstDeclPassExpression(exp);
893 if(stmt.forStmt.stmt)
894 InstDeclPassStatement(stmt.forStmt.stmt);
908 for(exp = stmt.expressions->first; exp; exp = exp.next)
909 InstDeclPassExpression(exp);
910 AddPointerCast(stmt.expressions->last);
917 if(stmt.asmStmt.inputFields)
919 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
921 InstDeclPassExpression(field.expression);
923 if(stmt.asmStmt.outputFields)
925 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
927 InstDeclPassExpression(field.expression);
929 if(stmt.asmStmt.clobberedFields)
931 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
933 InstDeclPassExpression(field.expression);
940 void TopoSort(OldList * input)
948 for(n = input->first; n; n = next)
951 if(n.type == declarationExternal && !n.declaration)
954 if(n.symbol && n.symbol.structExternal == n)
955 n.symbol.structExternal = null;
958 else if(!n.incoming.count)
963 else if(!n.nonBreakableIncoming)
970 for(x = input->first; x; x = x.next)
973 for(e : x.incoming; !e.breakable)
975 if(count != x.nonBreakableIncoming)
976 printf("Bug in input");
978 if(!x.incoming.count)
979 printf("This node should be in S!\n");
982 External y, from = e.from;
983 for(y = input->first; y; y = y.next)
988 for(y = B.first; y; y = y.next)
995 for(y = S.first; y; y = y.next)
1001 printf("This node is nowhere!\n");
1012 for(x = input->first; x; x = x.next)
1015 for(e : x.incoming; !e.breakable)
1017 if(count != x.nonBreakableIncoming)
1018 printf("Bug in input");
1019 if(!x.incoming.count)
1020 printf("This node should be in S!\n");
1023 External y, from = e.from;
1024 for(y = input->first; y; y = y.next)
1029 for(y = B.first; y; y = y.next)
1036 for(y = S.first; y; y = y.next)
1042 printf("This node is nowhere!\n");
1047 //DebugPrint("*** Free Node: [", n.id, "]\n\t", n.output);
1048 S.Remove((IteratorPointer)n);
1051 if(n && n.symbol && n.symbol.string && !strcmp(n.symbol.string, "ecere::com::Instance"))
1052 printf("Adding Instance\n");
1056 for(e = n.outgoing.first; e; e = ne)
1061 //DebugPrint(" This Free Node has an edge to [", m.id, "] ", m.output);
1062 if(m.nonBreakableIncoming)
1064 //DebugPrint("... which we think is in input");
1069 //DebugPrint("... which we think is in B");
1074 PrintLn("!!! Something's wrong !!!");
1080 if(!m.nonBreakableIncoming)
1083 m.nonBreakableIncoming--;
1084 //DebugPrint("Reducing non breakable incoming, now ", m.nonBreakableIncoming);
1087 n.outgoing.Remove((IteratorPointer)e);
1088 m.incoming.Remove((IteratorPointer)e);
1091 if(!m.incoming.count)
1093 //DebugPrint("Last edge to this node taken out, moving to S...");
1097 else if(!m.nonBreakableIncoming)
1099 //DebugPrint("Last non-breakable edge to this node taken out, moving to B...");
1105 else if((n = B.first))
1107 //DebugPrint("Breaking some of the ", n.incoming.count, " incoming edges to [", n.id, "] ", n.output);
1108 B.Remove((IteratorPointer)n);
1110 // Break the edges of this node
1111 for(e = n.incoming.first; e; e = ne)
1114 External m = e.from;
1117 for(x = input->first; x; x = x.next)
1120 for(e : x.incoming; !e.breakable)
1122 if(count != x.nonBreakableIncoming)
1123 printf("Bug in input");
1124 if(!x.incoming.count)
1125 printf("This node should be in S!\n");
1128 External y, from = e.from;
1129 for(y = input->first; y; y = y.next)
1134 for(y = B.first; y; y = y.next)
1141 for(y = S.first; y; y = y.next)
1147 printf("This node is nowhere!\n");
1152 f = m.ForwardDeclare();
1155 for(x = input->first; x; x = x.next)
1158 for(e : x.incoming; !e.breakable)
1160 if(count != x.nonBreakableIncoming)
1161 printf("Bug in input");
1162 if(!x.incoming.count)
1163 printf("This node should be in S!\n");
1166 External y, from = e.from;
1167 for(y = input->first; y; y = y.next)
1172 for(y = B.first; y; y = y.next)
1179 for(y = S.first; y; y = y.next)
1183 if(!y && from != n && from != f)
1185 printf("This node is nowhere!\n");
1190 // Recheck input for edges created by forward declaration
1193 for(c = input->first; c; c = next)
1196 if(!c.incoming.count)
1201 else if(!c.nonBreakableIncoming)
1209 //DebugPrint("Breaking edge from ", e.from.id, " to ", e.to.id);
1210 //DebugPrint("Creating a forward decl node [", f.id, "] for [", m.id, "]");
1212 for(e2 = m.outgoing.first; e2; e2 = n2)
1217 External to = e2.to;
1220 ;//DebugPrint("Breaking this particular connection");
1222 ;//DebugPrint("Also redirecting connection from ", m.id, " to ", to.id, " to come from ", f.id, " instead.");
1223 e2.breakable = false;
1225 m.outgoing.Remove((IteratorPointer)e2);
1227 to.nonBreakableIncoming++;
1228 if(e2 != e && to.nonBreakableIncoming == 1)
1230 // If this node was previously in B, move it to input
1235 //DebugPrint("Node ", e2.to.id, " now has ", e2.to.nonBreakableIncoming, " non-breakable incoming edges.");
1239 for(x = input->first; x; x = x.next)
1242 for(e : x.incoming; !e.breakable)
1244 if(count != x.nonBreakableIncoming)
1245 printf("Bug in input");
1246 if(!x.incoming.count)
1247 printf("This node should be in S!\n");
1250 External y, from = e.from;
1251 for(y = input->first; y; y = y.next)
1256 for(y = B.first; y; y = y.next)
1263 for(y = S.first; y; y = y.next)
1267 if(!y && from != n && from != f)
1269 printf("This node is nowhere!\n");
1273 if(!f.incoming.count)
1275 else if(!f.nonBreakableIncoming)
1280 for(x = input->first; x; x = x.next)
1283 for(e : x.incoming; !e.breakable)
1285 if(count != x.nonBreakableIncoming)
1286 printf("Bug in input");
1287 if(!x.incoming.count)
1288 printf("This node should be in S!\n");
1291 External y, from = e.from;
1292 for(y = input->first; y; y = y.next)
1297 for(y = B.first; y; y = y.next)
1304 for(y = S.first; y; y = y.next)
1311 for(y = L.first; y; y = y.next)
1314 PrintLn("This node is already in L!");
1321 ConsoleFile file { };
1322 printf("This node is nowhere!\n");
1323 OutputExternal(from, file);
1329 // Avoid needless edge breaking by processing a node as soon as one shows up in S
1334 // Put n back in input because it now has unbreakable edges
1343 External e = input->first;
1345 Compiler_Error("declarations cycles found\n");
1347 //OutputTree(input, f);
1349 for(x = input->first; x; x = x.next)
1352 for(e : x.incoming; !e.breakable)
1354 if(count != x.nonBreakableIncoming)
1355 printf("Bug in input");
1356 if(!x.incoming.count)
1357 printf("This node should be in S!\n");
1361 External y, from = e.from;
1362 for(y = input->first; y; y = y.next)
1367 for(y = B.first; y; y = y.next)
1374 for(y = S.first; y; y = y.next)
1380 printf("This node is nowhere!\n");
1385 SetOutputLineNumbers(false);
1386 OutputExternal(e, f);
1388 PrintLn("\nDepends on:\n");
1389 { TopoEdge i; for(i = e.incoming.last; i && !i.breakable && i.from.incoming.count; i = i.in.next) { e = i.from; break; } }
1391 OutputExternal(e, f);
1393 PrintLn("\nWhile that depends on:\n");
1394 { TopoEdge i; for(i = e.incoming.first; i && !i.breakable && i.from.incoming.count; i = i.in.next) { e = i.from; break; } }
1396 OutputExternal(e, f);
1398 PrintLn("\nWhile that depends on:\n");
1399 { TopoEdge i; for(i = e.incoming.first; i && !i.breakable && i.from.incoming.count; i = i.in.next) { e = i.from; break; } }
1401 OutputExternal(e, f);
1403 PrintLn("\nWhile that depends on:\n");
1404 { TopoEdge i; for(i = e.incoming.first; i && !i.breakable && i.from.incoming.count; i = i.in.next) { e = i.from; break; } }
1406 OutputExternal(e, f);
1411 while((e = input->first))
1425 for(n = input->first; n; n = next)
1428 if(n.type == declarationExternal && (!n.declaration || ((!n.declaration.specifiers || !n.declaration.specifiers->count) && (!n.declaration.declarators || !n.declaration.declarators->count))))
1431 if(n.symbol && n.symbol.structExternal == n)
1432 n.symbol.structExternal = null;
1438 public void ProcessInstanceDeclarations()
1441 curContext = globalContext;
1442 for(external = ast->first; external; external = external.next)
1444 curExternal = external;
1445 if(external.type == functionExternal)
1447 FunctionDefinition func = external.function;
1451 for(spec = func.specifiers->first; spec; spec = spec.next)
1454 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
1455 ReplaceByInstancePtr(spec, &func.declarator, type);
1456 InstDeclPassSpecifier(spec, false);
1459 InstDeclPassDeclarator(func.declarator);
1461 InstDeclPassStatement(func.body);
1463 else if(external.type == declarationExternal)
1465 if(external.declaration)
1466 InstDeclPassDeclaration(external.declaration);
1470 // Perform topological sort