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 !strcmp(_class.fullName, "ecere::com::Instance") ||
117 (_class.dataTypeString && !strcmp(_class.dataTypeString, "char *")) ||
118 //strcmp(_class.fullName, "bool") &&
119 !strcmp(_class.fullName, "uint64") ||
120 !strcmp(_class.fullName, "uint32") ||
121 !strcmp(_class.fullName, "uint16") ||
122 !strcmp(_class.fullName, "uintptr") ||
123 !strcmp(_class.fullName, "intptr") ||
124 !strcmp(_class.fullName, "uintsize") ||
125 !strcmp(_class.fullName, "intsize") ||
126 !strcmp(_class.fullName, "uint") ||
127 !strcmp(_class.fullName, "byte")))
130 if(_class.dataTypeString)
132 if(!strcmp(_class.dataTypeString, "uint64") ||
133 !strcmp(_class.dataTypeString, "uint32") ||
134 !strcmp(_class.dataTypeString, "uint16") ||
135 !strcmp(_class.dataTypeString, "uintptr") ||
136 !strcmp(_class.dataTypeString, "intptr") ||
137 !strcmp(_class.dataTypeString, "uintsize") ||
138 !strcmp(_class.dataTypeString, "intsize") ||
139 !strcmp(_class.dataTypeString, "uint") ||
140 !strcmp(_class.dataTypeString, "byte"))
143 _class.dataType = ProcessTypeString(_class.dataTypeString, false);
144 if(_class.dataType && _class.dataType.kind == classType)
145 classSym = _class.dataType._class;
147 classSym = FindClass(_class.dataTypeString);
148 _class = classSym ? classSym.registered : null;
151 if(!strcmp(_class.dataTypeString, "char *"))
152 spec.name = CopyString("char");
154 spec.name = CopyString(_class.dataTypeString);
159 spec.name = CopyString(null);
163 else if(!_class.base)
166 spec.type = baseSpecifier;
167 spec.specifier = VOID;
174 spec.type = structSpecifier;
175 spec.id = MkIdentifier("__ecereNameSpace__ecere__com__Instance");
177 spec.baseSpecs = null;
178 spec.definitions = null;
180 spec.addNameSpace = false;
183 if(_class && _class.dataTypeString && !strcmp(_class.dataTypeString, "char *"))
184 return true; //false;
185 if(!_class || _class.type == normalClass || _class.type == noHeadClass || !strcmp(_class.fullName, "ecere::com::Instance"))
187 else if(param && _class.type == structClass)
191 else if(spec.type == baseSpecifier)
193 if(spec.specifier == ANY_OBJECT)
195 spec.specifier = VOID;
202 static void ReplaceByInstancePtr(Specifier spec, Declarator * declPtr, int type)
204 Declarator decl = *declPtr;
205 if(decl && decl.type == pointerDeclarator)
207 // Pointers to simple classes shouldn't be added pointers
211 decl.pointer.pointer = MkPointer(null, decl.pointer.pointer);
215 Declarator newDecl { };
219 decl.declarator = newDecl;
223 decl.type = pointerDeclarator;
224 decl.pointer.pointer = MkPointer(null, null);
229 static void InstDeclPassSpecifier(Specifier spec)
234 if(spec.specifier == TYPED_OBJECT)
236 spec.type = extendedSpecifier;
237 spec.extDecl = MkExtDeclString(CopyString("struct __ecereNameSpace__ecere__com__Class * class, void *"));
247 for(e = spec.list->first; e; e = e.next)
254 case structSpecifier:
260 for(def = spec.definitions->first; def; def = def.next)
262 InstDeclPassDeclaration(def.decl);
265 InstDeclPassIdentifier(spec.id);
268 case extendedSpecifier:
269 if(spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s)
271 if(!strcmp(spec.extDecl.s, "dllexport"))
274 delete spec.extDecl.s;
275 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
276 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
280 if(targetPlatform == win32)
281 spec.extDecl.s = CopyString("__declspec(dllexport)");
283 spec.extDecl.s = CopyString("__attribute__ ((visibility(\"default\")))");
287 if(targetPlatform == win32)
288 spec.extDecl.s = CopyString("extern __declspec(dllexport)");
290 spec.extDecl.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
293 else if(!strcmp(spec.extDecl.s, "stdcall") || !strcmp(spec.extDecl.s, "_stdcall") ||
294 !strcmp(spec.extDecl.s, "__stdcall") || !strcmp(spec.extDecl.s, "__stdcall__"))
296 delete spec.extDecl.s;
297 if(targetPlatform == win32)
298 spec.extDecl.s = CopyString("__attribute__((__stdcall__))");
300 spec.extDecl.s = CopyString("");
307 static void InstDeclPassDeclarator(Declarator decl)
311 case structDeclarator:
313 InstDeclPassDeclarator(decl.declarator);
315 case identifierDeclarator:
318 InstDeclPassIdentifier(decl.identifier);
321 case bracketsDeclarator:
323 InstDeclPassDeclarator(decl.declarator);
325 case arrayDeclarator:
327 InstDeclPassDeclarator(decl.declarator);
329 case functionDeclarator:
332 InstDeclPassDeclarator(decl.declarator);
333 if(decl.function.parameters)
337 InstDeclPassDeclarator(decl.declarator);
338 for(type = decl.function.parameters->first; type; type = type.next)
340 bool typedObject = false;
341 Specifier spec = null;
344 spec = (Specifier)type.qualifiers->first;
345 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
349 InstDeclPassTypeName(type, true);
354 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
355 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
357 decl.function.parameters->Insert(spec.prev, _class);
363 case pointerDeclarator:
364 case extendedDeclarator:
365 case extendedDeclaratorEnd:
366 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
368 if(decl.extended.extended.type == extDeclString && decl.extended.extended.s && !strcmp(decl.extended.extended.s, "dllexport"))
370 delete decl.extended.extended.s;
371 if(targetPlatform == win32)
372 decl.extended.extended.s = CopyString("extern __declspec(dllexport)");
374 decl.extended.extended.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
376 else if(decl.extended.extended.type == extDeclString && decl.extended.extended.s &&
377 (!strcmp(decl.extended.extended.s, "stdcall") || !strcmp(decl.extended.extended.s, "_stdcall") ||
378 !strcmp(decl.extended.extended.s, "__stdcall") || !strcmp(decl.extended.extended.s, "__stdcall__")))
380 delete decl.extended.extended.s;
381 if(targetPlatform == win32)
382 decl.extended.extended.s = CopyString("__attribute__((__stdcall__))");
384 decl.extended.extended.s = CopyString("");
388 InstDeclPassDeclarator(decl.declarator);
393 /*static */void InstDeclPassTypeName(TypeName type, bool param)
398 for(spec = type.qualifiers->first; spec; spec = spec.next)
401 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
402 ReplaceByInstancePtr(spec, &type.declarator, result);
405 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
406 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
407 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass ||
408 (classSym.registered.type == systemClass && strcmp(classSym.string, "ecere::com::Instance") && strcmp(classSym.string, "ecere::com::Class"))))))
409 ReplaceByInstancePtr(spec, &type.declarator, 2);
411 InstDeclPassSpecifier(spec);
415 InstDeclPassDeclarator(type.declarator);
418 static void InstDeclPassIdentifier(Identifier id)
420 if(strchr(id.string, ':'))
426 strcpy(newID, "__ecereNameSpace__");
429 for(c = 0; (ch = id.string[c]); c++)
431 if(ch == ':') ch = '_';
437 id.string = CopyString(newID);
441 static void InstDeclPassExpression(Expression exp)
448 InstDeclPassIdentifier(exp.identifier);
457 InstDeclPassExpression(exp.op.exp1);
459 InstDeclPassExpression(exp.op.exp2);
461 case extensionExpressionExp:
465 for(e = exp.list->first; e; e = e.next)
466 InstDeclPassExpression(e);
472 InstDeclPassExpression(exp.index.exp);
473 for(e = exp.index.index->first; e; e = e.next)
474 InstDeclPassExpression(e);
480 InstDeclPassExpression(exp.call.exp);
481 if(exp.call.arguments)
483 for(e = exp.call.arguments->first; e; e = e.next)
484 InstDeclPassExpression(e);
490 InstDeclPassExpression(exp.member.exp);
495 InstDeclPassExpression(exp.member.exp);
499 InstDeclPassTypeName(exp.typeName, false);
503 Type type = exp.expType;
504 // Remove casts to simple structs... (Watch out for pointers later...)
505 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass)
507 Expression castExp = exp.cast.exp;
508 Expression prev = exp.prev, next = exp.next;
510 FreeExpContents(exp);
511 FreeType(exp.expType);
512 FreeType(exp.destType);
517 InstDeclPassExpression(exp);
521 InstDeclPassTypeName(exp.cast.typeName, false);
523 InstDeclPassExpression(exp.cast.exp);
530 InstDeclPassExpression(exp.cond.cond);
531 for(e = exp.cond.exp->first; e; e = e.next)
532 InstDeclPassExpression(e);
533 InstDeclPassExpression(exp.cond.elseExp);
536 case extensionCompoundExp:
538 InstDeclPassStatement(exp.compound);
543 InstDeclPassExpression(exp.vaArg.exp);
546 case extensionInitializerExp:
548 InstDeclPassTypeName(exp.initializer.typeName, false);
549 InstDeclPassInitializer(exp.initializer.initializer);
555 static void InstDeclPassInitializer(Initializer init)
560 InstDeclPassExpression(init.exp);
562 case listInitializer:
565 for(i = init.list->first; i; i = i.next)
566 InstDeclPassInitializer(i);
572 static void InstDeclPassDeclaration(Declaration decl)
576 case initDeclaration:
581 for(spec = decl.specifiers->first; spec; spec = spec.next)
584 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
589 for(d = decl.declarators->first; d; d = d.next)
590 ReplaceByInstancePtr(spec, &d.declarator, type);
593 InstDeclPassSpecifier(spec);
599 for(d = decl.declarators->first; d; d = d.next)
601 InstDeclPassDeclarator(d.declarator);
603 InstDeclPassInitializer(d.initializer);
608 case structDeclaration:
613 for(spec = decl.specifiers->first; spec; spec = spec.next)
616 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
621 for(d = decl.declarators->first; d; d = d.next)
622 ReplaceByInstancePtr(spec, &d, type);
625 InstDeclPassSpecifier(spec);
632 for(d = decl.declarators->first; d; d = d.next)
633 InstDeclPassDeclarator(d);
637 // This should be removed by now?
638 case instDeclaration:
639 // InstDeclPassInstantiation(decl.inst);
644 static void InstDeclPassStatement(Statement stmt)
649 InstDeclPassStatement(stmt.labeled.stmt);
652 // This expression should be constant...
653 if(stmt.caseStmt.exp)
654 InstDeclPassExpression(stmt.caseStmt.exp);
655 if(stmt.caseStmt.stmt)
656 InstDeclPassStatement(stmt.caseStmt.stmt);
662 Context prevContext = curContext;
664 if(!stmt.compound.isSwitch)
665 curContext = stmt.compound.context;
667 if(stmt.compound.declarations)
669 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
670 InstDeclPassDeclaration(decl);
672 if(stmt.compound.statements)
674 for(s = stmt.compound.statements->first; s; s = s.next)
675 InstDeclPassStatement(s);
677 curContext = prevContext;
685 for(exp = stmt.expressions->first; exp; exp = exp.next)
686 InstDeclPassExpression(exp);
695 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
696 InstDeclPassExpression(exp);
699 InstDeclPassStatement(stmt.ifStmt.stmt);
700 if(stmt.ifStmt.elseStmt)
701 InstDeclPassStatement(stmt.ifStmt.elseStmt);
707 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
708 InstDeclPassExpression(exp);
709 InstDeclPassStatement(stmt.switchStmt.stmt);
715 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
716 InstDeclPassExpression(exp);
717 InstDeclPassStatement(stmt.whileStmt.stmt);
725 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
726 InstDeclPassExpression(exp);
728 if(stmt.doWhile.stmt)
729 InstDeclPassStatement(stmt.doWhile.stmt);
735 if(stmt.forStmt.init)
736 InstDeclPassStatement(stmt.forStmt.init);
737 if(stmt.forStmt.check)
738 InstDeclPassStatement(stmt.forStmt.check);
739 if(stmt.forStmt.increment)
741 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
742 InstDeclPassExpression(exp);
744 if(stmt.forStmt.stmt)
745 InstDeclPassStatement(stmt.forStmt.stmt);
759 for(exp = stmt.expressions->first; exp; exp = exp.next)
760 InstDeclPassExpression(exp);
767 if(stmt.asmStmt.inputFields)
769 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
771 InstDeclPassExpression(field.expression);
773 if(stmt.asmStmt.outputFields)
775 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
777 InstDeclPassExpression(field.expression);
779 if(stmt.asmStmt.clobberedFields)
781 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
783 InstDeclPassExpression(field.expression);
790 public void ProcessInstanceDeclarations()
793 curContext = globalContext;
794 for(external = ast->first; external; external = external.next)
796 curExternal = external;
797 if(external.type == functionExternal)
799 FunctionDefinition func = external.function;
803 for(spec = func.specifiers->first; spec; spec = spec.next)
806 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
807 ReplaceByInstancePtr(spec, &func.declarator, type);
808 InstDeclPassSpecifier(spec);
811 InstDeclPassDeclarator(func.declarator);
813 InstDeclPassStatement(func.body);
815 else if(external.type == declarationExternal)
817 if(external.declaration)
818 InstDeclPassDeclaration(external.declaration);