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 bool 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;
75 spec.type = nameSpecifier;
79 FreeList(spec.templateArgs, FreeTemplateArgument);
80 spec.templateArgs = null;
83 if(_class && _class.type == structClass)
88 FullClassNameCat(name, _class.fullName, false);
89 //spec.name = CopyString(name);
91 spec.type = structSpecifier;
92 spec.baseSpecs = null;
93 spec.id = MkIdentifier(name);
95 spec.definitions = null;
97 spec.addNameSpace = false;
99 else if(_class && _class.type == noHeadClass)
101 char name[1024] = "";
102 FullClassNameCat(name, _class.fullName, false);
104 spec.type = structSpecifier;
105 spec.baseSpecs = null;
106 spec.id = MkIdentifier(name);
108 spec.definitions = null;
110 spec.addNameSpace = false;
114 if((_class.type != systemClass ||
115 !strcmp(_class.fullName, "enum") ||
116 (_class.dataTypeString && !strcmp(_class.dataTypeString, "char *")) ||
117 //strcmp(_class.fullName, "bool") &&
118 !strcmp(_class.fullName, "uint64") ||
119 !strcmp(_class.fullName, "uint32") ||
120 !strcmp(_class.fullName, "uint16") ||
121 !strcmp(_class.fullName, "uintptr") ||
122 !strcmp(_class.fullName, "intptr") ||
123 !strcmp(_class.fullName, "uintsize") ||
124 !strcmp(_class.fullName, "intsize") ||
125 !strcmp(_class.fullName, "uint") ||
126 !strcmp(_class.fullName, "byte")))
129 if(_class.dataTypeString)
131 if(!strcmp(_class.dataTypeString, "uint64") ||
132 !strcmp(_class.dataTypeString, "uint32") ||
133 !strcmp(_class.dataTypeString, "uint16") ||
134 !strcmp(_class.dataTypeString, "uintptr") ||
135 !strcmp(_class.dataTypeString, "intptr") ||
136 !strcmp(_class.dataTypeString, "uintsize") ||
137 !strcmp(_class.dataTypeString, "intsize") ||
138 !strcmp(_class.dataTypeString, "uint") ||
139 !strcmp(_class.dataTypeString, "byte"))
142 _class.dataType = ProcessTypeString(_class.dataTypeString, false);
143 if(_class.dataType && _class.dataType.kind == classType)
144 classSym = _class.dataType._class;
146 classSym = FindClass(_class.dataTypeString);
147 _class = classSym ? classSym.registered : null;
150 if(!strcmp(_class.dataTypeString, "char *"))
151 spec.name = CopyString("char");
153 spec.name = CopyString(_class.dataTypeString);
158 spec.name = CopyString(null);
162 else if(!_class.base)
165 spec.type = baseSpecifier;
166 spec.specifier = VOID;
173 spec.type = structSpecifier;
174 spec.id = MkIdentifier("__ecereNameSpace__ecere__com__Instance");
176 spec.baseSpecs = null;
177 spec.definitions = null;
179 spec.addNameSpace = false;
182 if(_class && _class.dataTypeString && !strcmp(_class.dataTypeString, "char *"))
183 return true; //false;
184 if(!_class || _class.type == normalClass || _class.type == noHeadClass)
186 else if(param && _class.type == structClass)
190 else if(spec.type == baseSpecifier)
192 if(spec.specifier == ANY_OBJECT)
194 spec.specifier = VOID;
201 static void ReplaceByInstancePtr(Specifier spec, Declarator * declPtr, int type)
203 Declarator decl = *declPtr;
204 if(decl && decl.type == pointerDeclarator)
206 // Pointers to simple classes shouldn't be added pointers
210 decl.pointer.pointer = MkPointer(null, decl.pointer.pointer);
214 Declarator newDecl { };
218 decl.declarator = newDecl;
222 decl.type = pointerDeclarator;
223 decl.pointer.pointer = MkPointer(null, null);
228 static void InstDeclPassSpecifier(Specifier spec)
233 if(spec.specifier == TYPED_OBJECT)
235 spec.type = extendedSpecifier;
236 spec.extDecl = MkExtDeclString(CopyString("struct __ecereNameSpace__ecere__com__Class * class, void *"));
246 for(e = spec.list->first; e; e = e.next)
253 case structSpecifier:
259 for(def = spec.definitions->first; def; def = def.next)
261 InstDeclPassDeclaration(def.decl);
264 InstDeclPassIdentifier(spec.id);
267 case extendedSpecifier:
268 if(spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s)
270 if(!strcmp(spec.extDecl.s, "dllexport"))
273 delete spec.extDecl.s;
274 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
275 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
279 if(targetPlatform == win32)
280 spec.extDecl.s = CopyString("__declspec(dllexport)");
282 spec.extDecl.s = CopyString("__attribute__ ((visibility(\"default\")))");
286 if(targetPlatform == win32)
287 spec.extDecl.s = CopyString("extern __declspec(dllexport)");
289 spec.extDecl.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
292 else if(!strcmp(spec.extDecl.s, "stdcall") || !strcmp(spec.extDecl.s, "_stdcall") ||
293 !strcmp(spec.extDecl.s, "__stdcall") || !strcmp(spec.extDecl.s, "__stdcall__"))
295 delete spec.extDecl.s;
296 if(targetPlatform == win32)
297 spec.extDecl.s = CopyString("__attribute__((__stdcall__))");
299 spec.extDecl.s = CopyString("");
306 static void InstDeclPassDeclarator(Declarator decl)
310 case structDeclarator:
312 InstDeclPassDeclarator(decl.declarator);
314 case identifierDeclarator:
317 InstDeclPassIdentifier(decl.identifier);
320 case bracketsDeclarator:
322 InstDeclPassDeclarator(decl.declarator);
324 case arrayDeclarator:
326 InstDeclPassDeclarator(decl.declarator);
328 case functionDeclarator:
331 InstDeclPassDeclarator(decl.declarator);
332 if(decl.function.parameters)
336 InstDeclPassDeclarator(decl.declarator);
337 for(type = decl.function.parameters->first; type; type = type.next)
339 bool typedObject = false;
340 Specifier spec = null;
343 spec = (Specifier)type.qualifiers->first;
344 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
348 InstDeclPassTypeName(type, true);
353 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
354 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
356 decl.function.parameters->Insert(spec.prev, _class);
362 case pointerDeclarator:
363 case extendedDeclarator:
364 case extendedDeclaratorEnd:
365 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
367 if(decl.extended.extended.type == extDeclString && decl.extended.extended.s && !strcmp(decl.extended.extended.s, "dllexport"))
369 delete decl.extended.extended.s;
370 if(targetPlatform == win32)
371 decl.extended.extended.s = CopyString("extern __declspec(dllexport)");
373 decl.extended.extended.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
375 else if(decl.extended.extended.type == extDeclString && decl.extended.extended.s &&
376 (!strcmp(decl.extended.extended.s, "stdcall") || !strcmp(decl.extended.extended.s, "_stdcall") ||
377 !strcmp(decl.extended.extended.s, "__stdcall") || !strcmp(decl.extended.extended.s, "__stdcall__")))
379 delete decl.extended.extended.s;
380 if(targetPlatform == win32)
381 decl.extended.extended.s = CopyString("__attribute__((__stdcall__))");
383 decl.extended.extended.s = CopyString("");
387 InstDeclPassDeclarator(decl.declarator);
392 /*static */void InstDeclPassTypeName(TypeName type, bool param)
397 for(spec = type.qualifiers->first; spec; spec = spec.next)
400 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
401 ReplaceByInstancePtr(spec, &type.declarator, result);
404 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
405 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
406 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass))))
407 ReplaceByInstancePtr(spec, &type.declarator, 2);
409 InstDeclPassSpecifier(spec);
413 InstDeclPassDeclarator(type.declarator);
416 static void InstDeclPassIdentifier(Identifier id)
418 if(strchr(id.string, ':'))
424 strcpy(newID, "__ecereNameSpace__");
427 for(c = 0; (ch = id.string[c]); c++)
429 if(ch == ':') ch = '_';
435 id.string = CopyString(newID);
439 static void InstDeclPassExpression(Expression exp)
446 InstDeclPassIdentifier(exp.identifier);
455 InstDeclPassExpression(exp.op.exp1);
457 InstDeclPassExpression(exp.op.exp2);
459 case extensionExpressionExp:
463 for(e = exp.list->first; e; e = e.next)
464 InstDeclPassExpression(e);
470 InstDeclPassExpression(exp.index.exp);
471 for(e = exp.index.index->first; e; e = e.next)
472 InstDeclPassExpression(e);
478 InstDeclPassExpression(exp.call.exp);
479 if(exp.call.arguments)
481 for(e = exp.call.arguments->first; e; e = e.next)
482 InstDeclPassExpression(e);
488 InstDeclPassExpression(exp.member.exp);
493 InstDeclPassExpression(exp.member.exp);
497 InstDeclPassTypeName(exp.typeName, false);
501 Type type = exp.expType;
502 // Remove casts to simple structs... (Watch out for pointers later...)
503 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass)
505 Expression castExp = exp.cast.exp;
506 Expression prev = exp.prev, next = exp.next;
508 FreeExpContents(exp);
509 FreeType(exp.expType);
510 FreeType(exp.destType);
515 InstDeclPassExpression(exp);
519 InstDeclPassTypeName(exp.cast.typeName, false);
521 InstDeclPassExpression(exp.cast.exp);
528 InstDeclPassExpression(exp.cond.cond);
529 for(e = exp.cond.exp->first; e; e = e.next)
530 InstDeclPassExpression(e);
531 InstDeclPassExpression(exp.cond.elseExp);
534 case extensionCompoundExp:
536 InstDeclPassStatement(exp.compound);
541 InstDeclPassExpression(exp.vaArg.exp);
544 case extensionInitializerExp:
546 InstDeclPassTypeName(exp.initializer.typeName, false);
547 InstDeclPassInitializer(exp.initializer.initializer);
553 static void InstDeclPassInitializer(Initializer init)
558 InstDeclPassExpression(init.exp);
560 case listInitializer:
563 for(i = init.list->first; i; i = i.next)
564 InstDeclPassInitializer(i);
570 static void InstDeclPassDeclaration(Declaration decl)
574 case initDeclaration:
579 for(spec = decl.specifiers->first; spec; spec = spec.next)
582 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
587 for(d = decl.declarators->first; d; d = d.next)
588 ReplaceByInstancePtr(spec, &d.declarator, type);
591 InstDeclPassSpecifier(spec);
597 for(d = decl.declarators->first; d; d = d.next)
599 InstDeclPassDeclarator(d.declarator);
601 InstDeclPassInitializer(d.initializer);
606 case structDeclaration:
611 for(spec = decl.specifiers->first; spec; spec = spec.next)
614 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
619 for(d = decl.declarators->first; d; d = d.next)
620 ReplaceByInstancePtr(spec, &d, type);
623 InstDeclPassSpecifier(spec);
630 for(d = decl.declarators->first; d; d = d.next)
631 InstDeclPassDeclarator(d);
635 // This should be removed by now?
636 case instDeclaration:
637 // InstDeclPassInstantiation(decl.inst);
642 static void InstDeclPassStatement(Statement stmt)
646 case badDeclarationStmt:
648 InstDeclPassDeclaration(stmt.decl);
651 InstDeclPassStatement(stmt.labeled.stmt);
654 // This expression should be constant...
655 if(stmt.caseStmt.exp)
656 InstDeclPassExpression(stmt.caseStmt.exp);
657 if(stmt.caseStmt.stmt)
658 InstDeclPassStatement(stmt.caseStmt.stmt);
664 Context prevContext = curContext;
666 if(!stmt.compound.isSwitch)
667 curContext = stmt.compound.context;
669 if(stmt.compound.declarations)
671 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
672 InstDeclPassDeclaration(decl);
674 if(stmt.compound.statements)
676 for(s = stmt.compound.statements->first; s; s = s.next)
677 InstDeclPassStatement(s);
679 curContext = prevContext;
687 for(exp = stmt.expressions->first; exp; exp = exp.next)
688 InstDeclPassExpression(exp);
697 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
698 InstDeclPassExpression(exp);
701 InstDeclPassStatement(stmt.ifStmt.stmt);
702 if(stmt.ifStmt.elseStmt)
703 InstDeclPassStatement(stmt.ifStmt.elseStmt);
709 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
710 InstDeclPassExpression(exp);
711 InstDeclPassStatement(stmt.switchStmt.stmt);
717 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
718 InstDeclPassExpression(exp);
719 InstDeclPassStatement(stmt.whileStmt.stmt);
727 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
728 InstDeclPassExpression(exp);
730 if(stmt.doWhile.stmt)
731 InstDeclPassStatement(stmt.doWhile.stmt);
737 if(stmt.forStmt.init)
738 InstDeclPassStatement(stmt.forStmt.init);
739 if(stmt.forStmt.check)
740 InstDeclPassStatement(stmt.forStmt.check);
741 if(stmt.forStmt.increment)
743 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
744 InstDeclPassExpression(exp);
746 if(stmt.forStmt.stmt)
747 InstDeclPassStatement(stmt.forStmt.stmt);
761 for(exp = stmt.expressions->first; exp; exp = exp.next)
762 InstDeclPassExpression(exp);
769 if(stmt.asmStmt.inputFields)
771 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
773 InstDeclPassExpression(field.expression);
775 if(stmt.asmStmt.outputFields)
777 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
779 InstDeclPassExpression(field.expression);
781 if(stmt.asmStmt.clobberedFields)
783 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
785 InstDeclPassExpression(field.expression);
792 public void ProcessInstanceDeclarations()
795 curContext = globalContext;
796 for(external = ast->first; external; external = external.next)
798 curExternal = external;
799 if(external.type == functionExternal)
801 FunctionDefinition func = external.function;
805 for(spec = func.specifiers->first; spec; spec = spec.next)
808 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
809 ReplaceByInstancePtr(spec, &func.declarator, type);
810 InstDeclPassSpecifier(spec);
813 InstDeclPassDeclarator(func.declarator);
815 InstDeclPassStatement(func.body);
817 else if(external.type == declarationExternal)
819 if(external.declaration)
820 InstDeclPassDeclaration(external.declaration);