From 395b6823cc137a5037c8aed74a0d41c2239327ba Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Tue, 19 Dec 2023 14:04:21 +0100 Subject: [PATCH] testing and fixing with real code example from @linus --- src/lang/cpp/AST.rsc | 59 +++++++++++++- src/lang/cpp/TypeSymbol.rsc | 2 +- src/lang/cpp/internal/AST.java | 86 ++++++++++----------- src/lang/cpp/internal/BindingsResolver.java | 2 +- src/lang/cpp/internal/Parser.java | 31 -------- 5 files changed, 103 insertions(+), 77 deletions(-) diff --git a/src/lang/cpp/AST.rsc b/src/lang/cpp/AST.rsc index b7933eff..e8a26f8c 100644 --- a/src/lang/cpp/AST.rsc +++ b/src/lang/cpp/AST.rsc @@ -552,4 +552,61 @@ java list[loc] parseForComments(loc file, str charset=DEFAULT_CHARSET, bool infe java rel[loc,loc] parseForMacros(loc file, str charset=DEFAULT_CHARSET, bool inferCharset=!(charset?), list[loc] includePaths = classPaths["vs12"], map[str,str] standardMacros=provideStandardMacros(), map[str,str] additionalMacros = ()); @synopsis{All functions in this module that have a charset parameter use this as default.} -public str DEFAULT_CHARSET = "UTF-8"; \ No newline at end of file +public str DEFAULT_CHARSET = "UTF-8"; + +public map[str, str] linusMacros = ( + "_M_IX86": "600", + //"_WIN32": "1", + // "_MSC_VER": "1400" + "__cdecl": "", + "__fastcall": "", + "__restrict": "", + "__sptr": "", + "__stdcall": "", + + + "__unaligned": "", + "__uptr": "", + "__w64": "", + "__forceinline": "__inline", + "__int8": "char", + "__int16": "short", + "__int32": "int", + "__int64": "long long", + + // additional: + //"_MSC_VER": "1700", + "__cplusplus": "199711L", + "__thiscall": "", + "_CHAR16T": "", + "_NATIVE_WCHAR_T_DEFINED": "1", + "__nullptr": "nullptr", + //"_MSC_EXTENSIONS": "1", + "__inline": "inline", + "__ptr32": "", + "__ptr64": "", + "__interface": "struct", + + "__pragma(A)": "", + "__identifier(A)": "A", + + +// "__declspec(A)": "", + "_stdcall": "", + + "_USE_DECLSPECS_FOR_SAL": "0", + //"_DLL": "1", + + "NDEBUG": "", + //"WIN32": "", + //"_WINDOWS": "", + //"_WIN32_DCOM": "", + //"_USRDLL": "", + "SSCF1_INCLUDED": "", + "LOGGINGTRACING_INCLUDED": "", + //"_WINDLL": "", + "_UNICODE": "", + "UNICODE": "" + //"_AFXDLL": "" +// "__INTELLISENSE__": "1" +); diff --git a/src/lang/cpp/TypeSymbol.rsc b/src/lang/cpp/TypeSymbol.rsc index 6d567343..d6925ff1 100644 --- a/src/lang/cpp/TypeSymbol.rsc +++ b/src/lang/cpp/TypeSymbol.rsc @@ -75,7 +75,7 @@ data TypeSymbol | \functionTemplate(loc decl, list[loc] templateParameters) | \variableTemplate(loc decl, list[loc] templateParameters) - | \aliasTemplate(loc decl, list[TypeSymbol] templateArguments, TypeSymbol \type) + | \aliasTemplate(loc decl, list[loc] templateParameters, TypeSymbol \type) | \functionSetType(loc decl, list[TypeSymbol] templateArguments) | \functionSetTypePointer(loc decl, list[TypeSymbol] templateArguments) diff --git a/src/lang/cpp/internal/AST.java b/src/lang/cpp/internal/AST.java index 84c54534..5989d470 100644 --- a/src/lang/cpp/internal/AST.java +++ b/src/lang/cpp/internal/AST.java @@ -684,8 +684,6 @@ public AST (IValueFactory vf) { = tf.constructor(typestore,_TypeSymbol,"eClassTemplate",tf.sourceLocationType(),"decl",tf.listType(tf.sourceLocationType()),"templateParameters"); private static final Type _TypeSymbol_templateTypeParameter_2 = tf.constructor(typestore,_TypeSymbol,"templateTypeParameter",tf.sourceLocationType(),"owner",tf.sourceLocationType(),"decl"); - private static final Type _TypeSymbol_cClassTemplate_2 - = tf.constructor(typestore,_TypeSymbol,"cClassTemplate",tf.sourceLocationType(),"decl",tf.listType(tf.sourceLocationType()),"templateParameters"); private static final Type _TypeSymbol_deferredClassInstance_1 = tf.constructor(typestore,_TypeSymbol,"deferredClassInstance",tf.stringType(),"name"); private static final Type _TypeSymbol_functionType_2 @@ -770,14 +768,16 @@ public AST (IValueFactory vf) { = tf.constructor(typestore,_TypeSymbol,"aliasTemplateInstance",tf.sourceLocationType(),"instantiatedTemplate",tf.listType(_TypeSymbol),"templateArguments"); private static final Type _TypeSymbol_int_0 = tf.constructor(typestore,_TypeSymbol,"int"); + private static final Type _TypeSymbol_aliasTemplate_3 + = tf.constructor(typestore,_TypeSymbol,"aliasTemplate",tf.sourceLocationType(),"decl",tf.listType(tf.sourceLocationType()),"templateParameters",_TypeSymbol,"type"); + private static final Type _TypeSymbol_cClassTemplate_2 + = tf.constructor(typestore,_TypeSymbol,"cClassTemplate",tf.sourceLocationType(),"decl",tf.listType(tf.sourceLocationType()),"templateParameters"); private static final Type _TypeSymbol_struct_1 = tf.constructor(typestore,_TypeSymbol,"struct",tf.sourceLocationType(),"decl"); private static final Type _TypeSymbol_array_2 = tf.constructor(typestore,_TypeSymbol,"array",_TypeSymbol,"baseType",tf.integerType(),"size"); private static final Type _TypeSymbol_nullPtr_0 = tf.constructor(typestore,_TypeSymbol,"nullPtr"); - private static final Type _TypeSymbol_aliasTemplate_3 - = tf.constructor(typestore,_TypeSymbol,"aliasTemplate",tf.sourceLocationType(),"decl",tf.listType(_TypeSymbol),"templateArguments",_TypeSymbol,"type"); private static final Type _TypeSymbol_decimal128_0 = tf.constructor(typestore,_TypeSymbol,"decimal128"); private static final Type _TypeSymbol_basicType_2 @@ -6298,25 +6298,6 @@ public IConstructor TypeSymbol_templateTypeParameter(ISourceLocation $owner, ISo return vf.constructor(_TypeSymbol_templateTypeParameter_2 , $owner, $decl).asWithKeywordParameters().setParameters(kwParams); } - public IConstructor TypeSymbol_cClassTemplate(ISourceLocation $decl, IList $templateParameters) { - - if (!$decl.getType().isSubtypeOf(tf.sourceLocationType())) { - throw new IllegalArgumentException("Expected " + tf.sourceLocationType() + " but got " + $decl.getType() + " for $decl:" + $decl); - } - - if (!$templateParameters.getType().isSubtypeOf(tf.listType(tf.sourceLocationType()))) { - throw new IllegalArgumentException("Expected " + tf.listType(tf.sourceLocationType()) + " but got " + $templateParameters.getType() + " for $templateParameters:" + $templateParameters); - } - - Map kwParams = new HashMap(); - - - - - - return vf.constructor(_TypeSymbol_cClassTemplate_2 , $decl, $templateParameters).asWithKeywordParameters().setParameters(kwParams); - } - public IConstructor TypeSymbol_deferredClassInstance(String $name) { if (!vf.string($name).getType().isSubtypeOf(tf.stringType())) { @@ -6959,11 +6940,19 @@ public IConstructor TypeSymbol_int() { return vf.constructor(_TypeSymbol_int_0 ).asWithKeywordParameters().setParameters(kwParams); } - public IConstructor TypeSymbol_struct(ISourceLocation $decl) { + public IConstructor TypeSymbol_aliasTemplate(ISourceLocation $decl, IList $templateParameters, IConstructor $type) { if (!$decl.getType().isSubtypeOf(tf.sourceLocationType())) { throw new IllegalArgumentException("Expected " + tf.sourceLocationType() + " but got " + $decl.getType() + " for $decl:" + $decl); } + + if (!$templateParameters.getType().isSubtypeOf(tf.listType(tf.sourceLocationType()))) { + throw new IllegalArgumentException("Expected " + tf.listType(tf.sourceLocationType()) + " but got " + $templateParameters.getType() + " for $templateParameters:" + $templateParameters); + } + + if (!$type.getType().isSubtypeOf(_TypeSymbol)) { + throw new IllegalArgumentException("Expected " + _TypeSymbol + " but got " + $type.getType() + " for $type:" + $type); + } Map kwParams = new HashMap(); @@ -6971,17 +6960,17 @@ public IConstructor TypeSymbol_struct(ISourceLocation $decl) { - return vf.constructor(_TypeSymbol_struct_1 , $decl).asWithKeywordParameters().setParameters(kwParams); + return vf.constructor(_TypeSymbol_aliasTemplate_3 , $decl, $templateParameters, $type).asWithKeywordParameters().setParameters(kwParams); } - public IConstructor TypeSymbol_array(IConstructor $baseType, IValue $size) { + public IConstructor TypeSymbol_cClassTemplate(ISourceLocation $decl, IList $templateParameters) { - if (!$baseType.getType().isSubtypeOf(_TypeSymbol)) { - throw new IllegalArgumentException("Expected " + _TypeSymbol + " but got " + $baseType.getType() + " for $baseType:" + $baseType); + if (!$decl.getType().isSubtypeOf(tf.sourceLocationType())) { + throw new IllegalArgumentException("Expected " + tf.sourceLocationType() + " but got " + $decl.getType() + " for $decl:" + $decl); } - if (!$size.getType().isSubtypeOf(tf.integerType())) { - throw new IllegalArgumentException("Expected " + tf.integerType() + " but got " + $size.getType() + " for $size:" + $size); + if (!$templateParameters.getType().isSubtypeOf(tf.listType(tf.sourceLocationType()))) { + throw new IllegalArgumentException("Expected " + tf.listType(tf.sourceLocationType()) + " but got " + $templateParameters.getType() + " for $templateParameters:" + $templateParameters); } Map kwParams = new HashMap(); @@ -6990,10 +6979,14 @@ public IConstructor TypeSymbol_array(IConstructor $baseType, IValue $size) { - return vf.constructor(_TypeSymbol_array_2 , $baseType, $size).asWithKeywordParameters().setParameters(kwParams); + return vf.constructor(_TypeSymbol_cClassTemplate_2 , $decl, $templateParameters).asWithKeywordParameters().setParameters(kwParams); } - public IConstructor TypeSymbol_nullPtr() { + public IConstructor TypeSymbol_struct(ISourceLocation $decl) { + + if (!$decl.getType().isSubtypeOf(tf.sourceLocationType())) { + throw new IllegalArgumentException("Expected " + tf.sourceLocationType() + " but got " + $decl.getType() + " for $decl:" + $decl); + } Map kwParams = new HashMap(); @@ -7001,21 +6994,17 @@ public IConstructor TypeSymbol_nullPtr() { - return vf.constructor(_TypeSymbol_nullPtr_0 ).asWithKeywordParameters().setParameters(kwParams); + return vf.constructor(_TypeSymbol_struct_1 , $decl).asWithKeywordParameters().setParameters(kwParams); } - public IConstructor TypeSymbol_aliasTemplate(ISourceLocation $decl, IList $templateArguments, IConstructor $type) { - - if (!$decl.getType().isSubtypeOf(tf.sourceLocationType())) { - throw new IllegalArgumentException("Expected " + tf.sourceLocationType() + " but got " + $decl.getType() + " for $decl:" + $decl); - } + public IConstructor TypeSymbol_array(IConstructor $baseType, IValue $size) { - if (!$templateArguments.getType().isSubtypeOf(tf.listType(_TypeSymbol))) { - throw new IllegalArgumentException("Expected " + tf.listType(_TypeSymbol) + " but got " + $templateArguments.getType() + " for $templateArguments:" + $templateArguments); + if (!$baseType.getType().isSubtypeOf(_TypeSymbol)) { + throw new IllegalArgumentException("Expected " + _TypeSymbol + " but got " + $baseType.getType() + " for $baseType:" + $baseType); } - if (!$type.getType().isSubtypeOf(_TypeSymbol)) { - throw new IllegalArgumentException("Expected " + _TypeSymbol + " but got " + $type.getType() + " for $type:" + $type); + if (!$size.getType().isSubtypeOf(tf.integerType())) { + throw new IllegalArgumentException("Expected " + tf.integerType() + " but got " + $size.getType() + " for $size:" + $size); } Map kwParams = new HashMap(); @@ -7024,7 +7013,18 @@ public IConstructor TypeSymbol_aliasTemplate(ISourceLocation $decl, IList $templ - return vf.constructor(_TypeSymbol_aliasTemplate_3 , $decl, $templateArguments, $type).asWithKeywordParameters().setParameters(kwParams); + return vf.constructor(_TypeSymbol_array_2 , $baseType, $size).asWithKeywordParameters().setParameters(kwParams); + } + + public IConstructor TypeSymbol_nullPtr() { + + Map kwParams = new HashMap(); + + + + + + return vf.constructor(_TypeSymbol_nullPtr_0 ).asWithKeywordParameters().setParameters(kwParams); } public IConstructor TypeSymbol_decimal128() { diff --git a/src/lang/cpp/internal/BindingsResolver.java b/src/lang/cpp/internal/BindingsResolver.java index f9d886cc..102feca5 100644 --- a/src/lang/cpp/internal/BindingsResolver.java +++ b/src/lang/cpp/internal/BindingsResolver.java @@ -376,7 +376,7 @@ private ISourceLocation resolveIEnumeration(IEnumeration binding, ISourceLocatio throw new RuntimeException("NYI" + binding.getClass().getSimpleName() + ": " + binding + " @ " + origin); } - private ISourceLocation resolveICPPBinding(ICPPBinding binding, ISourceLocation origin) throws URISyntaxException { + public ISourceLocation resolveICPPBinding(ICPPBinding binding, ISourceLocation origin) throws URISyntaxException { if (binding instanceof ICPPAliasTemplateInstance) { return resolveICPPAliasTemplateInstance((ICPPAliasTemplateInstance) binding, origin); } diff --git a/src/lang/cpp/internal/Parser.java b/src/lang/cpp/internal/Parser.java index a9b28116..0ec42172 100644 --- a/src/lang/cpp/internal/Parser.java +++ b/src/lang/cpp/internal/Parser.java @@ -54,7 +54,6 @@ import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpressionList; import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement; -import org.eclipse.cdt.core.dom.ast.IASTFieldDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFieldReference; import org.eclipse.cdt.core.dom.ast.IASTForStatement; import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression; @@ -142,7 +141,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerClause; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression.CaptureDefault; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification; @@ -1459,29 +1457,6 @@ private int visit(ICPPASTDesignatedInitializer initializer) { return PROCESS_ABORT; } - // InitializerClauses - private int visit(IASTInitializerClause initializerClause) { - at(initializerClause); - if (initializerClause instanceof IASTExpression) - visit((IASTExpression) initializerClause); - else if (initializerClause instanceof IASTInitializerList) - visit((IASTInitializerList) initializerClause); - else if (initializerClause instanceof ICASTDesignatedInitializer) - visit((ICASTDesignatedInitializer) initializerClause); - else if (initializerClause instanceof ICPPASTInitializerClause) - visit((ICPPASTInitializerClause) initializerClause); - else - throw new RuntimeException( - "Unknown IASTInitializerClause subclass " + initializerClause.getClass().getName() + " at " - + locs.forNode(initializerClause) + ". Exiting"); - return PROCESS_ABORT; - } - - private int visit(ICPPASTInitializerClause initializer) { - err("ICPPASTInitializerClause: " + initializer.getRawSignature()); - throw new RuntimeException("NYI at " + locs.forNode(initializer)); - } - // Declarators @Override @@ -1557,12 +1532,6 @@ private int visit(IASTArrayDeclarator declarator) { return PROCESS_ABORT; } - private int visit(IASTFieldDeclarator declarator) { - at(declarator); - err("FieldDeclarator: " + declarator.getRawSignature()); - throw new RuntimeException("NYI at " + locs.forNode(declarator)); - } - private int visit(IASTFunctionDeclarator declarator) { at(declarator); if (declarator instanceof IASTStandardFunctionDeclarator) {