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)
182 spec.specifier = VOID;
189 static void ReplaceByInstancePtr(Specifier spec, Declarator * declPtr, int type)
191 Declarator decl = *declPtr;
192 if(decl && decl.type == pointerDeclarator)
194 // Pointers to simple classes shouldn't be added pointers
198 decl.pointer.pointer = MkPointer(null, decl.pointer.pointer);
202 Declarator newDecl { };
206 decl.declarator = newDecl;
210 decl.type = pointerDeclarator;
211 decl.pointer.pointer = MkPointer(null, null);
216 static void InstDeclPassSpecifier(Specifier spec)
221 if(spec.specifier == TYPED_OBJECT)
223 spec.type = extendedSpecifier;
224 spec.extDecl = MkExtDeclString(CopyString("struct __ecereNameSpace__ecere__com__Class * class, void *"));
234 for(e = spec.list->first; e; e = e.next)
241 case structSpecifier:
247 for(def = spec.definitions->first; def; def = def.next)
249 InstDeclPassDeclaration(def.decl);
252 InstDeclPassIdentifier(spec.id);
255 case extendedSpecifier:
256 if(spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s)
258 if(!strcmp(spec.extDecl.s, "dllexport"))
261 delete spec.extDecl.s;
262 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
263 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
267 if(targetPlatform == win32)
268 spec.extDecl.s = CopyString("__declspec(dllexport)");
270 spec.extDecl.s = CopyString("__attribute__ ((visibility(\"default\")))");
274 if(targetPlatform == win32)
275 spec.extDecl.s = CopyString("extern __declspec(dllexport)");
277 spec.extDecl.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
280 else if(!strcmp(spec.extDecl.s, "stdcall") || !strcmp(spec.extDecl.s, "_stdcall") ||
281 !strcmp(spec.extDecl.s, "__stdcall") || !strcmp(spec.extDecl.s, "__stdcall__"))
283 delete spec.extDecl.s;
284 if(targetPlatform == win32)
285 spec.extDecl.s = CopyString("__attribute__((__stdcall__))");
287 spec.extDecl.s = CopyString("");
294 static void InstDeclPassDeclarator(Declarator decl)
298 case structDeclarator:
300 InstDeclPassDeclarator(decl.declarator);
302 case identifierDeclarator:
305 InstDeclPassIdentifier(decl.identifier);
308 case bracketsDeclarator:
310 InstDeclPassDeclarator(decl.declarator);
312 case arrayDeclarator:
314 InstDeclPassDeclarator(decl.declarator);
316 case functionDeclarator:
319 InstDeclPassDeclarator(decl.declarator);
320 if(decl.function.parameters)
324 InstDeclPassDeclarator(decl.declarator);
325 for(type = decl.function.parameters->first; type; type = type.next)
327 bool typedObject = false;
328 Specifier spec = null;
331 spec = (Specifier)type.qualifiers->first;
332 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
336 InstDeclPassTypeName(type, true);
341 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
342 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
344 decl.function.parameters->Insert(spec.prev, _class);
350 case pointerDeclarator:
351 case extendedDeclarator:
352 case extendedDeclaratorEnd:
353 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
355 if(decl.extended.extended.type == extDeclString && decl.extended.extended.s && !strcmp(decl.extended.extended.s, "dllexport"))
357 delete decl.extended.extended.s;
358 if(targetPlatform == win32)
359 decl.extended.extended.s = CopyString("extern __declspec(dllexport)");
361 decl.extended.extended.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
363 else if(decl.extended.extended.type == extDeclString && decl.extended.extended.s &&
364 (!strcmp(decl.extended.extended.s, "stdcall") || !strcmp(decl.extended.extended.s, "_stdcall") ||
365 !strcmp(decl.extended.extended.s, "__stdcall") || !strcmp(decl.extended.extended.s, "__stdcall__")))
367 delete decl.extended.extended.s;
368 if(targetPlatform == win32)
369 decl.extended.extended.s = CopyString("__attribute__((__stdcall__))");
371 decl.extended.extended.s = CopyString("");
375 InstDeclPassDeclarator(decl.declarator);
380 /*static */void InstDeclPassTypeName(TypeName type, bool param)
385 for(spec = type.qualifiers->first; spec; spec = spec.next)
388 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
389 ReplaceByInstancePtr(spec, &type.declarator, result);
392 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
393 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
394 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass))))
395 ReplaceByInstancePtr(spec, &type.declarator, 2);
397 InstDeclPassSpecifier(spec);
401 InstDeclPassDeclarator(type.declarator);
404 static void InstDeclPassIdentifier(Identifier id)
406 if(strchr(id.string, ':'))
412 strcpy(newID, "__ecereNameSpace__");
415 for(c = 0; (ch = id.string[c]); c++)
417 if(ch == ':') ch = '_';
423 id.string = CopyString(newID);
427 static void InstDeclPassExpression(Expression exp)
434 InstDeclPassIdentifier(exp.identifier);
443 InstDeclPassExpression(exp.op.exp1);
445 InstDeclPassExpression(exp.op.exp2);
447 case extensionExpressionExp:
451 for(e = exp.list->first; e; e = e.next)
452 InstDeclPassExpression(e);
458 InstDeclPassExpression(exp.index.exp);
459 for(e = exp.index.index->first; e; e = e.next)
460 InstDeclPassExpression(e);
466 InstDeclPassExpression(exp.call.exp);
467 if(exp.call.arguments)
469 for(e = exp.call.arguments->first; e; e = e.next)
470 InstDeclPassExpression(e);
477 InstDeclPassExpression(exp.member.exp);
483 InstDeclPassExpression(exp.member.exp);
487 InstDeclPassTypeName(exp.typeName, false);
491 Type type = exp.expType;
492 // Remove casts to simple structs... (Watch out for pointers later...)
493 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass)
495 Expression castExp = exp.cast.exp;
496 Expression prev = exp.prev, next = exp.next;
498 FreeExpContents(exp);
499 FreeType(exp.expType);
500 FreeType(exp.destType);
505 InstDeclPassExpression(exp);
509 InstDeclPassTypeName(exp.cast.typeName, false);
511 InstDeclPassExpression(exp.cast.exp);
518 InstDeclPassExpression(exp.cond.cond);
519 for(e = exp.cond.exp->first; e; e = e.next)
520 InstDeclPassExpression(e);
521 InstDeclPassExpression(exp.cond.elseExp);
524 case extensionCompoundExp:
526 InstDeclPassStatement(exp.compound);
531 InstDeclPassExpression(exp.vaArg.exp);
534 case extensionInitializerExp:
536 InstDeclPassTypeName(exp.initializer.typeName, false);
537 InstDeclPassInitializer(exp.initializer.initializer);
543 static void InstDeclPassInitializer(Initializer init)
549 InstDeclPassExpression(init.exp);
551 case listInitializer:
554 for(i = init.list->first; i; i = i.next)
555 InstDeclPassInitializer(i);
561 static void InstDeclPassDeclaration(Declaration decl)
565 case initDeclaration:
570 for(spec = decl.specifiers->first; spec; spec = spec.next)
573 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
578 for(d = decl.declarators->first; d; d = d.next)
579 ReplaceByInstancePtr(spec, &d.declarator, type);
582 InstDeclPassSpecifier(spec);
588 for(d = decl.declarators->first; d; d = d.next)
590 InstDeclPassDeclarator(d.declarator);
592 InstDeclPassInitializer(d.initializer);
597 case structDeclaration:
602 for(spec = decl.specifiers->first; spec; spec = spec.next)
605 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
610 for(d = decl.declarators->first; d; d = d.next)
611 ReplaceByInstancePtr(spec, &d, type);
614 InstDeclPassSpecifier(spec);
621 for(d = decl.declarators->first; d; d = d.next)
622 InstDeclPassDeclarator(d);
626 // This should be removed by now?
627 case instDeclaration:
628 // InstDeclPassInstantiation(decl.inst);
633 static void InstDeclPassStatement(Statement stmt)
637 case badDeclarationStmt:
639 InstDeclPassDeclaration(stmt.decl);
642 InstDeclPassStatement(stmt.labeled.stmt);
645 // This expression should be constant...
646 if(stmt.caseStmt.exp)
647 InstDeclPassExpression(stmt.caseStmt.exp);
648 if(stmt.caseStmt.stmt)
649 InstDeclPassStatement(stmt.caseStmt.stmt);
655 Context prevContext = curContext;
657 if(!stmt.compound.isSwitch)
658 curContext = stmt.compound.context;
660 if(stmt.compound.declarations)
662 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
663 InstDeclPassDeclaration(decl);
665 if(stmt.compound.statements)
667 for(s = stmt.compound.statements->first; s; s = s.next)
668 InstDeclPassStatement(s);
670 curContext = prevContext;
678 for(exp = stmt.expressions->first; exp; exp = exp.next)
679 InstDeclPassExpression(exp);
688 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
689 InstDeclPassExpression(exp);
692 InstDeclPassStatement(stmt.ifStmt.stmt);
693 if(stmt.ifStmt.elseStmt)
694 InstDeclPassStatement(stmt.ifStmt.elseStmt);
700 if(stmt.switchStmt.exp)
702 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
703 InstDeclPassExpression(exp);
705 InstDeclPassStatement(stmt.switchStmt.stmt);
711 if(stmt.whileStmt.exp)
713 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
714 InstDeclPassExpression(exp);
716 InstDeclPassStatement(stmt.whileStmt.stmt);
724 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
725 InstDeclPassExpression(exp);
727 if(stmt.doWhile.stmt)
728 InstDeclPassStatement(stmt.doWhile.stmt);
734 if(stmt.forStmt.init)
735 InstDeclPassStatement(stmt.forStmt.init);
736 if(stmt.forStmt.check)
737 InstDeclPassStatement(stmt.forStmt.check);
738 if(stmt.forStmt.increment)
740 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
741 InstDeclPassExpression(exp);
743 if(stmt.forStmt.stmt)
744 InstDeclPassStatement(stmt.forStmt.stmt);
758 for(exp = stmt.expressions->first; exp; exp = exp.next)
759 InstDeclPassExpression(exp);
766 if(stmt.asmStmt.inputFields)
768 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
770 InstDeclPassExpression(field.expression);
772 if(stmt.asmStmt.outputFields)
774 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
776 InstDeclPassExpression(field.expression);
778 if(stmt.asmStmt.clobberedFields)
780 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
782 InstDeclPassExpression(field.expression);
789 public void ProcessInstanceDeclarations()
792 curContext = globalContext;
793 for(external = ast->first; external; external = external.next)
795 curExternal = external;
796 if(external.type == functionExternal)
798 FunctionDefinition func = external.function;
802 for(spec = func.specifiers->first; spec; spec = spec.next)
805 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
806 ReplaceByInstancePtr(spec, &func.declarator, type);
807 InstDeclPassSpecifier(spec);
810 InstDeclPassDeclarator(func.declarator);
812 InstDeclPassStatement(func.body);
814 else if(external.type == declarationExternal)
816 if(external.declaration)
817 InstDeclPassDeclaration(external.declaration);