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, "uint") ||
125 !strcmp(_class.fullName, "byte")))
128 if(_class.dataTypeString)
130 if(!strcmp(_class.dataTypeString, "uint64") ||
131 !strcmp(_class.dataTypeString, "uint32") ||
132 !strcmp(_class.dataTypeString, "uint16") ||
133 !strcmp(_class.dataTypeString, "uintptr") ||
134 !strcmp(_class.dataTypeString, "intptr") ||
135 !strcmp(_class.dataTypeString, "uint") ||
136 !strcmp(_class.dataTypeString, "byte"))
139 _class.dataType = ProcessTypeString(_class.dataTypeString, false);
140 if(_class.dataType && _class.dataType.kind == classType)
141 classSym = _class.dataType._class;
143 classSym = FindClass(_class.dataTypeString);
144 _class = classSym ? classSym.registered : null;
147 if(!strcmp(_class.dataTypeString, "char *"))
148 spec.name = CopyString("char");
150 spec.name = CopyString(_class.dataTypeString);
155 spec.name = CopyString(null);
159 else if(!_class.base)
162 spec.type = baseSpecifier;
163 spec.specifier = VOID;
170 spec.type = structSpecifier;
171 spec.id = MkIdentifier("__ecereNameSpace__ecere__com__Instance");
173 spec.baseSpecs = null;
174 spec.definitions = null;
176 spec.addNameSpace = false;
179 if(_class && _class.dataTypeString && !strcmp(_class.dataTypeString, "char *"))
180 return true; //false;
181 if(!_class || _class.type == normalClass || _class.type == noHeadClass || !strcmp(_class.fullName, "ecere::com::Instance"))
183 else if(param && _class.type == structClass)
187 else if(spec.type == baseSpecifier)
189 if(spec.specifier == ANY_OBJECT)
191 spec.specifier = VOID;
198 static void ReplaceByInstancePtr(Specifier spec, Declarator * declPtr, int type)
200 Declarator decl = *declPtr;
201 if(decl && decl.type == pointerDeclarator)
203 // Pointers to simple classes shouldn't be added pointers
207 decl.pointer.pointer = MkPointer(null, decl.pointer.pointer);
211 Declarator newDecl { };
215 decl.declarator = newDecl;
219 decl.type = pointerDeclarator;
220 decl.pointer.pointer = MkPointer(null, null);
225 static void InstDeclPassSpecifier(Specifier spec)
230 if(spec.specifier == TYPED_OBJECT)
232 spec.type = extendedSpecifier;
233 spec.extDecl = MkExtDeclString(CopyString("struct __ecereNameSpace__ecere__com__Class * class, void *"));
243 for(e = spec.list->first; e; e = e.next)
250 case structSpecifier:
256 for(def = spec.definitions->first; def; def = def.next)
258 InstDeclPassDeclaration(def.decl);
261 InstDeclPassIdentifier(spec.id);
264 case extendedSpecifier:
265 if(spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s && !strcmp(spec.extDecl.s, "dllexport"))
268 delete spec.extDecl.s;
269 for(prevSpec = spec.prev; prevSpec; prevSpec = prevSpec.prev)
270 if(prevSpec.type == baseSpecifier && prevSpec.specifier == EXTERN)
274 if(targetPlatform == win32)
275 spec.extDecl.s = CopyString("__declspec(dllexport)");
277 spec.extDecl.s = CopyString("__attribute__ ((visibility(\"default\")))");
281 if(targetPlatform == win32)
282 spec.extDecl.s = CopyString("extern __declspec(dllexport)");
284 spec.extDecl.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
291 static void InstDeclPassDeclarator(Declarator decl)
295 case structDeclarator:
297 InstDeclPassDeclarator(decl.declarator);
299 case identifierDeclarator:
302 InstDeclPassIdentifier(decl.identifier);
305 case bracketsDeclarator:
307 InstDeclPassDeclarator(decl.declarator);
309 case arrayDeclarator:
311 InstDeclPassDeclarator(decl.declarator);
313 case functionDeclarator:
316 InstDeclPassDeclarator(decl.declarator);
317 if(decl.function.parameters)
321 InstDeclPassDeclarator(decl.declarator);
322 for(type = decl.function.parameters->first; type; type = type.next)
324 bool typedObject = false;
325 Specifier spec = null;
328 spec = (Specifier)type.qualifiers->first;
329 if(spec && spec.type == nameSpecifier && !strcmp(spec.name, "class"))
333 InstDeclPassTypeName(type, true);
338 qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
339 declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
341 decl.function.parameters->Insert(spec.prev, _class);
347 case pointerDeclarator:
348 case extendedDeclarator:
349 case extendedDeclaratorEnd:
350 if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended)
352 if(decl.extended.extended.type == extDeclString && decl.extended.extended.s && !strcmp(decl.extended.extended.s, "dllexport"))
354 delete decl.extended.extended.s;
355 if(targetPlatform == win32)
356 decl.extended.extended.s = CopyString("extern __declspec(dllexport)");
358 decl.extended.extended.s = CopyString("extern __attribute__ ((visibility(\"default\")))");
360 else if(decl.extended.extended.type == extDeclString && decl.extended.extended.s &&
361 (!strcmp(decl.extended.extended.s, "stdcall") || !strcmp(decl.extended.extended.s, "_stdcall") ||
362 !strcmp(decl.extended.extended.s, "__stdcall") || !strcmp(decl.extended.extended.s, "__stdcall__")))
364 delete decl.extended.extended.s;
365 if(targetPlatform == win32)
366 decl.extended.extended.s = CopyString("__attribute__((__stdcall__))");
368 decl.extended.extended.s = CopyString("");
372 InstDeclPassDeclarator(decl.declarator);
377 /*static */void InstDeclPassTypeName(TypeName type, bool param)
382 for(spec = type.qualifiers->first; spec; spec = spec.next)
385 if((result = ReplaceClassSpec(type.qualifiers, spec, param)))
386 ReplaceByInstancePtr(spec, &type.declarator, result);
389 Symbol classSym = (spec.type == nameSpecifier) ? spec.symbol /*FindClass(spec.name)*/ : null;
390 if(type.classObjectType && (!classSym || (classSym && classSym.registered &&
391 (classSym.registered.type == enumClass || classSym.registered.type == bitClass || classSym.registered.type == unitClass ||
392 (classSym.registered.type == systemClass && strcmp(classSym.string, "ecere::com::Instance") && strcmp(classSym.string, "ecere::com::Class"))))))
393 ReplaceByInstancePtr(spec, &type.declarator, 2);
395 InstDeclPassSpecifier(spec);
399 InstDeclPassDeclarator(type.declarator);
402 static void InstDeclPassIdentifier(Identifier id)
404 if(strchr(id.string, ':'))
410 strcpy(newID, "__ecereNameSpace__");
413 for(c = 0; (ch = id.string[c]); c++)
415 if(ch == ':') ch = '_';
421 id.string = CopyString(newID);
425 static void InstDeclPassExpression(Expression exp)
432 InstDeclPassIdentifier(exp.identifier);
441 InstDeclPassExpression(exp.op.exp1);
443 InstDeclPassExpression(exp.op.exp2);
445 case extensionExpressionExp:
449 for(e = exp.list->first; e; e = e.next)
450 InstDeclPassExpression(e);
456 InstDeclPassExpression(exp.index.exp);
457 for(e = exp.index.index->first; e; e = e.next)
458 InstDeclPassExpression(e);
464 InstDeclPassExpression(exp.call.exp);
465 if(exp.call.arguments)
467 for(e = exp.call.arguments->first; e; e = e.next)
468 InstDeclPassExpression(e);
474 InstDeclPassExpression(exp.member.exp);
479 InstDeclPassExpression(exp.member.exp);
483 InstDeclPassTypeName(exp.typeName, false);
487 Type type = exp.expType;
488 // Remove casts to simple structs... (Watch out for pointers later...)
489 if(type && type.kind == classType && type._class.registered && type._class.registered.type == structClass)
491 Expression castExp = exp.cast.exp;
492 Expression prev = exp.prev, next = exp.next;
494 FreeExpContents(exp);
495 FreeType(exp.expType);
496 FreeType(exp.destType);
501 InstDeclPassExpression(exp);
505 InstDeclPassTypeName(exp.cast.typeName, false);
507 InstDeclPassExpression(exp.cast.exp);
514 InstDeclPassExpression(exp.cond.cond);
515 for(e = exp.cond.exp->first; e; e = e.next)
516 InstDeclPassExpression(e);
517 InstDeclPassExpression(exp.cond.elseExp);
520 case extensionCompoundExp:
522 InstDeclPassStatement(exp.compound);
527 InstDeclPassExpression(exp.vaArg.exp);
530 case extensionInitializerExp:
532 InstDeclPassTypeName(exp.initializer.typeName, false);
533 InstDeclPassInitializer(exp.initializer.initializer);
539 static void InstDeclPassInitializer(Initializer init)
544 InstDeclPassExpression(init.exp);
546 case listInitializer:
549 for(i = init.list->first; i; i = i.next)
550 InstDeclPassInitializer(i);
556 static void InstDeclPassDeclaration(Declaration decl)
560 case initDeclaration:
565 for(spec = decl.specifiers->first; spec; spec = spec.next)
568 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
573 for(d = decl.declarators->first; d; d = d.next)
574 ReplaceByInstancePtr(spec, &d.declarator, type);
577 InstDeclPassSpecifier(spec);
583 for(d = decl.declarators->first; d; d = d.next)
585 InstDeclPassDeclarator(d.declarator);
587 InstDeclPassInitializer(d.initializer);
592 case structDeclaration:
597 for(spec = decl.specifiers->first; spec; spec = spec.next)
600 if((type = ReplaceClassSpec(decl.specifiers, spec, false)))
605 for(d = decl.declarators->first; d; d = d.next)
606 ReplaceByInstancePtr(spec, &d, type);
609 InstDeclPassSpecifier(spec);
616 for(d = decl.declarators->first; d; d = d.next)
617 InstDeclPassDeclarator(d);
621 // This should be removed by now?
622 case instDeclaration:
623 // InstDeclPassInstantiation(decl.inst);
628 static void InstDeclPassStatement(Statement stmt)
633 InstDeclPassStatement(stmt.labeled.stmt);
636 // This expression should be constant...
637 if(stmt.caseStmt.exp)
638 InstDeclPassExpression(stmt.caseStmt.exp);
639 if(stmt.caseStmt.stmt)
640 InstDeclPassStatement(stmt.caseStmt.stmt);
646 Context prevContext = curContext;
648 if(!stmt.compound.isSwitch)
649 curContext = stmt.compound.context;
651 if(stmt.compound.declarations)
653 for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
654 InstDeclPassDeclaration(decl);
656 if(stmt.compound.statements)
658 for(s = stmt.compound.statements->first; s; s = s.next)
659 InstDeclPassStatement(s);
661 curContext = prevContext;
669 for(exp = stmt.expressions->first; exp; exp = exp.next)
670 InstDeclPassExpression(exp);
679 for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
680 InstDeclPassExpression(exp);
683 InstDeclPassStatement(stmt.ifStmt.stmt);
684 if(stmt.ifStmt.elseStmt)
685 InstDeclPassStatement(stmt.ifStmt.elseStmt);
691 for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
692 InstDeclPassExpression(exp);
693 InstDeclPassStatement(stmt.switchStmt.stmt);
699 for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
700 InstDeclPassExpression(exp);
701 InstDeclPassStatement(stmt.whileStmt.stmt);
709 for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
710 InstDeclPassExpression(exp);
712 if(stmt.doWhile.stmt)
713 InstDeclPassStatement(stmt.doWhile.stmt);
719 if(stmt.forStmt.init)
720 InstDeclPassStatement(stmt.forStmt.init);
721 if(stmt.forStmt.check)
722 InstDeclPassStatement(stmt.forStmt.check);
723 if(stmt.forStmt.increment)
725 for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
726 InstDeclPassExpression(exp);
728 if(stmt.forStmt.stmt)
729 InstDeclPassStatement(stmt.forStmt.stmt);
743 for(exp = stmt.expressions->first; exp; exp = exp.next)
744 InstDeclPassExpression(exp);
751 if(stmt.asmStmt.inputFields)
753 for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
755 InstDeclPassExpression(field.expression);
757 if(stmt.asmStmt.outputFields)
759 for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
761 InstDeclPassExpression(field.expression);
763 if(stmt.asmStmt.clobberedFields)
765 for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
767 InstDeclPassExpression(field.expression);
774 public void ProcessInstanceDeclarations()
777 curContext = globalContext;
778 for(external = ast->first; external; external = external.next)
780 curExternal = external;
781 if(external.type == functionExternal)
783 FunctionDefinition func = external.function;
787 for(spec = func.specifiers->first; spec; spec = spec.next)
790 if((type = ReplaceClassSpec(func.specifiers, spec, false)))
791 ReplaceByInstancePtr(spec, &func.declarator, type);
792 InstDeclPassSpecifier(spec);
795 InstDeclPassDeclarator(func.declarator);
797 InstDeclPassStatement(func.body);
799 else if(external.type == declarationExternal)
801 if(external.declaration)
802 InstDeclPassDeclaration(external.declaration);