001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.deep;
043:
044: import antlr.TokenStream;
045: import java.lang.ref.SoftReference;
046: import java.util.*;
047:
048: import org.netbeans.modules.cnd.api.model.*;
049: import org.netbeans.modules.cnd.api.model.deep.*;
050: import org.netbeans.modules.cnd.modelimpl.csm.core.*;
051:
052: import antlr.collections.AST;
053: import java.io.DataInput;
054: import java.io.DataOutput;
055: import java.io.IOException;
056: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
057: import org.netbeans.modules.cnd.modelimpl.parser.CPPParserEx;
058: import org.netbeans.modules.cnd.modelimpl.parser.generated.CPPTokenTypes;
059:
060: /**
061: * Lazy statements
062: * @author Vladimir Kvashin
063: */
064: public final class LazyCompoundStatementImpl extends StatementBase
065: implements CsmCompoundStatement {
066:
067: private SoftReference<List/*<CsmStatement>*/> statements = null;
068:
069: public LazyCompoundStatementImpl(AST ast, CsmFile file,
070: CsmFunction scope) {
071: super (ast, file, scope);
072: assert (ast.getType() == CPPTokenTypes.CSM_COMPOUND_STATEMENT_LAZY);
073: // we need to throw away the compound statement AST under this element
074: ast.setFirstChild(null);
075: }
076:
077: public CsmStatement.Kind getKind() {
078: return CsmStatement.Kind.COMPOUND;
079: }
080:
081: public List<CsmStatement> getStatements() {
082: if (statements == null) {
083: return createStatements();
084: } else {
085: List<CsmStatement> list = statements.get();
086: return (list == null) ? createStatements() : list;
087: }
088: }
089:
090: /**
091: * 1) Creates a list of statements
092: * 2) If it is created successfully, stores a soft reference to this list
093: * and returns this list,
094: * otherwise just returns empty list
095: */
096: public List/*<CsmStatement>*/createStatements() {
097: List list = new ArrayList();
098: if (renderStatements(list)) {
099: statements = new SoftReference(list);
100: return list;
101: } else {
102: return Collections.EMPTY_LIST;
103: }
104: }
105:
106: private boolean renderStatements(List list) {
107: FileImpl file = (FileImpl) getContainingFile();
108: TokenStream stream = file.getTokenStream(getStartOffset(),
109: getEndOffset());
110: if (stream == null) {
111: Utils.LOG
112: .severe("Can't create compound statement: can't create token stream for file "
113: + file.getAbsolutePath()); // NOI18N
114: return false;
115: } else {
116: AST resolvedAst = resolveLazyCompoundStatement(stream);
117: file.releaseTokenStream(stream);
118: renderStatements(resolvedAst, list);
119: return true;
120: }
121: }
122:
123: private void renderStatements(AST ast, List list) {
124: for (ast = (ast == null ? null : ast.getFirstChild()); ast != null; ast = ast
125: .getNextSibling()) {
126: CsmStatement stmt = AstRenderer.renderStatement(ast,
127: getContainingFile(), this );
128: if (stmt != null) {
129: list.add(stmt);
130: }
131: }
132: }
133:
134: public Collection<CsmScopeElement> getScopeElements() {
135: return (Collection) getStatements();
136: }
137:
138: private AST resolveLazyCompoundStatement(TokenStream tokenStream) {
139: int flags = CPPParserEx.CPP_CPLUSPLUS;
140: if (!TraceFlags.REPORT_PARSING_ERRORS || TraceFlags.DEBUG) {
141: flags |= CPPParserEx.CPP_SUPPRESS_ERRORS;
142: }
143: CPPParserEx parser = CPPParserEx.getInstance(
144: getContainingFile().getName().toString(), tokenStream,
145: flags);
146: parser.setLazyCompound(false);
147: parser.compound_statement();
148: AST out = parser.getAST();
149: return out;
150: }
151:
152: @Override
153: public void write(DataOutput output) throws IOException {
154: super .write(output);
155: }
156:
157: public LazyCompoundStatementImpl(DataInput input)
158: throws IOException {
159: super(input);
160: this.statements = null;
161: }
162: }
|