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 /*Class*/ || spec.type == subClassSpecifier)
65 Symbol classSym = spec.symbol; // FindClass(spec.name);
66 if(spec.type == subClassSpecifier)
68 classSym = FindClass("ecere::com::Class");
73 Class _class = classSym.registered;
74 spec.type = nameSpecifier;
78 FreeList(spec.templateArgs, FreeTemplateArgument);
79 spec.templateArgs = null;
82 if(_class && _class.type == structClass)
87 FullClassNameCat(name, _class.fullName, false);
88 //spec.name = CopyString(name);
90 spec.type = structSpecifier;
91 spec.id = MkIdentifier(name);
93 spec.definitions = null;
96 external = DeclareStruct(_class.fullName, false);
97 // If this declaration created something, we gotta pass it through
100 Context ctx = curContext;
101 curContext = globalContext;
102 InstDeclPassDeclaration(external.declaration);
107 else if(_class && _class.type == noHeadClass)
111 spec.name = CopyString("void");
114 char name[1024] = "";
115 FullClassNameCat(name, _class.fullName, false);
117 spec.type = structSpecifier;
118 spec.id = MkIdentifier(name);
120 spec.definitions = null;
124 if((_class.type != systemClass ||
125 !strcmp(_class.fullName, "enum") ||
126 !strcmp(_class.fullName, "ecere::com::Instance") ||
127 (_class.dataTypeString && !strcmp(_class.dataTypeString, "char *")) ||
128 //strcmp(_class.fullName, "bool") &&
129 !strcmp(_class.fullName, "uint64") ||
130 !strcmp(_class.fullName, "uint32") ||
131 !strcmp(_class.fullName, "uint16") ||
132 !strcmp(_class.fullName, "uint") ||
133 !strcmp(_class.fullName, "byte")))
136 if(_class.dataTypeString)
138 if(!strcmp(_class.dataTypeString, "uint64") ||
139 !strcmp(_class.dataTypeString, "uint32") ||
140 !strcmp(_class.dataTypeString, "uint16") ||
141 !strcmp(_class.dataTypeString, "uint") ||
142 !strcmp(_class.dataTypeString, "byte"))
145 _class.dataType = ProcessTypeString(_class.dataTypeString, false);
146 if(_class.dataType && _class.dataType.kind == classType)
147 classSym = _class.dataType._class;
149 classSym = FindClass(_class.dataTypeString);
150 _class = classSym ? classSym.registered : null;
153 if(!strcmp(_class.dataTypeString, "char *"))
154 spec.name = CopyString("char");
156 spec.name = CopyString(_class.dataTypeString);
161 spec.name = CopyString(null);
165 else if(!_class.base)
168 spec.type = baseSpecifier;
169 spec.specifier = VOID;
176 spec.type = structSpecifier;
177 spec.id = MkIdentifier("__ecereNameSpace__ecere__com__Instance");
179 spec.definitions = null;
182 if(_class && _class.dataTypeString && !strcmp(_class.dataTypeString /*fullName*/, "char *"))
183 return true; //false;
184 if(!_class || _class.type == normalClass || _class.type == noHeadClass || !strcmp(_class.fullName, "ecere::com::Instance"))
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.name = 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(!strcmp(spec.name, "dllexport"))
272 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
273 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
277 if(targetPlatform == win32)
278 spec.name = CopyString("__declspec(dllexport)");
280 spec.name = CopyString("__attribute__ ((visibility(\"default\")))");
284 if(targetPlatform == win32)
285 spec.name = CopyString("extern __declspec(dllexport)");
287 spec.name = CopyString("extern __attribute__ ((visibility(\"default\")))");
292 /*case classSpecifier:
297 static void InstDeclPassDeclarator(Declarator decl)
301 case structDeclarator:
303 InstDeclPassDeclarator(decl.declarator);
305 case identifierDeclarator:
308 InstDeclPassIdentifier(decl.identifier);
311 case bracketsDeclarator:
313 InstDeclPassDeclarator(decl.declarator);
315 case arrayDeclarator:
317 InstDeclPassDeclarator(decl.declarator);
319 case functionDeclarator:
322 InstDeclPassDeclarator(decl.declarator);
323 if(decl.function.parameters)
327 InstDeclPassDeclarator(decl.declarator);
328 for(type = decl.function.parameters->first; type; type = type.next)
330 bool typedObject = false;
331 Specifier spec = null;
334 spec = (Specifier)type.qualifiers->first;
335 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
339 InstDeclPassTypeName(type, true);
344 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
345 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
347 decl.function.parameters->Insert(spec.prev, _class);
353 case pointerDeclarator:
354 case extendedDeclarator:
355 case extendedDeclaratorEnd:
356 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
358 if(!strcmp(decl.extended.extended, "dllexport"))
360 delete decl.extended.extended;
361 if(targetPlatform == win32)
362 decl.extended.extended = CopyString("extern __declspec(dllexport)");
364 decl.extended.extended = CopyString("extern __attribute__ ((visibility(\"default\")))");
366 else if(!strcmp(decl.extended.extended, "stdcall") || !strcmp(decl.extended.extended, "_stdcall") ||
367 !strcmp(decl.extended.extended, "__stdcall") || !strcmp(decl.extended.extended, "__stdcall__"))
369 delete decl.extended.extended;
370 if(targetPlatform == win32)
371 decl.extended.extended = CopyString("__attribute__((__stdcall__))");
373 decl.extended.extended = CopyString("");
377 InstDeclPassDeclarator(decl.declarator);
382 /*static */void InstDeclPassTypeName(TypeName type, bool param)
387 for(spec = type.qualifiers->first; spec; spec = spec.next)
390 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
391 ReplaceByInstancePtr(spec, &type.declarator, result);
394 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
395 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
396 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass ||
397 (classSym.registered.type == systemClass && strcmp(classSym.string, "ecere::com::Instance") && strcmp(classSym.string, "ecere::com::Class"))))))
398 ReplaceByInstancePtr(spec, &type.declarator, 2);
400 InstDeclPassSpecifier(spec);
404 InstDeclPassDeclarator(type.declarator);
407 static void InstDeclPassIdentifier(Identifier id)
409 if(strchr(id.string, ':'))
415 strcpy(newID, "__ecereNameSpace__");
418 for(c = 0; (ch = id.string[c]); c++)
420 if(ch == ':') ch = '_';
426 id.string = CopyString(newID);
430 static void InstDeclPassExpression(Expression exp)
437 InstDeclPassIdentifier(exp.identifier);
446 InstDeclPassExpression(exp.op.exp1);
448 InstDeclPassExpression(exp.op.exp2);
450 case extensionExpressionExp:
454 for(e = exp.list->first; e; e = e.next)
455 InstDeclPassExpression(e);
461 InstDeclPassExpression(exp.index.exp);
462 for(e = exp.index.index->first; e; e = e.next)
463 InstDeclPassExpression(e);
469 InstDeclPassExpression(exp.call.exp);
470 if(exp.call.arguments)
472 for(e = exp.call.arguments->first; e; e = e.next)
473 InstDeclPassExpression(e);
479 InstDeclPassExpression(exp.member.exp);
484 InstDeclPassExpression(exp.member.exp);
488 InstDeclPassTypeName(exp.typeName, false);
492 Type type = exp.expType;
493 // Remove casts to simple structs... (Watch out for pointers later...)
494 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass)
496 Expression castExp = exp.cast.exp;
497 Expression prev = exp.prev, next = exp.next;
499 FreeExpContents(exp);
500 FreeType(exp.expType);
501 FreeType(exp.destType);
506 InstDeclPassExpression(exp);
510 InstDeclPassTypeName(exp.cast.typeName, false);
512 InstDeclPassExpression(exp.cast.exp);
519 InstDeclPassExpression(exp.cond.cond);
520 for(e = exp.cond.exp->first; e; e = e.next)
521 InstDeclPassExpression(e);
522 InstDeclPassExpression(exp.cond.elseExp);
525 case extensionCompoundExp:
527 InstDeclPassStatement(exp.compound);
532 InstDeclPassExpression(exp.vaArg.exp);
535 case extensionInitializerExp:
537 InstDeclPassTypeName(exp.initializer.typeName, false);
538 InstDeclPassInitializer(exp.initializer.initializer);
544 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)
638 InstDeclPassStatement(stmt.labeled.stmt);
641 // This expression should be constant...
642 if(stmt.caseStmt.exp)
643 InstDeclPassExpression(stmt.caseStmt.exp);
644 if(stmt.caseStmt.stmt)
645 InstDeclPassStatement(stmt.caseStmt.stmt);
651 Context prevContext = curContext;
653 if(!stmt.compound.isSwitch)
654 curContext = stmt.compound.context;
656 if(stmt.compound.declarations)
658 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
659 InstDeclPassDeclaration(decl);
661 if(stmt.compound.statements)
663 for(s = stmt.compound.statements->first; s; s = s.next)
664 InstDeclPassStatement(s);
666 curContext = prevContext;
674 for(exp = stmt.expressions->first; exp; exp = exp.next)
675 InstDeclPassExpression(exp);
684 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
685 InstDeclPassExpression(exp);
688 InstDeclPassStatement(stmt.ifStmt.stmt);
689 if(stmt.ifStmt.elseStmt)
690 InstDeclPassStatement(stmt.ifStmt.elseStmt);
696 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
697 InstDeclPassExpression(exp);
698 InstDeclPassStatement(stmt.switchStmt.stmt);
704 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
705 InstDeclPassExpression(exp);
706 InstDeclPassStatement(stmt.whileStmt.stmt);
714 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
715 InstDeclPassExpression(exp);
717 if(stmt.doWhile.stmt)
718 InstDeclPassStatement(stmt.doWhile.stmt);
724 if(stmt.forStmt.init)
725 InstDeclPassStatement(stmt.forStmt.init);
726 if(stmt.forStmt.check)
727 InstDeclPassStatement(stmt.forStmt.check);
728 if(stmt.forStmt.increment)
730 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
731 InstDeclPassExpression(exp);
733 if(stmt.forStmt.stmt)
734 InstDeclPassStatement(stmt.forStmt.stmt);
748 for(exp = stmt.expressions->first; exp; exp = exp.next)
749 InstDeclPassExpression(exp);
756 if(stmt.asmStmt.inputFields)
758 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
760 InstDeclPassExpression(field.expression);
762 if(stmt.asmStmt.outputFields)
764 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
766 InstDeclPassExpression(field.expression);
768 if(stmt.asmStmt.clobberedFields)
770 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
772 InstDeclPassExpression(field.expression);
779 public void ProcessInstanceDeclarations()
782 curContext = globalContext;
783 for(external = ast->first; external; external = external.next)
785 curExternal = external;
786 if(external.type == functionExternal)
788 FunctionDefinition func = external.function;
792 for(spec = func.specifiers->first; spec; spec = spec.next)
795 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
796 ReplaceByInstancePtr(spec, &func.declarator, type);
797 InstDeclPassSpecifier(spec);
800 InstDeclPassDeclarator(func.declarator);
802 InstDeclPassStatement(func.body);
804 else if(external.type == declarationExternal)
806 if(external.declaration)
807 InstDeclPassDeclaration(external.declaration);