1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.utilities; 26 27 import java.util.regex.*; 28 29 /** Provides helper routines for parsing simple C++ expressions such 30 as "(JavaThread *) 0xf00" and "Universe::_collectedHeap". */ 31 32 public class CPPExpressions { 33 private static Pattern castPattern; 34 35 /** Represents a cast expression such as "(JavaThread *) 36 0xf00". Returns both the type to which we are casting as well as 37 the address. */ 38 public static class CastExpr { 39 private String type; 40 private String address; 41 CastExpr(String type, String address)42 private CastExpr(String type, String address) { 43 this.type = type; 44 this.address = address; 45 } 46 getType()47 public String getType() { 48 return type; 49 } 50 getAddress()51 public String getAddress() { 52 return address; 53 } 54 } 55 56 /** Represents a static field expression such as "Universe::_collectedHeap". */ 57 public static class StaticFieldExpr { 58 private String containingType; 59 private String fieldName; 60 StaticFieldExpr(String containingType, String fieldName)61 private StaticFieldExpr(String containingType, String fieldName) { 62 this.containingType = containingType; 63 this.fieldName = fieldName; 64 } 65 getContainingType()66 public String getContainingType() { 67 return containingType; 68 } 69 getFieldName()70 public String getFieldName() { 71 return fieldName; 72 } 73 } 74 75 /** Attempts to parse the given string into a CastExpr. Returns null 76 if the string did not match the supported pattern. */ parseCast(String expr)77 public static CastExpr parseCast(String expr) { 78 if (castPattern == null) { 79 castPattern = Pattern.compile("\\s*\\(\\s*([0-9A-Za-z:_]*)\\s*\\*\\s*\\)\\s*([0-9a-zA-Z]*)\\s*"); 80 } 81 Matcher matcher = castPattern.matcher(expr); 82 if (matcher.matches()) { 83 String type = matcher.group(1); 84 String addr = matcher.group(2); 85 return new CastExpr(type, addr); 86 } 87 return null; 88 } 89 90 /** Attempts to parse the given string into a 91 StaticFieldExpr. Returns null if the string did not match the 92 supported pattern. */ parseStaticField(String expr)93 public static StaticFieldExpr parseStaticField(String expr) { 94 String sep = "::"; 95 int idx = expr.lastIndexOf(sep); 96 if (idx < 0) { 97 return null; 98 } 99 String containingType = expr.substring(0, idx); 100 String fieldName = expr.substring(idx + sep.length(), expr.length()); 101 return new StaticFieldExpr(containingType, fieldName); 102 } 103 } 104