private void defineClassField(FieldDeclarationNode fieldDeclaration, TypeBuilder typeBuilder) { var type = CompilerHelper.resolveTypeReference(context, typeBuilder.PackageName, fieldDeclaration.Type); foreach (var decl in fieldDeclaration.Declarators) { var name = context.getIdentifier(decl.NameOffset, decl.NameLength); if (typeBuilder.getField(name) != null) { context.addError(CompileErrorId.AlreadyDefinedField, fieldDeclaration, BytecodeHelper.getDisplayName(typeBuilder), name); } var fieldBuilder = typeBuilder.defineField(name, type); decl.addUserData(fieldBuilder); setFieldModifiers(fieldDeclaration, fieldBuilder); } }
private void setFieldModifiers(FieldDeclarationNode fieldDeclaration, FieldBuilder fieldBuilder) { foreach (var modifier in fieldDeclaration.Modifiers) { switch (modifier) { case Public: if (fieldBuilder.IsPrivate || fieldBuilder.IsProtected) { context.addError(CompileErrorId.PublicProtectedPrivate, fieldDeclaration); } else { fieldBuilder.setPublic(true); } break; case Protected: if (fieldBuilder.IsPrivate || fieldBuilder.IsPublic) { context.addError(CompileErrorId.PublicProtectedPrivate, fieldDeclaration); } else { fieldBuilder.setProtected(true); } break; case Private: if (fieldBuilder.IsProtected || fieldBuilder.IsPublic) { context.addError(CompileErrorId.PublicProtectedPrivate, fieldDeclaration); } else { fieldBuilder.setPrivate(true); } break; case Final: if (fieldBuilder.IsVolatile) { context.addError(CompileErrorId.FinalVolatile, fieldDeclaration); } else { fieldBuilder.setFinal(true); } break; case Static: fieldBuilder.setStatic(true); break; case Transient: fieldBuilder.setTransient(true); break; case Volatile: if (fieldBuilder.IsFinal) { context.addError(CompileErrorId.FinalVolatile, fieldDeclaration); } else { fieldBuilder.setVolatile(true); } break; default: context.addError(CompileErrorId.UnexpectedModifier, fieldDeclaration, modifier.toString().toLowerCase()); break; } } }
private ITypeMember parseFieldOrPropertyOrMethod(List<AnnotationSectionNode> annotations, EnumSet<Modifier> modifiers, bool partial, TypeReferenceNode type, int startPosition) { if (!isIdentifier(lexicalUnit)) { throw error(ParseErrorId.IdentifierExpected); } int sp = scanner.StartPosition; int len = getLexicalUnitLength(); saveScannerState(); var typeParameters = new ArrayList<SimpleNameTypeReferenceNode>(); nextLexicalUnit(true); parseTypeParameters(typeParameters); Modifier setterAccess = checkSetterAccess(); bool forceAsField = false; if (lexicalUnit == LexicalUnit.Multiply) { forceAsField = true; nextLexicalUnit(true); } switch (lexicalUnit) { case OpenParenthesis: if (setterAccess != Modifier.Public || forceAsField) { throw error(ParseErrorId.IdentifierExpected); } var methodDeclaration = new MethodDeclarationNode { IsPartial = partial, ReturnType = type, NameOffset = sp, NameLength = len, StartPosition = startPosition }; if (docCommentEndPosition > 0) { methodDeclaration.DocumentationOffset = docCommentStartPosition; methodDeclaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(methodDeclaration); methodDeclaration.Modifiers.addAll(modifiers); methodDeclaration.Annotations.addAll(annotations); foreach (var t in typeParameters) { if (t.TypeReferenceKind != TypeReferenceKind.SimpleName || t.TypeArguments.size() > 0) { throw error(ParseErrorId.SimpleNameExpected); } methodDeclaration.TypeParameters.add((SimpleNameTypeReferenceNode)t); } if (nextLexicalUnit(true) != LexicalUnit.CloseParenthesis) { parseFormalParameters(methodDeclaration.Parameters, LexicalUnit.CloseParenthesis); } else { nextLexicalUnit(true); } parseTypeParameterConstraintsClauses(methodDeclaration.ConstraintsClauses); switch (lexicalUnit) { case OpenBrace: methodDeclaration.Body = parseBlockStatement(); methodDeclaration.EndPosition = methodDeclaration.Body.EndPosition; break; case SemiColon: methodDeclaration.EndPosition = scanner.EndPosition; nextLexicalUnit(true); break; default: throw error(ParseErrorId.OpenBraceExpected); } return methodDeclaration; case OpenBrace: if (setterAccess != Modifier.Public || forceAsField) { throw error(ParseErrorId.IdentifierExpected); } var propertyDeclaration = new PropertyDeclarationNode { Type = type, NameOffset = sp, NameLength = len, StartPosition = startPosition }; if (docCommentEndPosition > 0) { propertyDeclaration.DocumentationOffset = docCommentStartPosition; propertyDeclaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(propertyDeclaration); propertyDeclaration.Modifiers.addAll(modifiers); nextLexicalUnit(true); parseAccessorDeclaration(propertyDeclaration, false); if (lexicalUnit != LexicalUnit.CloseBrace) { parseAccessorDeclaration(propertyDeclaration, false); } docCommentEndPosition = 0; propertyDeclaration.EndPosition = scanner.EndPosition; nextLexicalUnit(false); return propertyDeclaration; case Assign: { if (setterAccess != Modifier.Public || forceAsField) { throw error(ParseErrorId.IdentifierExpected); } var declaration = new FieldDeclarationNode { Type = type, StartPosition = startPosition }; if (docCommentEndPosition > 0) { declaration.DocumentationOffset = docCommentStartPosition; declaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(declaration); declaration.Modifiers.addAll(modifiers); declaration.Annotations.addAll(annotations); nextLexicalUnit(true); var decl = new VariableDeclaratorNode { NameOffset = sp, NameLength = len }; setSavedScannerState(decl); decl.Value = parseFieldInitializer(); decl.EndPosition = decl.Value.EndPosition; declaration.Declarators.add(decl); while (lexicalUnit == LexicalUnit.Comma) { if (!isIdentifier(nextLexicalUnit(true))) { throw error(ParseErrorId.IdentifierExpected); } decl = new VariableDeclaratorNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() }; setScannerState(decl); decl.EndPosition = scanner.EndPosition; declaration.Declarators.add(decl); if (nextLexicalUnit(true) == LexicalUnit.Assign) { nextLexicalUnit(true); decl.Value = parseFieldInitializer(); decl.EndPosition = decl.Value.EndPosition; } } docCommentEndPosition = 0; declaration.EndPosition = parseSemiColon(false, false); return declaration; } case Comma: { if (setterAccess != Modifier.Public || forceAsField) { throw error(ParseErrorId.IdentifierExpected); } var declaration = new FieldDeclarationNode { Type = type, StartPosition = startPosition }; if (docCommentEndPosition > 0) { declaration.DocumentationOffset = docCommentStartPosition; declaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(declaration); declaration.Modifiers.addAll(modifiers); declaration.Annotations.addAll(annotations); var decl = new VariableDeclaratorNode { NameOffset = sp, NameLength = len }; setSavedScannerState(decl); decl.EndPosition = sp + len; declaration.Declarators.add(decl); do { if (!isIdentifier(nextLexicalUnit(true))) { throw error(ParseErrorId.IdentifierExpected); } decl = new VariableDeclaratorNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() }; setScannerState(decl); declaration.Declarators.add(decl); decl.EndPosition = scanner.EndPosition; if (nextLexicalUnit(true) == LexicalUnit.Assign) { nextLexicalUnit(true); decl.Value = parseFieldInitializer(); decl.EndPosition = decl.Value.EndPosition; } } while (lexicalUnit == LexicalUnit.Comma); docCommentEndPosition = 0; declaration.EndPosition = parseSemiColon(false, false); return declaration; } case SemiColon: { if ((modifiers.contains(Modifier.Public) || modifiers.contains(Modifier.Protected)) && !forceAsField) { var propertyShortDeclaration = new PropertyDeclarationNode { Type = type, NameOffset = sp, NameLength = len, StartPosition = startPosition }; if (docCommentEndPosition > 0) { propertyShortDeclaration.DocumentationOffset = docCommentStartPosition; propertyShortDeclaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(propertyShortDeclaration); propertyShortDeclaration.Modifiers.addAll(modifiers); fakeParseShortProperty(propertyShortDeclaration, setterAccess, false); docCommentEndPosition = 0; propertyShortDeclaration.EndPosition = scanner.EndPosition; nextLexicalUnit(false); return propertyShortDeclaration; } var declaration = new FieldDeclarationNode { Type = type, StartPosition = startPosition }; if (docCommentEndPosition > 0) { declaration.DocumentationOffset = docCommentStartPosition; declaration.DocumentationLength = docCommentEndPosition - docCommentStartPosition; docCommentEndPosition = 0; } setSavedScannerState(declaration); declaration.Modifiers.addAll(modifiers); declaration.Annotations.addAll(annotations); var decl = new VariableDeclaratorNode { NameOffset = sp, NameLength = len }; setSavedScannerState(decl); decl.EndPosition = sp + len; declaration.Declarators.add(decl); docCommentEndPosition = 0; declaration.EndPosition = scanner.EndPosition; nextLexicalUnit(false); return declaration; } default: throw error(ParseErrorId.UnexpectedLexicalUnit); } }